1 import type { TSESLint } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import { TYPES_SOURCES } from "../../constants"
4 import { isResolveCallee } from "../../util/container"
5
6 type MessageIds =
7 | "remoteLinkString"
8 | "remoteLinkRegistrationKey"
9 | "remoteLinkTypeImport"
10
11 const REMOTE_LINK_TYPE_RENAMES: Record<string, string> = {
12 RemoteLink: "Link",
13 IRemoteLink: "ILink",
14 }
15
16 export const rule = createRule<[], MessageIds>({
17 name: "prefer-link-over-remote-link",
18 meta: {
19 type: "suggestion",
20 docs: {
21 description:
22 "`RemoteLink` is deprecated since Medusa v2.2.0; use `Link` instead.",
23 },
24 fixable: "code",
25 messages: {
26 remoteLinkString:
27 '`"remoteLink"` is deprecated. Resolve `"link"` (or `ContainerRegistrationKeys.LINK`) instead.',
28 remoteLinkRegistrationKey:
29 "`ContainerRegistrationKeys.REMOTE_LINK` is deprecated. Use `ContainerRegistrationKeys.LINK` instead.",
30 remoteLinkTypeImport:
31 "`{{name}}` is deprecated. Import `{{replacement}}` from `@medusajs/framework/types` instead.",
32 },
33 schema: [],
34 },
35 defaultOptions: [],
36 create(context) {
37 return {
38 ImportDeclaration(node) {
39 if (typeof node.source.value !== "string") {
40 return
41 }
42 if (!TYPES_SOURCES.has(node.source.value)) {
43 return
44 }
45 for (const specifier of node.specifiers) {
46 if (specifier.type !== "ImportSpecifier") {
47 continue
48 }
49 if (specifier.imported.type !== "Identifier") {
50 continue
51 }
52 const importedName = specifier.imported.name
53 const replacement = REMOTE_LINK_TYPE_RENAMES[importedName]
54 if (!replacement) {
55 continue
56 }
57 context.report({
58 node: specifier.imported,
59 messageId: "remoteLinkTypeImport",
60 data: { name: importedName, replacement },
61 fix(fixer: TSESLint.RuleFixer) {
62 // Only safe to rewrite when the local binding is the same as the
63 // imported name (no alias) — otherwise renaming the imported part
64 // would break references to the local alias elsewhere in the file.
65 if (specifier.local.name !== importedName) {
66 return fixer.replaceText(specifier.imported, replacement)
67 }
68 return fixer.replaceText(specifier, replacement)
69 },
70 })
71 }
72 },
73
74 CallExpression(node) {
75 if (!isResolveCallee(node.callee)) {
76 return
77 }
78 const arg = node.arguments[0]
79 if (!arg) {
80 return
81 }
82
83 if (arg.type === "Literal" && arg.value === "remoteLink") {
84 context.report({
85 node: arg,
86 messageId: "remoteLinkString",
87 fix(fixer) {
88 const raw = arg.raw
89 const quote = raw[0] === "'" ? "'" : '"'
90 return fixer.replaceText(arg, `${quote}link${quote}`)
91 },
92 })
93 return
94 }
95
96 if (
97 arg.type === "MemberExpression" &&
98 !arg.computed &&
99 arg.property.type === "Identifier" &&
100 arg.property.name === "REMOTE_LINK"
101 ) {
102 context.report({
103 node: arg,
104 messageId: "remoteLinkRegistrationKey",
105 fix(fixer) {
106 return fixer.replaceText(arg.property, "LINK")
107 },
108 })
109 }
110 },
111 }
112 },
113 })
114
115 export default rule