|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { parseExpression } from "cron-parser" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { |
|
5
|
findProperty, |
|
6
|
resolveObjectExpression, |
|
7
|
resolveStaticStringValue, |
|
8
|
} from "../../util/ast" |
|
9
|
import { isIndexFile } from "../../util/filename" |
|
10
|
|
|
11
|
type MessageIds = "invalidCron" |
|
12
|
|
|
13
|
/** |
|
14
|
* Returns true when `value` parses as a valid cron expression under the same |
|
15
|
* parser Medusa uses at runtime to schedule jobs (`cron-parser`'s |
|
16
|
* `parseExpression`). Invalid expressions throw; we treat any throw as invalid. |
|
17
|
*/ |
|
18
|
const isValidCron = (value: string): boolean => { |
|
19
|
try { |
|
20
|
parseExpression(value) |
|
21
|
return true |
|
22
|
} catch { |
|
23
|
return false |
|
24
|
} |
|
25
|
} |
|
26
|
|
|
27
|
export const rule = createRule<[], MessageIds>({ |
|
28
|
name: "scheduled-job-schedule-valid-cron", |
|
29
|
meta: { |
|
30
|
type: "problem", |
|
31
|
docs: { |
|
32
|
description: |
|
33
|
"A scheduled job `config.schedule` string must be a valid cron expression.", |
|
34
|
}, |
|
35
|
messages: { |
|
36
|
invalidCron: |
|
37
|
"`{{value}}` is not a valid cron expression. The scheduled job `config.schedule` must be a valid cron expression (e.g. `0 0 * * *`).", |
|
38
|
}, |
|
39
|
schema: [], |
|
40
|
}, |
|
41
|
defaultOptions: [], |
|
42
|
create(context) { |
|
43
|
// `index.<ext>` files in a jobs directory are barrels/re-exports, not job |
|
44
|
// definitions. |
|
45
|
if (isIndexFile(context.filename)) { |
|
46
|
return {} |
|
47
|
} |
|
48
|
|
|
49
|
const sourceCode = context.sourceCode ?? context.getSourceCode() |
|
50
|
|
|
51
|
// The resolved `config` object literal. Stays null when `config` is |
|
52
|
// exported but can't be resolved to an object (re-export, non-object |
|
53
|
// initializer) — nothing to validate then. |
|
54
|
let configObject: TSESTree.ObjectExpression | null = null |
|
55
|
|
|
56
|
const trackConfig = ( |
|
57
|
init: TSESTree.Node | null | undefined, |
|
58
|
node: TSESTree.Node |
|
59
|
) => { |
|
60
|
const resolved = resolveObjectExpression(init, sourceCode.getScope(node)) |
|
61
|
if (resolved) { |
|
62
|
configObject = resolved |
|
63
|
} |
|
64
|
} |
|
65
|
|
|
66
|
return { |
|
67
|
ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
|
68
|
// export const config = { ... } | export const config: ConfigType = { ... } |
|
69
|
if ( |
|
70
|
node.declaration && |
|
71
|
node.declaration.type === AST_NODE_TYPES.VariableDeclaration |
|
72
|
) { |
|
73
|
for (const decl of node.declaration.declarations) { |
|
74
|
if ( |
|
75
|
decl.id.type === AST_NODE_TYPES.Identifier && |
|
76
|
decl.id.name === "config" |
|
77
|
) { |
|
78
|
trackConfig(decl.init, node) |
|
79
|
} |
|
80
|
} |
|
81
|
} |
|
82
|
|
|
83
|
// export { config } | export { foo as config } |
|
84
|
for (const spec of node.specifiers) { |
|
85
|
if ( |
|
86
|
spec.exported.type !== AST_NODE_TYPES.Identifier || |
|
87
|
spec.exported.name !== "config" |
|
88
|
) { |
|
89
|
continue |
|
90
|
} |
|
91
|
// A re-export from another module can't be resolved locally. |
|
92
|
if (node.source) { |
|
93
|
continue |
|
94
|
} |
|
95
|
trackConfig(spec.local, node) |
|
96
|
} |
|
97
|
}, |
|
98
|
"Program:exit"() { |
|
99
|
if (!configObject) { |
|
100
|
return |
|
101
|
} |
|
102
|
|
|
103
|
const scheduleProp = findProperty(configObject, "schedule") |
|
104
|
if (!scheduleProp) { |
|
105
|
// Presence of `schedule` is enforced by `scheduled-job-config-required`. |
|
106
|
return |
|
107
|
} |
|
108
|
|
|
109
|
// Interval-based schedules (`schedule: { interval: 60000 }`) aren't cron |
|
110
|
// strings — only validate when the value resolves to a string. |
|
111
|
const resolved = resolveStaticStringValue( |
|
112
|
scheduleProp.value, |
|
113
|
sourceCode.getScope(scheduleProp.value) |
|
114
|
) |
|
115
|
if (!resolved) { |
|
116
|
return |
|
117
|
} |
|
118
|
|
|
119
|
if (!isValidCron(resolved.value)) { |
|
120
|
context.report({ |
|
121
|
node: resolved.reportNode, |
|
122
|
messageId: "invalidCron", |
|
123
|
data: { value: resolved.value }, |
|
124
|
}) |
|
125
|
} |
|
126
|
}, |
|
127
|
} |
|
128
|
}, |
|
129
|
}) |
|
130
|
|
|
131
|
export default rule |