1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("use-inject-manager-on-public-methods", rule, {
7 valid: [
8 // Public method with @InjectManager() decorator.
9 {
10 code: `
11 import { MedusaService, InjectManager } from "@medusajs/framework/utils"
12 class FooService extends MedusaService({}) {
13 @InjectManager()
14 async list(sharedContext: Context = {}) {}
15 }
16 `,
17 },
18 // Protected method with @InjectTransactionManager() decorator.
19 {
20 code: `
21 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
22 class FooService extends MedusaService({}) {
23 @InjectTransactionManager()
24 protected async list_(sharedContext: Context = {}) {}
25 }
26 `,
27 },
28 // Method without a Context parameter is not flagged.
29 {
30 code: `
31 import { MedusaService } from "@medusajs/framework/utils"
32 class FooService extends MedusaService({}) {
33 async list(filters: object) {}
34 }
35 `,
36 },
37 // Private method with @InjectTransactionManager() decorator.
38 {
39 code: `
40 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
41 class FooService extends MedusaService({}) {
42 @InjectTransactionManager()
43 private async helper(sharedContext: Context = {}) {}
44 }
45 `,
46 },
47 // Non-service class is not checked.
48 {
49 code: `
50 class Plain {
51 async list(sharedContext: Context = {}) {}
52 }
53 `,
54 },
55 // A `*Service`-named class that doesn't extend `MedusaService` is not a
56 // service class — its undecorated Context method is not flagged.
57 {
58 code: `
59 import { InjectManager } from "@medusajs/framework/utils"
60 class OrderService {
61 async list(sharedContext: Context = {}) {}
62 }
63 `,
64 },
65 // Constructor is exempt.
66 {
67 code: `
68 import { MedusaService } from "@medusajs/framework/utils"
69 class FooService extends MedusaService({}) {
70 constructor(sharedContext: Context) {
71 super(...arguments)
72 }
73 }
74 `,
75 },
76 // Aliased decorator import is honored.
77 {
78 code: `
79 import { MedusaService, InjectManager as IM } from "@medusajs/framework/utils"
80 class FooService extends MedusaService({}) {
81 @IM()
82 async list(sharedContext: Context = {}) {}
83 }
84 `,
85 },
86 // Getter / setter / static methods aren't covered by this rule (no Context params anyway).
87 {
88 code: `
89 import { MedusaService } from "@medusajs/framework/utils"
90 class FooService extends MedusaService({}) {
91 get name() { return "foo" }
92 static helper() {}
93 }
94 `,
95 },
96 // Overload signatures (bodyless) are ignored — the decorator lives on the
97 // implementation, which carries it here.
98 {
99 code: `
100 import { MedusaService, InjectManager } from "@medusajs/framework/utils"
101 class FooService extends MedusaService({}) {
102 list(id: string, sharedContext?: Context): Promise<string>
103 list(id: number, sharedContext?: Context): Promise<number>
104 @InjectManager()
105 async list(id: any, sharedContext: Context = {}): Promise<any> {}
106 }
107 `,
108 },
109 ],
110 invalid: [
111 // Public method with Context param missing @InjectManager() — autofix inserts decorator.
112 {
113 code: `
114 import { MedusaService, InjectManager } from "@medusajs/framework/utils"
115 class FooService extends MedusaService({}) {
116 async list(sharedContext: Context = {}) {}
117 }
118 `,
119 output: `
120 import { MedusaService, InjectManager } from "@medusajs/framework/utils"
121 class FooService extends MedusaService({}) {
122 @InjectManager()
123 async list(sharedContext: Context = {}) {}
124 }
125 `,
126 errors: [{ messageId: "missingInjectManager" }],
127 },
128 // Protected method with Context param missing @InjectTransactionManager() — autofix.
129 {
130 code: `
131 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
132 class FooService extends MedusaService({}) {
133 protected async list_(sharedContext: Context = {}) {}
134 }
135 `,
136 output: `
137 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
138 class FooService extends MedusaService({}) {
139 @InjectTransactionManager()
140 protected async list_(sharedContext: Context = {}) {}
141 }
142 `,
143 errors: [{ messageId: "missingInjectTransactionManager" }],
144 },
145 // No import for the decorator — reported without autofix.
146 {
147 code: `
148 import { MedusaService } from "@medusajs/framework/utils"
149 class FooService extends MedusaService({}) {
150 async list(sharedContext: Context = {}) {}
151 }
152 `,
153 output: null,
154 errors: [{ messageId: "missingInjectManager" }],
155 },
156 // Honors aliased decorator import in the autofix.
157 {
158 code: `
159 import { MedusaService, InjectManager as IM } from "@medusajs/framework/utils"
160 class FooService extends MedusaService({}) {
161 async list(sharedContext: Context = {}) {}
162 }
163 `,
164 output: `
165 import { MedusaService, InjectManager as IM } from "@medusajs/framework/utils"
166 class FooService extends MedusaService({}) {
167 @IM()
168 async list(sharedContext: Context = {}) {}
169 }
170 `,
171 errors: [{ messageId: "missingInjectManager" }],
172 },
173 // Decorator inserted before existing unrelated decorator.
174 {
175 code: `
176 import { MedusaService, InjectManager, EmitEvents } from "@medusajs/framework/utils"
177 class FooService extends MedusaService({}) {
178 @EmitEvents()
179 async list(sharedContext: Context = {}) {}
180 }
181 `,
182 output: `
183 import { MedusaService, InjectManager, EmitEvents } from "@medusajs/framework/utils"
184 class FooService extends MedusaService({}) {
185 @InjectManager()
186 @EmitEvents()
187 async list(sharedContext: Context = {}) {}
188 }
189 `,
190 errors: [{ messageId: "missingInjectManager" }],
191 },
192 // Multiple methods on the same class are each reported.
193 {
194 code: `
195 import { MedusaService, InjectManager, InjectTransactionManager } from "@medusajs/framework/utils"
196 class FooService extends MedusaService({}) {
197 async list(sharedContext: Context = {}) {}
198 protected async list_(sharedContext: Context = {}) {}
199 }
200 `,
201 output: `
202 import { MedusaService, InjectManager, InjectTransactionManager } from "@medusajs/framework/utils"
203 class FooService extends MedusaService({}) {
204 @InjectManager()
205 async list(sharedContext: Context = {}) {}
206 @InjectTransactionManager()
207 protected async list_(sharedContext: Context = {}) {}
208 }
209 `,
210 errors: [
211 { messageId: "missingInjectManager" },
212 { messageId: "missingInjectTransactionManager" },
213 ],
214 },
215 // Private method missing @InjectTransactionManager() is flagged like protected.
216 {
217 code: `
218 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
219 class FooService extends MedusaService({}) {
220 private async helper(sharedContext: Context = {}) {}
221 }
222 `,
223 output: `
224 import { MedusaService, InjectTransactionManager } from "@medusajs/framework/utils"
225 class FooService extends MedusaService({}) {
226 @InjectTransactionManager()
227 private async helper(sharedContext: Context = {}) {}
228 }
229 `,
230 errors: [{ messageId: "missingInjectTransactionManager" }],
231 },
232 ],
233 })