|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { |
|
5
|
createMedusaServiceBindings, |
|
6
|
getParamDecorators, |
|
7
|
getParamIdentifier, |
|
8
|
hasDecoratorWithLocalName, |
|
9
|
isContextTypedIdentifier, |
|
10
|
isMedusaServiceSuper, |
|
11
|
trackFrameworkUtilsImports, |
|
12
|
trackMedusaServiceImports, |
|
13
|
} from "../../util/service-scope" |
|
14
|
|
|
15
|
type MessageIds = "missingMedusaContext" |
|
16
|
|
|
17
|
const MEDUSA_CONTEXT = "MedusaContext" |
|
18
|
|
|
19
|
export const rule = createRule<[], MessageIds>({ |
|
20
|
name: "medusa-context-on-context-param", |
|
21
|
meta: { |
|
22
|
type: "problem", |
|
23
|
docs: { |
|
24
|
description: |
|
25
|
"Service method parameters typed `Context` must be decorated with `@MedusaContext()`.", |
|
26
|
}, |
|
27
|
fixable: "code", |
|
28
|
messages: { |
|
29
|
missingMedusaContext: |
|
30
|
"Service method parameters typed `Context` must be decorated with `@MedusaContext()`.", |
|
31
|
}, |
|
32
|
schema: [], |
|
33
|
}, |
|
34
|
defaultOptions: [], |
|
35
|
create(context) { |
|
36
|
const serviceBindings = createMedusaServiceBindings() |
|
37
|
const medusaContextBinding = new Set<string>() |
|
38
|
|
|
39
|
function checkClass( |
|
40
|
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression |
|
41
|
) { |
|
42
|
if (!isMedusaServiceSuper(node.superClass, serviceBindings)) { |
|
43
|
return |
|
44
|
} |
|
45
|
|
|
46
|
for (const member of node.body.body) { |
|
47
|
if (member.type !== AST_NODE_TYPES.MethodDefinition) { |
|
48
|
continue |
|
49
|
} |
|
50
|
if (member.kind === "constructor") { |
|
51
|
continue |
|
52
|
} |
|
53
|
if (member.computed) { |
|
54
|
continue |
|
55
|
} |
|
56
|
const value = member.value |
|
57
|
// Skip TypeScript overload signatures (bodyless declarations): the |
|
58
|
// `@MedusaContext()` decorator can only live on the implementation, |
|
59
|
// which is the method that carries a function body and is checked on |
|
60
|
// its own. |
|
61
|
if (value.type !== AST_NODE_TYPES.FunctionExpression) { |
|
62
|
continue |
|
63
|
} |
|
64
|
|
|
65
|
for (const param of value.params) { |
|
66
|
const id = getParamIdentifier(param) |
|
67
|
if (!id) { |
|
68
|
continue |
|
69
|
} |
|
70
|
if (!isContextTypedIdentifier(id)) { |
|
71
|
continue |
|
72
|
} |
|
73
|
|
|
74
|
if ( |
|
75
|
hasDecoratorWithLocalName( |
|
76
|
getParamDecorators(param), |
|
77
|
medusaContextBinding |
|
78
|
) |
|
79
|
) { |
|
80
|
continue |
|
81
|
} |
|
82
|
|
|
83
|
const localName = |
|
84
|
medusaContextBinding.values().next().value ?? MEDUSA_CONTEXT |
|
85
|
const canAutofix = medusaContextBinding.size > 0 |
|
86
|
|
|
87
|
context.report({ |
|
88
|
node: param, |
|
89
|
messageId: "missingMedusaContext", |
|
90
|
fix: canAutofix |
|
91
|
? (fixer) => fixer.insertTextBefore(param, `@${localName}() `) |
|
92
|
: undefined, |
|
93
|
}) |
|
94
|
} |
|
95
|
} |
|
96
|
} |
|
97
|
|
|
98
|
return { |
|
99
|
ImportDeclaration(node) { |
|
100
|
trackMedusaServiceImports(node, serviceBindings) |
|
101
|
trackFrameworkUtilsImports(node, { |
|
102
|
[MEDUSA_CONTEXT]: medusaContextBinding, |
|
103
|
}) |
|
104
|
}, |
|
105
|
|
|
106
|
ClassDeclaration: checkClass, |
|
107
|
ClassExpression: checkClass, |
|
108
|
} |
|
109
|
}, |
|
110
|
}) |
|
111
|
|
|
112
|
export default rule |