|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "useFrameworkReexport" | "noDirectMikroOrmImport" |
|
5
|
|
|
6
|
/** |
|
7
|
* Prefix shared by every `@mikro-orm/*` package, including the trailing slash. |
|
8
|
*/ |
|
9
|
const MIKRO_ORM_PREFIX = "@mikro-orm/" |
|
10
|
|
|
11
|
/** |
|
12
|
* The `@mikro-orm/*` subpackages that `@medusajs/framework` re-exports as of |
|
13
|
* v2.11.0 (`@medusajs/framework/mikro-orm/<sub>`). Imports of these can be |
|
14
|
* rewritten automatically. Other `@mikro-orm/*` subpaths are still flagged, but |
|
15
|
* without an autofix because there is no guaranteed framework re-export to point |
|
16
|
* them at. |
|
17
|
*/ |
|
18
|
const MIKRO_ORM_REEXPORTED_SUBPATHS: ReadonlySet<string> = new Set([ |
|
19
|
"cli", |
|
20
|
"core", |
|
21
|
"knex", |
|
22
|
"migrations", |
|
23
|
"postgresql", |
|
24
|
]) |
|
25
|
|
|
26
|
/** `awilix` is re-exported wholesale as `@medusajs/framework/awilix`. */ |
|
27
|
const AWILIX_SOURCE = "awilix" |
|
28
|
const AWILIX_REEXPORT = "@medusajs/framework/awilix" |
|
29
|
|
|
30
|
function rewriteSource(value: string): string | null { |
|
31
|
if (value === AWILIX_SOURCE) { |
|
32
|
return AWILIX_REEXPORT |
|
33
|
} |
|
34
|
|
|
35
|
if (value.startsWith(MIKRO_ORM_PREFIX)) { |
|
36
|
const rest = value.slice(MIKRO_ORM_PREFIX.length) |
|
37
|
if (MIKRO_ORM_REEXPORTED_SUBPATHS.has(rest)) { |
|
38
|
return `@medusajs/framework/mikro-orm/${rest}` |
|
39
|
} |
|
40
|
} |
|
41
|
|
|
42
|
return null |
|
43
|
} |
|
44
|
|
|
45
|
function check( |
|
46
|
context: Parameters<Parameters<typeof createRule>[0]["create"]>[0], |
|
47
|
source: TSESTree.StringLiteral | null | undefined |
|
48
|
) { |
|
49
|
if (!source) { |
|
50
|
return |
|
51
|
} |
|
52
|
const value = source.value |
|
53
|
if (typeof value !== "string") { |
|
54
|
return |
|
55
|
} |
|
56
|
|
|
57
|
const isMikroOrm = value.startsWith(MIKRO_ORM_PREFIX) |
|
58
|
if (value !== AWILIX_SOURCE && !isMikroOrm) { |
|
59
|
return |
|
60
|
} |
|
61
|
|
|
62
|
const replacement = rewriteSource(value) |
|
63
|
if (replacement) { |
|
64
|
context.report({ |
|
65
|
node: source, |
|
66
|
messageId: "useFrameworkReexport", |
|
67
|
data: { source: value, replacement }, |
|
68
|
fix(fixer) { |
|
69
|
const quote = source.raw[0] |
|
70
|
return fixer.replaceText(source, `${quote}${replacement}${quote}`) |
|
71
|
}, |
|
72
|
}) |
|
73
|
return |
|
74
|
} |
|
75
|
|
|
76
|
// A `@mikro-orm/*` subpath that the framework doesn't re-export — flag it but |
|
77
|
// leave the rewrite to the developer. |
|
78
|
context.report({ |
|
79
|
node: source, |
|
80
|
messageId: "noDirectMikroOrmImport", |
|
81
|
data: { source: value }, |
|
82
|
}) |
|
83
|
} |
|
84
|
|
|
85
|
export const rule = createRule<[], MessageIds>({ |
|
86
|
name: "no-mikroorm-direct-import", |
|
87
|
meta: { |
|
88
|
type: "suggestion", |
|
89
|
docs: { |
|
90
|
description: |
|
91
|
"Don't import MikroORM or awilix directly. As of Medusa v2.11, they're bundled into `@medusajs/framework`; use the `@medusajs/framework/mikro-orm/*` and `@medusajs/framework/awilix` re-exports.", |
|
92
|
}, |
|
93
|
messages: { |
|
94
|
useFrameworkReexport: |
|
95
|
"Import from `{{ replacement }}` instead of `{{ source }}`. MikroORM and awilix are re-exported by `@medusajs/framework` as of Medusa v2.11.", |
|
96
|
noDirectMikroOrmImport: |
|
97
|
"Don't import directly from `{{ source }}`. As of Medusa v2.11, MikroORM is bundled into `@medusajs/framework` — use a `@medusajs/framework/mikro-orm/*` re-export.", |
|
98
|
}, |
|
99
|
fixable: "code", |
|
100
|
schema: [], |
|
101
|
}, |
|
102
|
defaultOptions: [], |
|
103
|
create(context) { |
|
104
|
return { |
|
105
|
ImportDeclaration(node) { |
|
106
|
check(context, node.source) |
|
107
|
}, |
|
108
|
ExportNamedDeclaration(node) { |
|
109
|
if (node.source && node.source.type === AST_NODE_TYPES.Literal) { |
|
110
|
check(context, node.source as TSESTree.StringLiteral) |
|
111
|
} |
|
112
|
}, |
|
113
|
ExportAllDeclaration(node) { |
|
114
|
check(context, node.source) |
|
115
|
}, |
|
116
|
} |
|
117
|
}, |
|
118
|
}) |
|
119
|
|
|
120
|
export default rule |