|
1
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
import { getInitializedVariableName } from "../../util/ast" |
|
4
|
import { getFilenameStem } from "../../util/filename" |
|
5
|
import { KEBAB_CASE_RE, toKebab } from "../../util/strings" |
|
6
|
import { |
|
7
|
createWorkflowSdkBindings, |
|
8
|
trackWorkflowSdkImports, |
|
9
|
} from "../../util/workflow-scope" |
|
10
|
|
|
11
|
type MessageIds = "notKebabCase" | "idMismatch" |
|
12
|
|
|
13
|
/** |
|
14
|
* Strips a trailing `step` segment from a kebab string. `fetch-customers-step` |
|
15
|
* → `fetch-customers`. A bare `step` (no preceding segment) is left untouched — |
|
16
|
* stripping would yield an empty id. |
|
17
|
*/ |
|
18
|
function stripStepSuffix(kebab: string): string { |
|
19
|
const stripped = kebab.replace(/-step$/, "") |
|
20
|
return stripped.length > 0 ? stripped : kebab |
|
21
|
} |
|
22
|
|
|
23
|
export const rule = createRule<[], MessageIds>({ |
|
24
|
name: "step-id-kebab-case", |
|
25
|
meta: { |
|
26
|
type: "suggestion", |
|
27
|
docs: { |
|
28
|
description: |
|
29
|
"Step id (first argument of `createStep`) must be a kebab-case string literal and should match the enclosing variable name or filename (trailing `Step` segment stripped).", |
|
30
|
}, |
|
31
|
fixable: "code", |
|
32
|
messages: { |
|
33
|
notKebabCase: |
|
34
|
"Step id `{{id}}` must be kebab-case (lowercase letters, digits, and hyphens; starting with a letter).", |
|
35
|
idMismatch: |
|
36
|
"Step id `{{id}}` should match the enclosing variable name or filename (expected `{{expected}}`).", |
|
37
|
}, |
|
38
|
schema: [], |
|
39
|
}, |
|
40
|
defaultOptions: [], |
|
41
|
create(context) { |
|
42
|
const bindings = createWorkflowSdkBindings() |
|
43
|
|
|
44
|
return { |
|
45
|
ImportDeclaration(node) { |
|
46
|
trackWorkflowSdkImports(node, bindings) |
|
47
|
}, |
|
48
|
|
|
49
|
CallExpression(node) { |
|
50
|
if (bindings.createStep.size === 0) { |
|
51
|
return |
|
52
|
} |
|
53
|
if (node.callee.type !== AST_NODE_TYPES.Identifier) { |
|
54
|
return |
|
55
|
} |
|
56
|
if (!bindings.createStep.has(node.callee.name)) { |
|
57
|
return |
|
58
|
} |
|
59
|
|
|
60
|
const idArg = node.arguments[0] |
|
61
|
if (!idArg) { |
|
62
|
return |
|
63
|
} |
|
64
|
if (idArg.type !== AST_NODE_TYPES.Literal) { |
|
65
|
return |
|
66
|
} |
|
67
|
if (typeof idArg.value !== "string") { |
|
68
|
return |
|
69
|
} |
|
70
|
|
|
71
|
const id = idArg.value |
|
72
|
const quote = context.sourceCode.getText(idArg).charAt(0) |
|
73
|
const isKebab = KEBAB_CASE_RE.test(id) |
|
74
|
|
|
75
|
const varName = getInitializedVariableName(node) |
|
76
|
const fileStem = getFilenameStem(context.filename) |
|
77
|
|
|
78
|
const expected = |
|
79
|
(varName ? stripStepSuffix(toKebab(varName)) : null) ?? |
|
80
|
(fileStem ? stripStepSuffix(toKebab(fileStem)) : null) |
|
81
|
|
|
82
|
if (!isKebab) { |
|
83
|
context.report({ |
|
84
|
node: idArg, |
|
85
|
messageId: "notKebabCase", |
|
86
|
data: { id }, |
|
87
|
fix(fixer) { |
|
88
|
const replacement = expected ?? stripStepSuffix(toKebab(id)) |
|
89
|
if (!KEBAB_CASE_RE.test(replacement)) { |
|
90
|
return null |
|
91
|
} |
|
92
|
return fixer.replaceText(idArg, `${quote}${replacement}${quote}`) |
|
93
|
}, |
|
94
|
}) |
|
95
|
return |
|
96
|
} |
|
97
|
|
|
98
|
if (expected && expected !== id && KEBAB_CASE_RE.test(expected)) { |
|
99
|
context.report({ |
|
100
|
node: idArg, |
|
101
|
messageId: "idMismatch", |
|
102
|
data: { id, expected }, |
|
103
|
fix(fixer) { |
|
104
|
return fixer.replaceText(idArg, `${quote}${expected}${quote}`) |
|
105
|
}, |
|
106
|
}) |
|
107
|
} |
|
108
|
}, |
|
109
|
} |
|
110
|
}, |
|
111
|
}) |
|
112
|
|
|
113
|
export default rule |