1 import { createRule } from "../../create-rule"
2 import {
3 createWorkflowSdkBindings,
4 isInWorkflowDefinitionScope,
5 trackWorkflowSdkImports,
6 } from "../../util/workflow-scope"
7
8 type MessageIds = "spreadInWorkflow" | "restInWorkflow"
9
10 export const rule = createRule<[], MessageIds>({
11 name: "no-spread-in-workflow",
12 meta: {
13 type: "problem",
14 docs: {
15 description:
16 "Disallow spread (`...`) and rest (`...`) elements directly inside a `createWorkflow` constructor or a `when().then()` callback. Wrap object/array manipulation in `transform(...)` so it runs at execution time.",
17 },
18 messages: {
19 spreadInWorkflow:
20 "Do not use the spread operator (`...`) inside a workflow constructor or `when().then()` callback — it evaluates at workflow-definition time, not execution time. Move the operation into a `transform(...)` callback or a `createStep` instead.",
21 restInWorkflow:
22 "Do not use rest destructuring (`...`) inside a workflow constructor or `when().then()` callback — it evaluates at workflow-definition time, not execution time. Move the operation into a `transform(...)` callback or a `createStep` instead.",
23 },
24 schema: [],
25 },
26 defaultOptions: [],
27 create(context) {
28 const bindings = createWorkflowSdkBindings()
29
30 return {
31 ImportDeclaration(node) {
32 trackWorkflowSdkImports(node, bindings)
33 },
34
35 SpreadElement(node) {
36 if (bindings.createWorkflow.size === 0) {
37 return
38 }
39 if (!isInWorkflowDefinitionScope(node, bindings)) {
40 return
41 }
42 context.report({
43 node,
44 messageId: "spreadInWorkflow",
45 })
46 },
47
48 RestElement(node) {
49 if (bindings.createWorkflow.size === 0) {
50 return
51 }
52 if (!isInWorkflowDefinitionScope(node, bindings)) {
53 return
54 }
55 context.report({
56 node,
57 messageId: "restInWorkflow",
58 })
59 },
60 }
61 },
62 })
63
64 export default rule