1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { AST_NODE_TYPES } from "@typescript-eslint/utils"
3 import { WORKFLOWS_SDK_SOURCE } from "../constants"
4
5 export { WORKFLOWS_SDK_SOURCE }
6
7 export const CREATE_WORKFLOW = "createWorkflow"
8 export const CREATE_STEP = "createStep"
9 export const TRANSFORM = "transform"
10 export const WHEN = "when"
11 export const WORKFLOW_RESPONSE = "WorkflowResponse"
12 export const STEP_RESPONSE = "StepResponse"
13
14 export type WorkflowSdkBindings = {
15 createWorkflow: Set<string>
16 createStep: Set<string>
17 transform: Set<string>
18 when: Set<string>
19 workflowResponse: Set<string>
20 stepResponse: Set<string>
21 }
22
23 export function createWorkflowSdkBindings(): WorkflowSdkBindings {
24 return {
25 createWorkflow: new Set(),
26 createStep: new Set(),
27 transform: new Set(),
28 when: new Set(),
29 workflowResponse: new Set(),
30 stepResponse: new Set(),
31 }
32 }
33
34 const TRACKED_IMPORTS = [
35 CREATE_WORKFLOW,
36 CREATE_STEP,
37 TRANSFORM,
38 WHEN,
39 WORKFLOW_RESPONSE,
40 STEP_RESPONSE,
41 ] as const
42
43 type TrackedImport = (typeof TRACKED_IMPORTS)[number]
44
45 const BUCKET_BY_IMPORT: Record<TrackedImport, keyof WorkflowSdkBindings> = {
46 [CREATE_WORKFLOW]: "createWorkflow",
47 [CREATE_STEP]: "createStep",
48 [TRANSFORM]: "transform",
49 [WHEN]: "when",
50 [WORKFLOW_RESPONSE]: "workflowResponse",
51 [STEP_RESPONSE]: "stepResponse",
52 }
53
54 export function trackWorkflowSdkImports(
55 node: TSESTree.ImportDeclaration,
56 bindings: WorkflowSdkBindings
57 ): void {
58 if (node.source.value !== WORKFLOWS_SDK_SOURCE) {
59 return
60 }
61 for (const specifier of node.specifiers) {
62 if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) {
63 continue
64 }
65 if (specifier.imported.type !== AST_NODE_TYPES.Identifier) {
66 continue
67 }
68 const imported = specifier.imported.name as TrackedImport
69 if (!(imported in BUCKET_BY_IMPORT)) {
70 continue
71 }
72 bindings[BUCKET_BY_IMPORT[imported]].add(specifier.local.name)
73 }
74 }
75
76 export type FunctionLike =
77 | TSESTree.ArrowFunctionExpression
78 | TSESTree.FunctionExpression
79 | TSESTree.FunctionDeclaration
80
81 export function getEnclosingFunction(node: TSESTree.Node): FunctionLike | null {
82 let current: TSESTree.Node | undefined = node.parent
83 while (current) {
84 if (
85 current.type === AST_NODE_TYPES.ArrowFunctionExpression ||
86 current.type === AST_NODE_TYPES.FunctionExpression ||
87 current.type === AST_NODE_TYPES.FunctionDeclaration
88 ) {
89 return current
90 }
91 current = current.parent
92 }
93 return null
94 }
95
96 const CALLABLE_SDK_BUCKETS: (keyof WorkflowSdkBindings)[] = [
97 "createWorkflow",
98 "createStep",
99 "transform",
100 "when",
101 ]
102
103 /**
104 * Returns true when `calleeName` resolves to any tracked workflows-sdk
105 * function binding (`createWorkflow`, `createStep`, `transform`, `when`) —
106 * as opposed to `StepResponse`/`WorkflowResponse`, which are only ever used
107 * via `new`, never called directly.
108 */
109 export function isWorkflowSdkHelperCall(
110 calleeName: string,
111 bindings: WorkflowSdkBindings
112 ): boolean {
113 return CALLABLE_SDK_BUCKETS.some((bucket) => bindings[bucket].has(calleeName))
114 }
115
116 /**
117 * Returns true when `fn` is the second argument to a `createWorkflow(...)`
118 * call whose callee resolves to a tracked import binding.
119 */
120 export function isWorkflowConstructorFunction(
121 fn: FunctionLike,
122 bindings: WorkflowSdkBindings
123 ): boolean {
124 const parent = fn.parent
125 if (!parent || parent.type !== AST_NODE_TYPES.CallExpression) {
126 return false
127 }
128 if (parent.arguments[1] !== fn) {
129 return false
130 }
131 if (parent.callee.type !== AST_NODE_TYPES.Identifier) {
132 return false
133 }
134 return bindings.createWorkflow.has(parent.callee.name)
135 }
136
137 /**
138 * Returns true when `fn` is the first argument to a `.then(...)` call whose
139 * receiver is a `when(...)` call against a tracked `when` import binding.
140 *
141 * The `.then(...)` callback runs at workflow-definition time, same as the
142 * workflow constructor itself — anything you can't do in the constructor,
143 * you also can't do here.
144 */
145 export function isWhenThenCallbackFunction(
146 fn: FunctionLike,
147 bindings: WorkflowSdkBindings
148 ): boolean {
149 const parent = fn.parent
150 if (!parent || parent.type !== AST_NODE_TYPES.CallExpression) {
151 return false
152 }
153 if (parent.arguments[0] !== fn) {
154 return false
155 }
156 const callee = parent.callee
157 if (callee.type !== AST_NODE_TYPES.MemberExpression) {
158 return false
159 }
160 if (callee.property.type !== AST_NODE_TYPES.Identifier) {
161 return false
162 }
163 if (callee.property.name !== "then") {
164 return false
165 }
166 const receiver = callee.object
167 if (receiver.type !== AST_NODE_TYPES.CallExpression) {
168 return false
169 }
170 if (receiver.callee.type !== AST_NODE_TYPES.Identifier) {
171 return false
172 }
173 return bindings.when.has(receiver.callee.name)
174 }
175
176 /**
177 * Returns true when `fn` is a callback argument to a `transform(...)` call
178 * whose callee resolves to a tracked `transform` import binding.
179 *
180 * `transform(data, ...callbacks)` accepts one or more callback arguments after
181 * the data argument at index 0 — any of them counts.
182 */
183 export function isTransformCallbackFunction(
184 fn: FunctionLike,
185 bindings: WorkflowSdkBindings
186 ): boolean {
187 const parent = fn.parent
188 if (!parent || parent.type !== AST_NODE_TYPES.CallExpression) {
189 return false
190 }
191 const argIndex = parent.arguments.indexOf(
192 fn as TSESTree.CallExpressionArgument
193 )
194 if (argIndex < 1) {
195 return false
196 }
197 if (parent.callee.type !== AST_NODE_TYPES.Identifier) {
198 return false
199 }
200 return bindings.transform.has(parent.callee.name)
201 }
202
203 /**
204 * Returns true when `fn` is the main callback (arg index 1) of a
205 * `createStep(...)` call whose callee resolves to a tracked `createStep`
206 * import binding.
207 *
208 * `createStep(id, mainFn, compensationFn?)` — only the main fn is checked.
209 * Compensation callbacks are not required to return anything, so they are
210 * intentionally excluded.
211 */
212 export function isStepCallbackFunction(
213 fn: FunctionLike,
214 bindings: WorkflowSdkBindings
215 ): boolean {
216 const parent = fn.parent
217 if (!parent || parent.type !== AST_NODE_TYPES.CallExpression) {
218 return false
219 }
220 if (parent.arguments[1] !== fn) {
221 return false
222 }
223 if (parent.callee.type !== AST_NODE_TYPES.Identifier) {
224 return false
225 }
226 return bindings.createStep.has(parent.callee.name)
227 }
228
229 /**
230 * Returns true when `node` is a `new StepResponse(...)` or
231 * `new WorkflowResponse(...)` whose callee identifier resolves to a tracked
232 * workflows-sdk import binding.
233 */
234 export function isResponseConstructor(
235 node: TSESTree.NewExpression,
236 bindings: WorkflowSdkBindings
237 ): boolean {
238 if (node.callee.type !== AST_NODE_TYPES.Identifier) {
239 return false
240 }
241 return (
242 bindings.stepResponse.has(node.callee.name) ||
243 bindings.workflowResponse.has(node.callee.name)
244 )
245 }
246
247 /**
248 * True when `node` lives directly inside a `transform(...)` callback — not
249 * inside a further-nested function.
250 */
251 export function isInTransformCallback(
252 node: TSESTree.Node,
253 bindings: WorkflowSdkBindings
254 ): boolean {
255 const fn = getEnclosingFunction(node)
256 if (!fn) {
257 return false
258 }
259 return isTransformCallbackFunction(fn, bindings)
260 }
261
262 /**
263 * True when `node` lives directly inside a workflow constructor body — not
264 * inside a nested `createStep` / `transform` callback.
265 *
266 * Implementation: the immediately-enclosing function must itself be the
267 * workflow constructor. Nested step/transform callbacks become the
268 * immediately-enclosing function for code inside them, so this check
269 * naturally excludes them.
270 */
271 export function isInWorkflowConstructor(
272 node: TSESTree.Node,
273 bindings: WorkflowSdkBindings
274 ): boolean {
275 const fn = getEnclosingFunction(node)
276 if (!fn) {
277 return false
278 }
279 return isWorkflowConstructorFunction(fn, bindings)
280 }
281
282 /**
283 * True when `node` lives inside the *body* of a function that runs at
284 * workflow-*definition* time — either the workflow constructor itself, or
285 * a `when(...).then(callback)` callback. Both are subject to the same
286 * "no conditional control flow, no value manipulation" constraints.
287 *
288 * Excludes nodes inside `fn.params` (default-parameter expressions) — those
289 * are arguably def-time too, but they're rarely the source of the bugs
290 * these rules target and treating them as in-scope produces noisy
291 * false-positives on legitimate defaults. Carve out if a rule emerges
292 * that genuinely cares.
293 *
294 * Nested `createStep` / `transform` callbacks are *execution-time*; they
295 * naturally fall out because they become the immediately-enclosing function.
296 */
297 export function isInWorkflowDefinitionScope(
298 node: TSESTree.Node,
299 bindings: WorkflowSdkBindings
300 ): boolean {
301 const fn = getEnclosingFunction(node)
302 if (!fn) {
303 return false
304 }
305 if (
306 !isWorkflowConstructorFunction(fn, bindings) &&
307 !isWhenThenCallbackFunction(fn, bindings)
308 ) {
309 return false
310 }
311
312 let current: TSESTree.Node = node
313 let parent: TSESTree.Node | undefined = current.parent
314 while (parent && parent !== fn) {
315 current = parent
316 parent = current.parent
317 }
318 if (!parent) {
319 return false
320 }
321 return !(fn.params as TSESTree.Node[]).includes(current)
322 }