1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { AST_NODE_TYPES } from "@typescript-eslint/utils"
3 import { FRAMEWORK_UTILS_SOURCE } from "../constants"
4
5 export const MEDUSA_SERVICE = "MedusaService"
6 export const CONTEXT_TYPE = "Context"
7
8 export type MedusaServiceBindings = {
9 medusaService: Set<string>
10 }
11
12 export function createMedusaServiceBindings(): MedusaServiceBindings {
13 return {
14 medusaService: new Set(),
15 }
16 }
17
18 /**
19 * Records local names bound to `MedusaService` from
20 * `@medusajs/framework/utils` (honors `import { MedusaService as MS }`).
21 *
22 * Call from an `ImportDeclaration` visitor.
23 */
24 export function trackMedusaServiceImports(
25 node: TSESTree.ImportDeclaration,
26 bindings: MedusaServiceBindings
27 ): void {
28 if (node.source.value !== FRAMEWORK_UTILS_SOURCE) {
29 return
30 }
31 for (const specifier of node.specifiers) {
32 if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) {
33 continue
34 }
35 if (specifier.imported.type !== AST_NODE_TYPES.Identifier) {
36 continue
37 }
38 if (specifier.imported.name !== MEDUSA_SERVICE) {
39 continue
40 }
41 bindings.medusaService.add(specifier.local.name)
42 }
43 }
44
45 /**
46 * True when `superClass` is a `CallExpression` whose callee resolves to a
47 * tracked `MedusaService` binding (e.g. `class X extends MedusaService({...})`).
48 */
49 export function isMedusaServiceSuper(
50 superClass: TSESTree.LeftHandSideExpression | null,
51 bindings: MedusaServiceBindings
52 ): boolean {
53 if (!superClass) {
54 return false
55 }
56 if (superClass.type !== AST_NODE_TYPES.CallExpression) {
57 return false
58 }
59 const callee = superClass.callee
60 if (callee.type !== AST_NODE_TYPES.Identifier) {
61 return false
62 }
63 return bindings.medusaService.has(callee.name)
64 }
65
66 /**
67 * Generic import tracker for named specifiers from
68 * `@medusajs/framework/utils`. Given a map of canonical imported names to
69 * `Set<string>` buckets, populates each bucket with the *local* name a
70 * consumer bound that import to (honors `import { Foo as F }`).
71 *
72 * Call from an `ImportDeclaration` visitor. Imports from other sources are
73 * ignored.
74 */
75 export function trackFrameworkUtilsImports(
76 node: TSESTree.ImportDeclaration,
77 buckets: Record<string, Set<string>>
78 ): void {
79 if (node.source.value !== FRAMEWORK_UTILS_SOURCE) {
80 return
81 }
82 for (const specifier of node.specifiers) {
83 if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) {
84 continue
85 }
86 if (specifier.imported.type !== AST_NODE_TYPES.Identifier) {
87 continue
88 }
89 const bucket = buckets[specifier.imported.name]
90 if (!bucket) {
91 continue
92 }
93 bucket.add(specifier.local.name)
94 }
95 }
96
97 /**
98 * Unwraps a parameter pattern to its binding `Identifier`. Handles both
99 * `foo: T` (bare `Identifier`) and `foo: T = x` (`AssignmentPattern` with an
100 * `Identifier` on the left). Returns `null` for destructuring patterns,
101 * rest elements, and TS parameter properties.
102 */
103 export function getParamIdentifier(
104 param: TSESTree.Parameter
105 ): TSESTree.Identifier | null {
106 if (param.type === AST_NODE_TYPES.Identifier) {
107 return param
108 }
109 if (
110 param.type === AST_NODE_TYPES.AssignmentPattern &&
111 param.left.type === AST_NODE_TYPES.Identifier
112 ) {
113 return param.left
114 }
115 return null
116 }
117
118 /**
119 * True when `id`'s type annotation is a bare reference to `Context`.
120 *
121 * AST-level check only — does not resolve the import; a custom local
122 * `type Context = ...` would also match. Acceptable: the rules that use
123 * this gate are scoped to Medusa service classes where `Context` is the
124 * convention.
125 */
126 export function isContextTypedIdentifier(id: TSESTree.Identifier): boolean {
127 const annotation = id.typeAnnotation?.typeAnnotation
128 if (!annotation) {
129 return false
130 }
131 if (annotation.type !== AST_NODE_TYPES.TSTypeReference) {
132 return false
133 }
134 if (annotation.typeName.type !== AST_NODE_TYPES.Identifier) {
135 return false
136 }
137 return annotation.typeName.name === CONTEXT_TYPE
138 }
139
140 /** True when any of `fn`'s parameters is typed `Context`. */
141 export function hasContextParam(
142 fn: TSESTree.FunctionExpression | TSESTree.TSEmptyBodyFunctionExpression
143 ): boolean {
144 for (const param of fn.params) {
145 const id = getParamIdentifier(param)
146 if (id && isContextTypedIdentifier(id)) {
147 return true
148 }
149 }
150 return false
151 }
152
153 /**
154 * Returns parameter decorators attached to `param`. TSESTree puts
155 * decorators on the outermost pattern (the `AssignmentPattern` when there's
156 * a default), but we fall back to the inner `Identifier`'s decorators
157 * defensively.
158 */
159 export function getParamDecorators(
160 param: TSESTree.Parameter
161 ): TSESTree.Decorator[] {
162 const outer = (param as { decorators?: TSESTree.Decorator[] }).decorators
163 if (outer && outer.length) {
164 return outer
165 }
166 if (param.type === AST_NODE_TYPES.AssignmentPattern) {
167 const inner = (param.left as { decorators?: TSESTree.Decorator[] })
168 .decorators
169 if (inner && inner.length) {
170 return inner
171 }
172 }
173 return []
174 }
175
176 /**
177 * True when any decorator in `decorators` is `@<name>()` / `@<name>` where
178 * `<name>` is in `localNames`. Used to gate rules whose autofix or
179 * detection relies on decorator presence.
180 */
181 export function hasDecoratorWithLocalName(
182 decorators: TSESTree.Decorator[] | undefined,
183 localNames: Set<string>
184 ): boolean {
185 if (!decorators?.length) {
186 return false
187 }
188 for (const decorator of decorators) {
189 const expr = decorator.expression
190 let calleeName: string | null = null
191 if (expr.type === AST_NODE_TYPES.CallExpression) {
192 if (expr.callee.type === AST_NODE_TYPES.Identifier) {
193 calleeName = expr.callee.name
194 }
195 } else if (expr.type === AST_NODE_TYPES.Identifier) {
196 calleeName = expr.name
197 }
198 if (calleeName && localNames.has(calleeName)) {
199 return true
200 }
201 }
202 return false
203 }