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 hasContextParam,
7 hasDecoratorWithLocalName,
8 isMedusaServiceSuper,
9 trackFrameworkUtilsImports,
10 trackMedusaServiceImports,
11 } from "../../util/service-scope"
12
13 type MessageIds = "missingInjectManager" | "missingInjectTransactionManager"
14
15 const INJECT_MANAGER = "InjectManager"
16 const INJECT_TRANSACTION_MANAGER = "InjectTransactionManager"
17
18 export const rule = createRule<[], MessageIds>({
19 name: "use-inject-manager-on-public-methods",
20 meta: {
21 type: "suggestion",
22 docs: {
23 description:
24 "Service methods that accept a `Context` parameter should be decorated with `@InjectManager()` (public) or `@InjectTransactionManager()` (protected).",
25 },
26 fixable: "code",
27 messages: {
28 missingInjectManager:
29 "Public service methods that accept a `Context` parameter must be decorated with `@InjectManager()`.",
30 missingInjectTransactionManager:
31 "Protected/private service methods that accept a `Context` parameter must be decorated with `@InjectTransactionManager()`.",
32 },
33 schema: [],
34 },
35 defaultOptions: [],
36 create(context) {
37 const serviceBindings = createMedusaServiceBindings()
38 const injectManagerBinding = new Set<string>()
39 const injectTransactionManagerBinding = new Set<string>()
40
41 function checkClass(
42 node: TSESTree.ClassDeclaration | TSESTree.ClassExpression
43 ) {
44 if (!isMedusaServiceSuper(node.superClass, serviceBindings)) {
45 return
46 }
47
48 for (const member of node.body.body) {
49 if (member.type !== AST_NODE_TYPES.MethodDefinition) {
50 continue
51 }
52 if (member.kind === "constructor") {
53 continue
54 }
55 if (member.kind !== "method") {
56 continue
57 }
58 if (member.computed) {
59 continue
60 }
61 const value = member.value
62 // Skip TypeScript overload signatures (bodyless declarations): the
63 // decorator can only live on the implementation, which is the method
64 // that carries a function body and is checked on its own.
65 if (value.type !== AST_NODE_TYPES.FunctionExpression) {
66 continue
67 }
68
69 if (!hasContextParam(value)) {
70 continue
71 }
72
73 const isInternal =
74 member.accessibility === "protected" ||
75 member.accessibility === "private"
76 const requiredLocalNames = isInternal
77 ? injectTransactionManagerBinding
78 : injectManagerBinding
79 const canonicalName = isInternal
80 ? INJECT_TRANSACTION_MANAGER
81 : INJECT_MANAGER
82 const messageId: MessageIds = isInternal
83 ? "missingInjectTransactionManager"
84 : "missingInjectManager"
85
86 if (hasDecoratorWithLocalName(member.decorators, requiredLocalNames)) {
87 continue
88 }
89
90 const localName =
91 requiredLocalNames.values().next().value ?? canonicalName
92 const canAutofix = requiredLocalNames.size > 0
93
94 context.report({
95 node: member.key,
96 messageId,
97 fix: canAutofix
98 ? (fixer) => {
99 const indent = " ".repeat(member.loc.start.column)
100 return fixer.insertTextBefore(
101 member,
102 `@${localName}()\n${indent}`
103 )
104 }
105 : undefined,
106 })
107 }
108 }
109
110 return {
111 ImportDeclaration(node) {
112 trackMedusaServiceImports(node, serviceBindings)
113 trackFrameworkUtilsImports(node, {
114 [INJECT_MANAGER]: injectManagerBinding,
115 [INJECT_TRANSACTION_MANAGER]: injectTransactionManagerBinding,
116 })
117 },
118
119 ClassDeclaration: checkClass,
120 ClassExpression: checkClass,
121 }
122 },
123 })
124
125 export default rule