1 ---
2 sidebar_label: "no-direct-variable-mutation-in-workflow"
3 ---
4
5 export const metadata = {
6 title: `no-direct-variable-mutation-in-workflow - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule disallows mutating or recomputing variables 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 assignments, increments, arithmetic, and template literals that use placeholder values 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 createStep,
27 WorkflowResponse,
28 } from "@medusajs/framework/workflows-sdk"
29
30 createWorkflow("my-workflow", (input) => {
31 const message1 = step1(input)
32 const message2 = step2(input)
33 const combined = `${message1} ${message2}`
34 return new WorkflowResponse(combined)
35 })
36 ```
37
38 Instead, derive new values inside `transform`:
39
40 ```ts title="src/workflows/example.ts"
41 import {
42 createWorkflow,
43 transform,
44 } from "@medusajs/framework/workflows-sdk"
45
46 createWorkflow("my-workflow", (input) => {
47 const message1 = step1(input)
48 const message2 = step2(input)
49 const combined = transform({
50 message1, message2,
51 }, ({ message1, message2 }) => {
52 return `${message1} ${message2}`
53 })
54
55 return new WorkflowResponse(combined)
56 })
57 ```
58
59 ## Why it's Important
60
61 A workflow's constructor and `when().then()` callbacks run at definition time, not execution time. So, locals like step outputs and input are placeholders, not real values. Mutating or recomputing them doesn't work as expected. Use `transform` to derive new values.
62
63 Learn more in the [Workflows documentation](!docs!/learn/fundamentals/workflows).
64
65 ## Fixable
66
67 This rule isn't auto-fixable.