|
1
|
import { createRuleTester } from "../../../test-utils" |
|
2
|
import { rule } from "../rule" |
|
3
|
|
|
4
|
const ruleTester = createRuleTester() |
|
5
|
|
|
6
|
ruleTester.run("service-methods-must-be-async", rule, { |
|
7
|
valid: [ |
|
8
|
// All methods async. |
|
9
|
{ |
|
10
|
code: ` |
|
11
|
import { MedusaService } from "@medusajs/framework/utils" |
|
12
|
class FooService extends MedusaService({}) { |
|
13
|
async create() {} |
|
14
|
async update() {} |
|
15
|
} |
|
16
|
`, |
|
17
|
}, |
|
18
|
// Method with Promise return type annotation but no async (e.g. delegates to another async fn). |
|
19
|
{ |
|
20
|
code: ` |
|
21
|
import { MedusaService } from "@medusajs/framework/utils" |
|
22
|
class FooService extends MedusaService({}) { |
|
23
|
fetch(): Promise<void> { |
|
24
|
return Promise.resolve() |
|
25
|
} |
|
26
|
} |
|
27
|
`, |
|
28
|
}, |
|
29
|
// Constructor is not flagged. |
|
30
|
{ |
|
31
|
code: ` |
|
32
|
import { MedusaService } from "@medusajs/framework/utils" |
|
33
|
class FooService extends MedusaService({}) { |
|
34
|
constructor() { |
|
35
|
super(...arguments) |
|
36
|
} |
|
37
|
async create() {} |
|
38
|
} |
|
39
|
`, |
|
40
|
}, |
|
41
|
// Private/protected methods are not flagged. |
|
42
|
{ |
|
43
|
code: ` |
|
44
|
import { MedusaService } from "@medusajs/framework/utils" |
|
45
|
class FooService extends MedusaService({}) { |
|
46
|
private helper() {} |
|
47
|
protected internal() {} |
|
48
|
async publicOne() {} |
|
49
|
} |
|
50
|
`, |
|
51
|
}, |
|
52
|
// Static method that's already async is fine. |
|
53
|
{ |
|
54
|
code: ` |
|
55
|
import { MedusaService } from "@medusajs/framework/utils" |
|
56
|
class FooService extends MedusaService({}) { |
|
57
|
static async helper() {} |
|
58
|
async run() {} |
|
59
|
} |
|
60
|
`, |
|
61
|
}, |
|
62
|
// Getters and setters are exempt — they can't be async and are accessed as |
|
63
|
// properties, not invoked like service methods. |
|
64
|
{ |
|
65
|
code: ` |
|
66
|
import { MedusaService } from "@medusajs/framework/utils" |
|
67
|
class FooService extends MedusaService({}) { |
|
68
|
get name() { return "foo" } |
|
69
|
set name(v: string) {} |
|
70
|
get model() { return this.model_ } |
|
71
|
async run() {} |
|
72
|
} |
|
73
|
`, |
|
74
|
}, |
|
75
|
// Class whose name doesn't end in "Service" and doesn't extend MedusaService is ignored. |
|
76
|
{ |
|
77
|
code: ` |
|
78
|
class Plain { |
|
79
|
create() {} |
|
80
|
} |
|
81
|
`, |
|
82
|
}, |
|
83
|
// A `*Service`-named class that doesn't extend `MedusaService` is NOT |
|
84
|
// treated as a service — its sync methods are allowed, regardless of file |
|
85
|
// location. (Fixes false positives on helper classes like |
|
86
|
// `EntityDiscoveryService`.) |
|
87
|
{ |
|
88
|
filename: "/repo/packages/modules/settings/src/utils/entity-discovery.ts", |
|
89
|
code: ` |
|
90
|
export class EntityDiscoveryService { |
|
91
|
discover() {} |
|
92
|
} |
|
93
|
`, |
|
94
|
}, |
|
95
|
// A plain class in a module's service location that doesn't extend |
|
96
|
// `MedusaService` is not checked, even when its methods are sync. |
|
97
|
{ |
|
98
|
filename: "/repo/packages/modules/order/src/services/order.ts", |
|
99
|
code: ` |
|
100
|
class OrderHelper { |
|
101
|
create() {} |
|
102
|
} |
|
103
|
`, |
|
104
|
}, |
|
105
|
{ |
|
106
|
filename: "/repo/packages/modules/order/src/service.ts", |
|
107
|
code: ` |
|
108
|
class OrderModuleService { |
|
109
|
create() {} |
|
110
|
} |
|
111
|
`, |
|
112
|
}, |
|
113
|
// MedusaService imported from a non-framework source is ignored (and class isn't named *Service). |
|
114
|
{ |
|
115
|
code: ` |
|
116
|
import { MedusaService } from "some-other-lib" |
|
117
|
class Foo extends MedusaService({}) { |
|
118
|
create() {} |
|
119
|
} |
|
120
|
`, |
|
121
|
}, |
|
122
|
// Class extends something other than MedusaService, and isn't named *Service. |
|
123
|
{ |
|
124
|
code: ` |
|
125
|
import { MedusaService } from "@medusajs/framework/utils" |
|
126
|
class Foo extends SomethingElse { |
|
127
|
create() {} |
|
128
|
} |
|
129
|
`, |
|
130
|
}, |
|
131
|
// Honors aliased MedusaService import. |
|
132
|
{ |
|
133
|
code: ` |
|
134
|
import { MedusaService as MS } from "@medusajs/framework/utils" |
|
135
|
class FooService extends MS({}) { |
|
136
|
async create() {} |
|
137
|
} |
|
138
|
`, |
|
139
|
}, |
|
140
|
], |
|
141
|
invalid: [ |
|
142
|
{ |
|
143
|
code: ` |
|
144
|
import { MedusaService } from "@medusajs/framework/utils" |
|
145
|
class FooService extends MedusaService({}) { |
|
146
|
create() {} |
|
147
|
} |
|
148
|
`, |
|
149
|
output: ` |
|
150
|
import { MedusaService } from "@medusajs/framework/utils" |
|
151
|
class FooService extends MedusaService({}) { |
|
152
|
async create() {} |
|
153
|
} |
|
154
|
`, |
|
155
|
errors: [{ messageId: "methodMustBeAsync" }], |
|
156
|
}, |
|
157
|
// Multiple non-async methods. |
|
158
|
{ |
|
159
|
code: ` |
|
160
|
import { MedusaService } from "@medusajs/framework/utils" |
|
161
|
class FooService extends MedusaService({}) { |
|
162
|
create() {} |
|
163
|
update() {} |
|
164
|
async remove() {} |
|
165
|
} |
|
166
|
`, |
|
167
|
output: ` |
|
168
|
import { MedusaService } from "@medusajs/framework/utils" |
|
169
|
class FooService extends MedusaService({}) { |
|
170
|
async create() {} |
|
171
|
async update() {} |
|
172
|
async remove() {} |
|
173
|
} |
|
174
|
`, |
|
175
|
errors: [ |
|
176
|
{ messageId: "methodMustBeAsync" }, |
|
177
|
{ messageId: "methodMustBeAsync" }, |
|
178
|
], |
|
179
|
}, |
|
180
|
// Non-Promise return type annotation. |
|
181
|
{ |
|
182
|
code: ` |
|
183
|
import { MedusaService } from "@medusajs/framework/utils" |
|
184
|
class FooService extends MedusaService({}) { |
|
185
|
fetch(): void {} |
|
186
|
} |
|
187
|
`, |
|
188
|
output: ` |
|
189
|
import { MedusaService } from "@medusajs/framework/utils" |
|
190
|
class FooService extends MedusaService({}) { |
|
191
|
async fetch(): void {} |
|
192
|
} |
|
193
|
`, |
|
194
|
errors: [{ messageId: "methodMustBeAsync" }], |
|
195
|
}, |
|
196
|
// Aliased MedusaService import. |
|
197
|
{ |
|
198
|
code: ` |
|
199
|
import { MedusaService as MS } from "@medusajs/framework/utils" |
|
200
|
class FooService extends MS({}) { |
|
201
|
create() {} |
|
202
|
} |
|
203
|
`, |
|
204
|
output: ` |
|
205
|
import { MedusaService as MS } from "@medusajs/framework/utils" |
|
206
|
class FooService extends MS({}) { |
|
207
|
async create() {} |
|
208
|
} |
|
209
|
`, |
|
210
|
errors: [{ messageId: "methodMustBeAsync" }], |
|
211
|
}, |
|
212
|
// Class expression. |
|
213
|
{ |
|
214
|
code: ` |
|
215
|
import { MedusaService } from "@medusajs/framework/utils" |
|
216
|
const FooService = class extends MedusaService({}) { |
|
217
|
create() {} |
|
218
|
} |
|
219
|
`, |
|
220
|
output: ` |
|
221
|
import { MedusaService } from "@medusajs/framework/utils" |
|
222
|
const FooService = class extends MedusaService({}) { |
|
223
|
async create() {} |
|
224
|
} |
|
225
|
`, |
|
226
|
errors: [{ messageId: "methodMustBeAsync" }], |
|
227
|
}, |
|
228
|
// Static methods are invocable from outside the service, so they must be async too. |
|
229
|
{ |
|
230
|
code: ` |
|
231
|
import { MedusaService } from "@medusajs/framework/utils" |
|
232
|
class FooService extends MedusaService({}) { |
|
233
|
static helper() {} |
|
234
|
} |
|
235
|
`, |
|
236
|
output: ` |
|
237
|
import { MedusaService } from "@medusajs/framework/utils" |
|
238
|
class FooService extends MedusaService({}) { |
|
239
|
static async helper() {} |
|
240
|
} |
|
241
|
`, |
|
242
|
errors: [{ messageId: "methodMustBeAsync" }], |
|
243
|
}, |
|
244
|
], |
|
245
|
}) |