|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
import { |
|
4
|
FunctionNode, |
|
5
|
isFunctionNode, |
|
6
|
resolveFunctionFromIdentifier, |
|
7
|
} from "../../util/ast" |
|
8
|
import { isIndexFile } from "../../util/filename" |
|
9
|
|
|
10
|
type MessageIds = "mustBeAsync" |
|
11
|
|
|
12
|
export const rule = createRule<[], MessageIds>({ |
|
13
|
name: "scheduled-job-default-export-async", |
|
14
|
meta: { |
|
15
|
type: "problem", |
|
16
|
docs: { |
|
17
|
description: "The default-exported scheduled job function must be async.", |
|
18
|
}, |
|
19
|
fixable: "code", |
|
20
|
messages: { |
|
21
|
mustBeAsync: |
|
22
|
"The default-exported scheduled job must be an async function. Scheduled jobs receive the Medusa container and are always awaited by the framework.", |
|
23
|
}, |
|
24
|
schema: [], |
|
25
|
}, |
|
26
|
defaultOptions: [], |
|
27
|
create(context) { |
|
28
|
// `index.<ext>` files in a jobs directory are barrels/re-exports, not job |
|
29
|
// definitions. |
|
30
|
if (isIndexFile(context.filename)) { |
|
31
|
return {} |
|
32
|
} |
|
33
|
|
|
34
|
const sourceCode = context.sourceCode ?? context.getSourceCode() |
|
35
|
|
|
36
|
function checkFunction(fn: FunctionNode) { |
|
37
|
if (fn.async) { |
|
38
|
return |
|
39
|
} |
|
40
|
context.report({ |
|
41
|
node: fn, |
|
42
|
messageId: "mustBeAsync", |
|
43
|
fix(fixer) { |
|
44
|
return fixer.insertTextBefore(fn, "async ") |
|
45
|
}, |
|
46
|
}) |
|
47
|
} |
|
48
|
|
|
49
|
return { |
|
50
|
ExportDefaultDeclaration(node: TSESTree.ExportDefaultDeclaration) { |
|
51
|
const decl = node.declaration |
|
52
|
|
|
53
|
// export default (async) function () {} | export default (async) () => {} |
|
54
|
if (isFunctionNode(decl)) { |
|
55
|
checkFunction(decl) |
|
56
|
return |
|
57
|
} |
|
58
|
|
|
59
|
// export default jobFn — resolve the binding to its function. |
|
60
|
if (decl.type === AST_NODE_TYPES.Identifier) { |
|
61
|
const fn = resolveFunctionFromIdentifier( |
|
62
|
sourceCode.getScope(node), |
|
63
|
decl |
|
64
|
) |
|
65
|
if (fn) { |
|
66
|
checkFunction(fn) |
|
67
|
} |
|
68
|
} |
|
69
|
}, |
|
70
|
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
|
71
|
// `export { default } from "./impl"` re-exports another module's |
|
72
|
// binding — we can't resolve it locally, so skip (no false positive). |
|
73
|
if (node.source) { |
|
74
|
return |
|
75
|
} |
|
76
|
|
|
77
|
for (const spec of node.specifiers) { |
|
78
|
if ( |
|
79
|
spec.exported.type !== AST_NODE_TYPES.Identifier || |
|
80
|
spec.exported.name !== "default" |
|
81
|
) { |
|
82
|
continue |
|
83
|
} |
|
84
|
|
|
85
|
// `export { job as default }` — resolve `job`. |
|
86
|
const fn = resolveFunctionFromIdentifier( |
|
87
|
sourceCode.getScope(node), |
|
88
|
spec.local |
|
89
|
) |
|
90
|
if (fn) { |
|
91
|
checkFunction(fn) |
|
92
|
} |
|
93
|
return |
|
94
|
} |
|
95
|
}, |
|
96
|
} |
|
97
|
}, |
|
98
|
}) |
|
99
|
|
|
100
|
export default rule |