|
1
|
--- |
|
2
|
sidebar_label: "no-conditional-expressions-in-workflow" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `no-conditional-expressions-in-workflow - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule disallows conditional operators, such as `&&`, `||`, `??`, ternaries, `!`, optional chaining, and equality checks, directly inside a `createWorkflow` constructor or a `when().then()` callback. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets files in your project's `src/workflows` directory. It reports conditional operators placed directly in a workflow's constructor or in a `when().then()` callback. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/workflows/example.ts" |
|
24
|
import { |
|
25
|
createWorkflow, |
|
26
|
WorkflowResponse, |
|
27
|
} from "@medusajs/framework/workflows-sdk" |
|
28
|
|
|
29
|
createWorkflow("my-workflow", (input) => { |
|
30
|
const v = input || "default" |
|
31
|
return new WorkflowResponse(v) |
|
32
|
}) |
|
33
|
``` |
|
34
|
|
|
35
|
Instead, wrap value manipulation in `transform`: |
|
36
|
|
|
37
|
```ts title="src/workflows/example.ts" |
|
38
|
import { |
|
39
|
createWorkflow, |
|
40
|
transform, |
|
41
|
WorkflowResponse, |
|
42
|
} from "@medusajs/framework/workflows-sdk" |
|
43
|
|
|
44
|
createWorkflow("my-workflow", (input) => { |
|
45
|
const v = transform({ input }, (data) => { |
|
46
|
return data.input || "default" |
|
47
|
}) |
|
48
|
|
|
49
|
return new WorkflowResponse(v) |
|
50
|
}) |
|
51
|
``` |
|
52
|
|
|
53
|
## Why it's Important |
|
54
|
|
|
55
|
A workflow's constructor and `when().then()` callbacks run at definition time, not execution time. So, the operands are placeholders rather than real values, and conditional operators don't behave as you might expect. Use `transform` to manipulate values at execution time. |
|
56
|
|
|
57
|
Learn more in the [Workflows documentation](!docs!/learn/fundamentals/workflows). |
|
58
|
|
|
59
|
## Fixable |
|
60
|
|
|
61
|
This rule isn't auto-fixable. |