|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "zodImportSource" |
|
5
|
|
|
6
|
const BARE_ZOD = "zod" |
|
7
|
const MEDUSA_ZOD = "@medusajs/framework/zod" |
|
8
|
|
|
9
|
function check( |
|
10
|
context: Parameters<Parameters<typeof createRule>[0]["create"]>[0], |
|
11
|
source: TSESTree.StringLiteral | null | undefined |
|
12
|
) { |
|
13
|
if (!source) { |
|
14
|
return |
|
15
|
} |
|
16
|
if (source.value !== BARE_ZOD) { |
|
17
|
return |
|
18
|
} |
|
19
|
context.report({ |
|
20
|
node: source, |
|
21
|
messageId: "zodImportSource", |
|
22
|
fix(fixer) { |
|
23
|
const quote = source.raw[0] |
|
24
|
return fixer.replaceText(source, `${quote}${MEDUSA_ZOD}${quote}`) |
|
25
|
}, |
|
26
|
}) |
|
27
|
} |
|
28
|
|
|
29
|
export const rule = createRule<[], MessageIds>({ |
|
30
|
name: "zod-import-source", |
|
31
|
meta: { |
|
32
|
type: "suggestion", |
|
33
|
docs: { |
|
34
|
description: |
|
35
|
"Zod should be imported from `@medusajs/framework/zod` (Medusa v2.13+), not the bare `zod` package.", |
|
36
|
}, |
|
37
|
messages: { |
|
38
|
zodImportSource: |
|
39
|
"Import Zod from `@medusajs/framework/zod` instead of `zod`.", |
|
40
|
}, |
|
41
|
fixable: "code", |
|
42
|
schema: [], |
|
43
|
}, |
|
44
|
defaultOptions: [], |
|
45
|
create(context) { |
|
46
|
return { |
|
47
|
ImportDeclaration(node) { |
|
48
|
check(context, node.source) |
|
49
|
}, |
|
50
|
ExportNamedDeclaration(node) { |
|
51
|
if (node.source && node.source.type === AST_NODE_TYPES.Literal) { |
|
52
|
check(context, node.source as TSESTree.StringLiteral) |
|
53
|
} |
|
54
|
}, |
|
55
|
ExportAllDeclaration(node) { |
|
56
|
check(context, node.source) |
|
57
|
}, |
|
58
|
} |
|
59
|
}, |
|
60
|
}) |
|
61
|
|
|
62
|
export default rule |