1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import {
4 createWorkflowSdkBindings,
5 isInWorkflowDefinitionScope,
6 trackWorkflowSdkImports,
7 WorkflowSdkBindings,
8 } from "../../util/workflow-scope"
9
10 type MessageIds = "loopInWorkflow"
11
12 type LoopNode =
13 | TSESTree.ForStatement
14 | TSESTree.ForInStatement
15 | TSESTree.ForOfStatement
16 | TSESTree.WhileStatement
17 | TSESTree.DoWhileStatement
18
19 export const rule = createRule<[], MessageIds>({
20 name: "no-loops-in-workflow",
21 meta: {
22 type: "problem",
23 docs: {
24 description:
25 "Disallow `for`, `for-in`, `for-of`, `while`, and `do-while` loops directly inside a `createWorkflow` constructor or a `when().then()` callback. Use `transform` for input prep, or loop outside the workflow (around `.run()`).",
26 },
27 messages: {
28 loopInWorkflow:
29 "Do not use loops inside a workflow constructor or a `when().then()` callback — they run at definition time, not execution time. Use `transform` for input preparation, or loop outside the workflow around `.run()`.",
30 },
31 schema: [],
32 },
33 defaultOptions: [],
34 create(context) {
35 const bindings: WorkflowSdkBindings = createWorkflowSdkBindings()
36
37 const check = (node: LoopNode) => {
38 if (bindings.createWorkflow.size === 0) {
39 return
40 }
41 if (!isInWorkflowDefinitionScope(node, bindings)) {
42 return
43 }
44 context.report({ node, messageId: "loopInWorkflow" })
45 }
46
47 return {
48 ImportDeclaration(node) {
49 trackWorkflowSdkImports(node, bindings)
50 },
51 ForStatement: check,
52 ForInStatement: check,
53 ForOfStatement: check,
54 WhileStatement: check,
55 DoWhileStatement: check,
56 }
57 },
58 })
59
60 export default rule