|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
import { objectHasProperty, resolveObjectExpression } from "../../util/ast" |
|
4
|
import { isIndexFile } from "../../util/filename" |
|
5
|
|
|
6
|
type MessageIds = |
|
7
|
| "missingConfigExport" |
|
8
|
| "missingNameProperty" |
|
9
|
| "missingScheduleProperty" |
|
10
|
|
|
11
|
export const rule = createRule<[], MessageIds>({ |
|
12
|
name: "scheduled-job-config-required", |
|
13
|
meta: { |
|
14
|
type: "problem", |
|
15
|
docs: { |
|
16
|
description: |
|
17
|
"Scheduled job files must export a `config` object with `name` and `schedule` properties.", |
|
18
|
}, |
|
19
|
messages: { |
|
20
|
missingConfigExport: |
|
21
|
"Scheduled job files must have a named export `config` with both a `name` and a `schedule` property.", |
|
22
|
missingNameProperty: |
|
23
|
"The scheduled job `config` export must declare a `name` property (a unique identifier for the job).", |
|
24
|
missingScheduleProperty: |
|
25
|
"The scheduled job `config` export must declare a `schedule` property (a cron expression string, or an object with an `interval` in milliseconds).", |
|
26
|
}, |
|
27
|
schema: [], |
|
28
|
}, |
|
29
|
defaultOptions: [], |
|
30
|
create(context) { |
|
31
|
// `index.<ext>` files in a jobs directory are barrels/re-exports, not job |
|
32
|
// definitions — they don't declare a `config`, so skip them. |
|
33
|
if (isIndexFile(context.filename)) { |
|
34
|
return {} |
|
35
|
} |
|
36
|
|
|
37
|
const sourceCode = context.sourceCode ?? context.getSourceCode() |
|
38
|
|
|
39
|
let hasConfigExport = false |
|
40
|
// The resolved config object literal, used for the property checks. Stays |
|
41
|
// null when `config` is exported but can't be resolved to an object (e.g. |
|
42
|
// a re-export or a non-object initializer) — the property checks are |
|
43
|
// skipped. |
|
44
|
let configObject: TSESTree.ObjectExpression | null = null |
|
45
|
|
|
46
|
return { |
|
47
|
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
|
48
|
// export const config = { ... } | export const config: ConfigType = { ... } |
|
49
|
if ( |
|
50
|
node.declaration && |
|
51
|
node.declaration.type === AST_NODE_TYPES.VariableDeclaration |
|
52
|
) { |
|
53
|
for (const decl of node.declaration.declarations) { |
|
54
|
if ( |
|
55
|
decl.id.type === AST_NODE_TYPES.Identifier && |
|
56
|
decl.id.name === "config" |
|
57
|
) { |
|
58
|
hasConfigExport = true |
|
59
|
const resolved = resolveObjectExpression( |
|
60
|
decl.init, |
|
61
|
sourceCode.getScope(node) |
|
62
|
) |
|
63
|
if (resolved) { |
|
64
|
configObject = resolved |
|
65
|
} |
|
66
|
} |
|
67
|
} |
|
68
|
} |
|
69
|
|
|
70
|
// export { config } | export { foo as config } | export { config } from "./x" |
|
71
|
for (const spec of node.specifiers) { |
|
72
|
if ( |
|
73
|
spec.exported.type !== AST_NODE_TYPES.Identifier || |
|
74
|
spec.exported.name !== "config" |
|
75
|
) { |
|
76
|
continue |
|
77
|
} |
|
78
|
|
|
79
|
hasConfigExport = true |
|
80
|
// A re-export from another module can't be resolved locally — skip |
|
81
|
// the property checks (no false positive). |
|
82
|
if (node.source) { |
|
83
|
continue |
|
84
|
} |
|
85
|
|
|
86
|
const resolved = resolveObjectExpression( |
|
87
|
spec.local, |
|
88
|
sourceCode.getScope(node) |
|
89
|
) |
|
90
|
if (resolved) { |
|
91
|
configObject = resolved |
|
92
|
} |
|
93
|
} |
|
94
|
}, |
|
95
|
"Program:exit"(node: TSESTree.Program) { |
|
96
|
if (!hasConfigExport) { |
|
97
|
context.report({ node, messageId: "missingConfigExport" }) |
|
98
|
return |
|
99
|
} |
|
100
|
|
|
101
|
if (!configObject) { |
|
102
|
return |
|
103
|
} |
|
104
|
|
|
105
|
if (objectHasProperty(configObject, "name") === false) { |
|
106
|
context.report({ |
|
107
|
node: configObject, |
|
108
|
messageId: "missingNameProperty", |
|
109
|
}) |
|
110
|
} |
|
111
|
|
|
112
|
if (objectHasProperty(configObject, "schedule") === false) { |
|
113
|
context.report({ |
|
114
|
node: configObject, |
|
115
|
messageId: "missingScheduleProperty", |
|
116
|
}) |
|
117
|
} |
|
118
|
}, |
|
119
|
} |
|
120
|
}, |
|
121
|
}) |
|
122
|
|
|
123
|
export default rule |