1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { AST_NODE_TYPES } from "@typescript-eslint/utils"
3 import { isResolveCallee } from "./container"
4 import { trackFrameworkUtilsImports } from "./service-scope"
5
6 export const DEFINE_LINK = "defineLink"
7
8 /**
9 * Receiver names that identify Medusa's Link service, normalized by
10 * {@link normalizeReceiverName} so casing and separators don't matter
11 * (`remoteLink`, `remote_link`, and `link_` all collapse into this set).
12 */
13 export const LINK_RECEIVER_NAMES: ReadonlySet<string> = new Set([
14 "link",
15 "links",
16 "remotelink",
17 "remotelinks",
18 "linkmodule",
19 "linkmodules",
20 ])
21
22 /** Type names that identify a binding as Medusa's Link service. */
23 export const LINK_TYPE_NAMES: ReadonlySet<string> = new Set([
24 "Link",
25 "ILink",
26 "ILinkModule",
27 "ILinkModuleService",
28 "RemoteLink",
29 "IRemoteLink",
30 ])
31
32 /**
33 * `ContainerRegistrationKeys` / `Modules` members that resolve the Link
34 * service. `REMOTE_LINK` is the deprecated pre-v2.2.0 key.
35 */
36 export const LINK_RESOLVE_KEYS: ReadonlySet<string> = new Set([
37 "LINK",
38 "LINK_MODULES",
39 "REMOTE_LINK",
40 ])
41
42 /** String registration keys that resolve the Link service. */
43 export const LINK_RESOLVE_STRINGS: ReadonlySet<string> = new Set([
44 "link",
45 "link_modules",
46 "remoteLink",
47 ])
48
49 /** Lowercases and strips separators so `remote_link` and `remoteLink` match. */
50 export function normalizeReceiverName(name: string): string {
51 return name.toLowerCase().replace(/[^a-z0-9]/g, "")
52 }
53
54 /** True when a type annotation names one of {@link LINK_TYPE_NAMES}. */
55 export function isLinkTypeAnnotation(
56 annotation: TSESTree.TSTypeAnnotation | undefined
57 ): boolean {
58 const typeNode = annotation?.typeAnnotation
59 return (
60 typeNode?.type === AST_NODE_TYPES.TSTypeReference &&
61 typeNode.typeName.type === AST_NODE_TYPES.Identifier &&
62 LINK_TYPE_NAMES.has(typeNode.typeName.name)
63 )
64 }
65
66 /**
67 * True when `node` is a container resolution that yields the Link service —
68 * `container.resolve(ContainerRegistrationKeys.LINK)`, `resolve(Modules.LINK)`,
69 * `resolve<Link>(...)`, or `resolve("link_modules")`. Unwraps `await`.
70 */
71 export function isLinkResolveCall(
72 node: TSESTree.Node | null | undefined
73 ): boolean {
74 let expression = node
75 while (expression?.type === AST_NODE_TYPES.AwaitExpression) {
76 expression = expression.argument
77 }
78 if (
79 expression?.type !== AST_NODE_TYPES.CallExpression ||
80 !isResolveCallee(expression.callee)
81 ) {
82 return false
83 }
84
85 const typeArgument = expression.typeArguments?.params[0]
86 if (
87 typeArgument?.type === AST_NODE_TYPES.TSTypeReference &&
88 typeArgument.typeName.type === AST_NODE_TYPES.Identifier &&
89 LINK_TYPE_NAMES.has(typeArgument.typeName.name)
90 ) {
91 return true
92 }
93
94 const key = expression.arguments[0]
95 if (!key) {
96 return false
97 }
98 if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") {
99 return LINK_RESOLVE_STRINGS.has(key.value)
100 }
101 return (
102 key.type === AST_NODE_TYPES.MemberExpression &&
103 !key.computed &&
104 key.property.type === AST_NODE_TYPES.Identifier &&
105 LINK_RESOLVE_KEYS.has(key.property.name)
106 )
107 }
108
109 /**
110 * Records the local name of a `VariableDeclarator` or `PropertyDefinition`
111 * that holds the Link service, recognized either by its type annotation or by
112 * a container resolution initializer.
113 *
114 * Call from the matching visitors; pair with {@link isLinkReceiver}.
115 */
116 export function trackLinkBinding(
117 node: TSESTree.VariableDeclarator | TSESTree.PropertyDefinition,
118 localNames: Set<string>
119 ): void {
120 if (node.type === AST_NODE_TYPES.VariableDeclarator) {
121 if (node.id.type !== AST_NODE_TYPES.Identifier) {
122 return
123 }
124 if (
125 isLinkTypeAnnotation(node.id.typeAnnotation) ||
126 isLinkResolveCall(node.init)
127 ) {
128 localNames.add(node.id.name)
129 }
130 return
131 }
132
133 if (node.computed || node.key.type !== AST_NODE_TYPES.Identifier) {
134 return
135 }
136 if (
137 isLinkTypeAnnotation(node.typeAnnotation) ||
138 isLinkResolveCall(node.value)
139 ) {
140 localNames.add(node.key.name)
141 }
142 }
143
144 /** The name a call's receiver is known by, e.g. `link`, `this.link_`. */
145 export function getReceiverName(object: TSESTree.Node): string | null {
146 if (object.type === AST_NODE_TYPES.Identifier) {
147 return object.name
148 }
149 if (
150 object.type === AST_NODE_TYPES.MemberExpression &&
151 !object.computed &&
152 object.property.type === AST_NODE_TYPES.Identifier
153 ) {
154 return object.property.name
155 }
156 return null
157 }
158
159 /**
160 * True when `object` is recognizable as Medusa's Link service — either a name
161 * tracked by {@link trackLinkBinding} or one of {@link LINK_RECEIVER_NAMES}.
162 *
163 * Without this gate a rule matching link method names would also match
164 * identically-named methods on unrelated third-party SDKs.
165 */
166 export function isLinkReceiver(
167 object: TSESTree.Node,
168 localNames: ReadonlySet<string>
169 ): boolean {
170 const receiver = getReceiverName(object)
171 if (!receiver) {
172 return false
173 }
174 return (
175 localNames.has(receiver) ||
176 LINK_RECEIVER_NAMES.has(normalizeReceiverName(receiver))
177 )
178 }
179
180 /**
181 * Records local names bound to `defineLink` from `@medusajs/framework/utils`
182 * (honors `import { defineLink as dl }`).
183 *
184 * Call from an `ImportDeclaration` visitor.
185 */
186 export function trackDefineLinkImports(
187 node: TSESTree.ImportDeclaration,
188 localNames: Set<string>
189 ): void {
190 trackFrameworkUtilsImports(node, { [DEFINE_LINK]: localNames })
191 }
192
193 /** True when `node` calls a tracked `defineLink` binding. */
194 export function isDefineLinkCall(
195 node: TSESTree.CallExpression,
196 localNames: ReadonlySet<string>
197 ): boolean {
198 return (
199 localNames.size > 0 &&
200 node.callee.type === AST_NODE_TYPES.Identifier &&
201 localNames.has(node.callee.name)
202 )
203 }
204
205 /**
206 * Matches `<X>.linkable.<y>` at any depth on `<X>`, e.g.
207 * `ProductModule.linkable.product` or `foo.bar.linkable.product`.
208 */
209 export function isLinkableMemberChain(node: TSESTree.Node): boolean {
210 if (node.type !== AST_NODE_TYPES.MemberExpression || node.computed) {
211 return false
212 }
213 const object = node.object
214 if (object.type !== AST_NODE_TYPES.MemberExpression || object.computed) {
215 return false
216 }
217 return (
218 object.property.type === AST_NODE_TYPES.Identifier &&
219 object.property.name === "linkable"
220 )
221 }
222
223 /**
224 * Walks a member chain (e.g. `BlogModule.linkable.post.id`) and returns true
225 * when any segment is a non-computed `.linkable` access.
226 */
227 export function hasLinkableInChain(node: TSESTree.Node): boolean {
228 let current: TSESTree.Node | undefined = node
229 while (current && current.type === AST_NODE_TYPES.MemberExpression) {
230 if (
231 !current.computed &&
232 current.property.type === AST_NODE_TYPES.Identifier &&
233 current.property.name === "linkable"
234 ) {
235 return true
236 }
237 current = current.object
238 }
239 return false
240 }