|
1
|
import type { TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { |
|
5
|
createWorkflowSdkBindings, |
|
6
|
trackWorkflowSdkImports, |
|
7
|
} from "../../util/workflow-scope" |
|
8
|
|
|
9
|
type MessageIds = "asyncWorkflowConstructor" |
|
10
|
|
|
11
|
export const rule = createRule<[], MessageIds>({ |
|
12
|
name: "no-async-workflow-constructor", |
|
13
|
meta: { |
|
14
|
type: "problem", |
|
15
|
docs: { |
|
16
|
description: |
|
17
|
"The function passed to `createWorkflow` must not be `async`. Workflow constructors run at definition time, not execution time.", |
|
18
|
}, |
|
19
|
fixable: "code", |
|
20
|
messages: { |
|
21
|
asyncWorkflowConstructor: |
|
22
|
"The function passed to `createWorkflow` must not be `async`. Workflow constructors are evaluated at definition time and cannot await; use steps for asynchronous work.", |
|
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
|
CallExpression(node) { |
|
36
|
if ( |
|
37
|
node.callee.type !== AST_NODE_TYPES.Identifier || |
|
38
|
!bindings.createWorkflow.has(node.callee.name) |
|
39
|
) { |
|
40
|
return |
|
41
|
} |
|
42
|
|
|
43
|
const constructor = node.arguments[1] |
|
44
|
if ( |
|
45
|
!constructor || |
|
46
|
(constructor.type !== AST_NODE_TYPES.ArrowFunctionExpression && |
|
47
|
constructor.type !== AST_NODE_TYPES.FunctionExpression) || |
|
48
|
!constructor.async |
|
49
|
) { |
|
50
|
return |
|
51
|
} |
|
52
|
|
|
53
|
const fn = constructor as |
|
54
|
| TSESTree.ArrowFunctionExpression |
|
55
|
| TSESTree.FunctionExpression |
|
56
|
|
|
57
|
context.report({ |
|
58
|
node: fn, |
|
59
|
messageId: "asyncWorkflowConstructor", |
|
60
|
fix(fixer) { |
|
61
|
const sourceCode = context.sourceCode ?? context.getSourceCode() |
|
62
|
const asyncToken = sourceCode.getFirstToken(fn) |
|
63
|
if (!asyncToken || asyncToken.value !== "async") { |
|
64
|
return null |
|
65
|
} |
|
66
|
const nextToken = sourceCode.getTokenAfter(asyncToken) |
|
67
|
if (!nextToken) { |
|
68
|
return null |
|
69
|
} |
|
70
|
return fixer.removeRange([asyncToken.range[0], nextToken.range[0]]) |
|
71
|
}, |
|
72
|
}) |
|
73
|
}, |
|
74
|
} |
|
75
|
}, |
|
76
|
}) |
|
77
|
|
|
78
|
export default rule |