|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "useFrameworkEntrypoint" | "noInternalImport" |
|
5
|
|
|
6
|
/** |
|
7
|
* Deprecated standalone packages whose contents moved into |
|
8
|
* `@medusajs/framework/*` subpaths. Importing them still works today but is |
|
9
|
* discouraged — the framework entry points are the supported surface. |
|
10
|
*/ |
|
11
|
const OLD_PACKAGE_REWRITES: Record<string, string> = { |
|
12
|
"@medusajs/utils": "@medusajs/framework/utils", |
|
13
|
"@medusajs/types": "@medusajs/framework/types", |
|
14
|
"@medusajs/workflows-sdk": "@medusajs/framework/workflows-sdk", |
|
15
|
"@medusajs/modules-sdk": "@medusajs/framework/modules-sdk", |
|
16
|
"@medusajs/orchestration": "@medusajs/framework/orchestration", |
|
17
|
} |
|
18
|
|
|
19
|
/** |
|
20
|
* Deep import into a `@medusajs/*` package's compiled build output, e.g. |
|
21
|
* `@medusajs/medusa/dist/...` or `@medusajs/framework/dist/...`. Requires a |
|
22
|
* path segment that is exactly `dist` so package names like |
|
23
|
* `@medusajs/some-dist-thing` aren't matched. |
|
24
|
*/ |
|
25
|
const MEDUSA_DIST_IMPORT = /^@medusajs\/[^/]+\/(?:[^/]+\/)*dist(?:\/|$)/ |
|
26
|
|
|
27
|
function check( |
|
28
|
context: Parameters<Parameters<typeof createRule>[0]["create"]>[0], |
|
29
|
source: TSESTree.StringLiteral | null | undefined |
|
30
|
) { |
|
31
|
if (!source) { |
|
32
|
return |
|
33
|
} |
|
34
|
const value = source.value |
|
35
|
if (typeof value !== "string") { |
|
36
|
return |
|
37
|
} |
|
38
|
|
|
39
|
const replacement = OLD_PACKAGE_REWRITES[value] |
|
40
|
if (replacement) { |
|
41
|
context.report({ |
|
42
|
node: source, |
|
43
|
messageId: "useFrameworkEntrypoint", |
|
44
|
data: { source: value, replacement }, |
|
45
|
fix(fixer) { |
|
46
|
const quote = source.raw[0] |
|
47
|
return fixer.replaceText(source, `${quote}${replacement}${quote}`) |
|
48
|
}, |
|
49
|
}) |
|
50
|
return |
|
51
|
} |
|
52
|
|
|
53
|
if (MEDUSA_DIST_IMPORT.test(value)) { |
|
54
|
context.report({ |
|
55
|
node: source, |
|
56
|
messageId: "noInternalImport", |
|
57
|
data: { source: value }, |
|
58
|
}) |
|
59
|
} |
|
60
|
} |
|
61
|
|
|
62
|
export const rule = createRule<[], MessageIds>({ |
|
63
|
name: "import-from-framework-not-internal", |
|
64
|
meta: { |
|
65
|
type: "suggestion", |
|
66
|
docs: { |
|
67
|
description: |
|
68
|
"Use the public `@medusajs/framework/*` entry points; don't import from a package's internal `dist/` build output or from deprecated standalone packages.", |
|
69
|
}, |
|
70
|
messages: { |
|
71
|
useFrameworkEntrypoint: |
|
72
|
"Import from `{{ replacement }}` instead of the deprecated `{{ source }}` package.", |
|
73
|
noInternalImport: |
|
74
|
"Don't import from internal build output `{{ source }}`. Use a public `@medusajs/framework/*` entry point instead.", |
|
75
|
}, |
|
76
|
fixable: "code", |
|
77
|
schema: [], |
|
78
|
}, |
|
79
|
defaultOptions: [], |
|
80
|
create(context) { |
|
81
|
return { |
|
82
|
ImportDeclaration(node) { |
|
83
|
check(context, node.source) |
|
84
|
}, |
|
85
|
ExportNamedDeclaration(node) { |
|
86
|
if (node.source && node.source.type === AST_NODE_TYPES.Literal) { |
|
87
|
check(context, node.source as TSESTree.StringLiteral) |
|
88
|
} |
|
89
|
}, |
|
90
|
ExportAllDeclaration(node) { |
|
91
|
check(context, node.source) |
|
92
|
}, |
|
93
|
} |
|
94
|
}, |
|
95
|
}) |
|
96
|
|
|
97
|
export default rule |