|
1
|
--- |
|
2
|
sidebar_label: "when-block-must-have-name" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `when-block-must-have-name - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires a `when(...)` call to include an explicit name as its first argument when its `.then()` callback returns a value that isn't a direct step invocation. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets `when(...)` calls inside `createWorkflow` bodies that: |
|
20
|
|
|
21
|
- use the two-argument form `when(values, condition)` (no name), and |
|
22
|
- have a `.then()` callback that returns something that isn't a step result. |
|
23
|
|
|
24
|
When the `.then()` callback returns a `transform(...)` call, a plain object literal, or any other non-step result, Medusa wraps the result in a synthetic step. Without an explicit name, Medusa assigns a random name to that step at runtime and logs a warning in production. |
|
25
|
|
|
26
|
The following code is reported by the rule: |
|
27
|
|
|
28
|
```ts title="src/workflows/example.ts" |
|
29
|
import { |
|
30
|
createWorkflow, |
|
31
|
when, |
|
32
|
transform, |
|
33
|
} from "@medusajs/framework/workflows-sdk" |
|
34
|
|
|
35
|
export const exampleWorkflow = createWorkflow( |
|
36
|
"example", |
|
37
|
(input) => { |
|
38
|
when({ input }, ({ input }) => input.data?.custom_amount === null).then( |
|
39
|
() => { |
|
40
|
return transform({ input }, ({ input }) => ({ id: input.id })) |
|
41
|
} |
|
42
|
) |
|
43
|
} |
|
44
|
) |
|
45
|
``` |
|
46
|
|
|
47
|
To fix this, pass a unique name as the first argument to `when`: |
|
48
|
|
|
49
|
```ts title="src/workflows/example.ts" |
|
50
|
import { |
|
51
|
createWorkflow, |
|
52
|
when, |
|
53
|
transform, |
|
54
|
} from "@medusajs/framework/workflows-sdk" |
|
55
|
|
|
56
|
export const exampleWorkflow = createWorkflow( |
|
57
|
"example", |
|
58
|
(input) => { |
|
59
|
when("custom-amount-null", { input }, ({ input }) => input.data?.custom_amount === null).then( |
|
60
|
() => { |
|
61
|
return transform({ input }, ({ input }) => ({ id: input.id })) |
|
62
|
} |
|
63
|
) |
|
64
|
} |
|
65
|
) |
|
66
|
``` |
|
67
|
|
|
68
|
## Why it's Important |
|
69
|
|
|
70
|
When the `.then()` callback returns a value that Medusa wraps in a synthetic step (such as a `transform` result), Medusa assigns a random name to that step at runtime if no name is provided. Random names change across restarts, which makes distributed-tracing logs and compensation-log entries hard to read and correlates poorly across runs. |
|
71
|
|
|
72
|
Learn more in the [Workflows documentation](!docs!/learn/fundamentals/workflows). |
|
73
|
|
|
74
|
## Fixable |
|
75
|
|
|
76
|
This rule is not auto-fixable. You must add the name manually as the first argument to `when`. |
|
77
|
|
|
78
|
## Turn it Off |
|
79
|
|
|
80
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
81
|
|
|
82
|
```ts title="eslint.config.ts" |
|
83
|
import { defineConfig } from "eslint/config" |
|
84
|
import medusa from "@medusajs/eslint-plugin" |
|
85
|
|
|
86
|
export default defineConfig([ |
|
87
|
...medusa.configs.recommended, |
|
88
|
{ |
|
89
|
rules: { |
|
90
|
"@medusajs/when-block-must-have-name": "off", |
|
91
|
}, |
|
92
|
}, |
|
93
|
]) |
|
94
|
``` |
|
95
|
|
|
96
|
Or disable it for a single line using an inline comment: |
|
97
|
|
|
98
|
```ts |
|
99
|
// eslint-disable-next-line @medusajs/when-block-must-have-name |
|
100
|
when({ input }, ({ input }) => !!input.flag).then(() => { |
|
101
|
``` |