1 import { AST_NODE_TYPES } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import {
4 createWorkflowSdkBindings,
5 isInWorkflowDefinitionScope,
6 trackWorkflowSdkImports,
7 } from "../../util/workflow-scope"
8
9 type MessageIds =
10 | "logicalExpression"
11 | "conditionalExpression"
12 | "negation"
13 | "optionalChaining"
14 | "equalityExpression"
15
16 const EQUALITY_OPERATORS = new Set(["===", "==", "!==", "!="])
17
18 export const rule = createRule<[], MessageIds>({
19 name: "no-conditional-expressions-in-workflow",
20 meta: {
21 type: "problem",
22 docs: {
23 description:
24 "Disallow conditional operators (`&&`, `||`, `??`, ternary, `!`, `!!`, optional chaining, `===`/`==`/`!==`/`!=`) directly inside a `createWorkflow` constructor or a `when().then()` callback. Wrap value manipulation in `transform`.",
25 },
26 messages: {
27 logicalExpression:
28 "Do not use `{{operator}}` inside a workflow constructor or a `when().then()` callback — both run at definition time, not execution time. Wrap value manipulation in `transform(...)` from `@medusajs/framework/workflows-sdk`.",
29 conditionalExpression:
30 "Do not use ternary expressions inside a workflow constructor or a `when().then()` callback — both run at definition time, not execution time. Wrap value manipulation in `transform(...)` from `@medusajs/framework/workflows-sdk`.",
31 negation:
32 "Do not use the `!` operator inside a workflow constructor or a `when().then()` callback — it runs at definition time, not execution time. Wrap value manipulation in `transform(...)` from `@medusajs/framework/workflows-sdk`.",
33 equalityExpression:
34 "Do not use `{{operator}}` inside a workflow constructor or a `when().then()` callback — it runs at definition time, not execution time. Wrap value comparison in `transform(...)` from `@medusajs/framework/workflows-sdk`.",
35 optionalChaining:
36 "Do not use optional chaining (`?.`) inside a workflow constructor or a `when().then()` callback — both run at definition time, not execution time. Wrap value manipulation in `transform(...)` from `@medusajs/framework/workflows-sdk`.",
37 },
38 schema: [],
39 },
40 defaultOptions: [],
41 create(context) {
42 const bindings = createWorkflowSdkBindings()
43
44 return {
45 ImportDeclaration(node) {
46 trackWorkflowSdkImports(node, bindings)
47 },
48
49 LogicalExpression(node) {
50 if (bindings.createWorkflow.size === 0) {
51 return
52 }
53 if (!isInWorkflowDefinitionScope(node, bindings)) {
54 return
55 }
56 context.report({
57 node,
58 messageId: "logicalExpression",
59 data: { operator: node.operator },
60 })
61 },
62
63 ConditionalExpression(node) {
64 if (bindings.createWorkflow.size === 0) {
65 return
66 }
67 if (!isInWorkflowDefinitionScope(node, bindings)) {
68 return
69 }
70 context.report({
71 node,
72 messageId: "conditionalExpression",
73 })
74 },
75
76 UnaryExpression(node) {
77 if (bindings.createWorkflow.size === 0) {
78 return
79 }
80 if (node.operator !== "!") {
81 return
82 }
83 // Avoid double-reporting on `!!x` / `!!!x` — only the outermost
84 // `!` reports (the one whose parent is not also `!`).
85 if (
86 node.parent?.type === AST_NODE_TYPES.UnaryExpression &&
87 node.parent.operator === "!"
88 ) {
89 return
90 }
91 if (!isInWorkflowDefinitionScope(node, bindings)) {
92 return
93 }
94 context.report({
95 node,
96 messageId: "negation",
97 })
98 },
99
100 BinaryExpression(node) {
101 if (bindings.createWorkflow.size === 0) {
102 return
103 }
104 if (!EQUALITY_OPERATORS.has(node.operator)) {
105 return
106 }
107 if (!isInWorkflowDefinitionScope(node, bindings)) {
108 return
109 }
110 context.report({
111 node,
112 messageId: "equalityExpression",
113 data: { operator: node.operator },
114 })
115 },
116
117 ChainExpression(node) {
118 if (bindings.createWorkflow.size === 0) {
119 return
120 }
121 if (!isInWorkflowDefinitionScope(node, bindings)) {
122 return
123 }
124 context.report({
125 node,
126 messageId: "optionalChaining",
127 })
128 },
129 }
130 },
131 })
132
133 export default rule