|
1
|
import { createRule } from "../../create-rule" |
|
2
|
import { isNonLiteralRef } from "../../util/ast" |
|
3
|
import { |
|
4
|
createWorkflowSdkBindings, |
|
5
|
isInWorkflowDefinitionScope, |
|
6
|
trackWorkflowSdkImports, |
|
7
|
WorkflowSdkBindings, |
|
8
|
} from "../../util/workflow-scope" |
|
9
|
|
|
10
|
type MessageIds = "mutateInWorkflow" | "recomputeInWorkflow" |
|
11
|
|
|
12
|
const ARITHMETIC_OPERATORS = new Set(["+", "-", "*", "/", "%", "**"]) |
|
13
|
|
|
14
|
export const rule = createRule<[], MessageIds>({ |
|
15
|
name: "no-direct-variable-mutation-in-workflow", |
|
16
|
meta: { |
|
17
|
type: "problem", |
|
18
|
docs: { |
|
19
|
description: |
|
20
|
"Disallow mutating or recomputing variables directly inside a `createWorkflow` constructor or a `when().then()` callback. The constructor runs at workflow-definition time, so locals (step outputs, input, etc.) are placeholders, not real values — derive new values inside `transform` instead.", |
|
21
|
}, |
|
22
|
messages: { |
|
23
|
mutateInWorkflow: |
|
24
|
"Do not mutate variables inside a workflow constructor — values are placeholders at definition time, not real values. Use `transform` to derive a new value.", |
|
25
|
recomputeInWorkflow: |
|
26
|
"Do not recompute variables (arithmetic / template literals) inside a workflow constructor — values are placeholders at definition time. Move the computation into `transform`.", |
|
27
|
}, |
|
28
|
schema: [], |
|
29
|
}, |
|
30
|
defaultOptions: [], |
|
31
|
create(context) { |
|
32
|
const bindings: WorkflowSdkBindings = createWorkflowSdkBindings() |
|
33
|
|
|
34
|
return { |
|
35
|
ImportDeclaration(node) { |
|
36
|
trackWorkflowSdkImports(node, bindings) |
|
37
|
}, |
|
38
|
AssignmentExpression(node) { |
|
39
|
if (bindings.createWorkflow.size === 0) { |
|
40
|
return |
|
41
|
} |
|
42
|
if (!isInWorkflowDefinitionScope(node, bindings)) { |
|
43
|
return |
|
44
|
} |
|
45
|
context.report({ node, messageId: "mutateInWorkflow" }) |
|
46
|
}, |
|
47
|
UpdateExpression(node) { |
|
48
|
if (bindings.createWorkflow.size === 0) { |
|
49
|
return |
|
50
|
} |
|
51
|
if (!isInWorkflowDefinitionScope(node, bindings)) { |
|
52
|
return |
|
53
|
} |
|
54
|
context.report({ node, messageId: "mutateInWorkflow" }) |
|
55
|
}, |
|
56
|
BinaryExpression(node) { |
|
57
|
if (bindings.createWorkflow.size === 0) { |
|
58
|
return |
|
59
|
} |
|
60
|
if (!ARITHMETIC_OPERATORS.has(node.operator)) { |
|
61
|
return |
|
62
|
} |
|
63
|
if (!isNonLiteralRef(node.left) && !isNonLiteralRef(node.right)) { |
|
64
|
return |
|
65
|
} |
|
66
|
if (!isInWorkflowDefinitionScope(node, bindings)) { |
|
67
|
return |
|
68
|
} |
|
69
|
context.report({ node, messageId: "recomputeInWorkflow" }) |
|
70
|
}, |
|
71
|
TemplateLiteral(node) { |
|
72
|
if (bindings.createWorkflow.size === 0) { |
|
73
|
return |
|
74
|
} |
|
75
|
if (node.expressions.length === 0) { |
|
76
|
return |
|
77
|
} |
|
78
|
if (!node.expressions.some((expr) => isNonLiteralRef(expr))) { |
|
79
|
return |
|
80
|
} |
|
81
|
if (!isInWorkflowDefinitionScope(node, bindings)) { |
|
82
|
return |
|
83
|
} |
|
84
|
context.report({ node, messageId: "recomputeInWorkflow" }) |
|
85
|
}, |
|
86
|
} |
|
87
|
}, |
|
88
|
}) |
|
89
|
|
|
90
|
export default rule |