|
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 { returnTypeIsPromise } from "../../util/ast" |
|
5
|
import { |
|
6
|
createMedusaServiceBindings, |
|
7
|
isMedusaServiceSuper, |
|
8
|
trackMedusaServiceImports, |
|
9
|
} from "../../util/service-scope" |
|
10
|
|
|
11
|
type MessageIds = "methodMustBeAsync" |
|
12
|
|
|
13
|
export const rule = createRule<[], MessageIds>({ |
|
14
|
name: "service-methods-must-be-async", |
|
15
|
meta: { |
|
16
|
type: "problem", |
|
17
|
docs: { |
|
18
|
description: |
|
19
|
"All public methods on a service class that extends `MedusaService(...)` must be `async` (or return a `Promise`). Medusa always awaits service method calls.", |
|
20
|
}, |
|
21
|
fixable: "code", |
|
22
|
messages: { |
|
23
|
methodMustBeAsync: |
|
24
|
"Public methods on a service class must be `async` (or return a `Promise`). Add `async` or annotate the method with a `Promise<...>` return type.", |
|
25
|
}, |
|
26
|
schema: [], |
|
27
|
}, |
|
28
|
defaultOptions: [], |
|
29
|
create(context) { |
|
30
|
const bindings = createMedusaServiceBindings() |
|
31
|
|
|
32
|
function checkClass( |
|
33
|
node: TSESTree.ClassDeclaration | TSESTree.ClassExpression |
|
34
|
) { |
|
35
|
// Only check classes that extend `MedusaService(...)`. Avoids false |
|
36
|
// positives on unrelated classes (including `*Service`-named helpers) |
|
37
|
// that don't expose awaitable service methods. |
|
38
|
if (!isMedusaServiceSuper(node.superClass, bindings)) { |
|
39
|
return |
|
40
|
} |
|
41
|
|
|
42
|
for (const member of node.body.body) { |
|
43
|
if (member.type !== AST_NODE_TYPES.MethodDefinition) { |
|
44
|
continue |
|
45
|
} |
|
46
|
if (member.kind === "constructor") { |
|
47
|
continue |
|
48
|
} |
|
49
|
// Getters and setters can't be `async` and aren't invoked like service |
|
50
|
// methods (the caller reads/writes a property), so they're exempt. |
|
51
|
if (member.kind === "get" || member.kind === "set") { |
|
52
|
continue |
|
53
|
} |
|
54
|
if ( |
|
55
|
member.accessibility === "private" || |
|
56
|
member.accessibility === "protected" |
|
57
|
) { |
|
58
|
continue |
|
59
|
} |
|
60
|
if (member.computed) { |
|
61
|
continue |
|
62
|
} |
|
63
|
|
|
64
|
const value = member.value |
|
65
|
if ( |
|
66
|
value.type !== AST_NODE_TYPES.FunctionExpression && |
|
67
|
value.type !== AST_NODE_TYPES.TSEmptyBodyFunctionExpression |
|
68
|
) { |
|
69
|
continue |
|
70
|
} |
|
71
|
|
|
72
|
if ((value as TSESTree.FunctionExpression).async) { |
|
73
|
continue |
|
74
|
} |
|
75
|
if (returnTypeIsPromise(value)) { |
|
76
|
continue |
|
77
|
} |
|
78
|
|
|
79
|
context.report({ |
|
80
|
node: member.key, |
|
81
|
messageId: "methodMustBeAsync", |
|
82
|
fix: (fixer) => fixer.insertTextBefore(member.key, "async "), |
|
83
|
}) |
|
84
|
} |
|
85
|
} |
|
86
|
|
|
87
|
return { |
|
88
|
ImportDeclaration(node) { |
|
89
|
trackMedusaServiceImports(node, bindings) |
|
90
|
}, |
|
91
|
|
|
92
|
ClassDeclaration: checkClass, |
|
93
|
ClassExpression: checkClass, |
|
94
|
} |
|
95
|
}, |
|
96
|
}) |
|
97
|
|
|
98
|
export default rule |