|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "svelte/no-reactive-reassign" |
| 5 | +description: "disallow reassigning reactive values" |
| 6 | +--- |
| 7 | + |
| 8 | +# svelte/no-reactive-reassign |
| 9 | + |
| 10 | +> disallow reassigning reactive values |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule aims to prevent unintended behavior caused by modification or reassignment of reactive values. |
| 17 | + |
| 18 | +<ESLintCodeBlock> |
| 19 | + |
| 20 | +<!--eslint-skip--> |
| 21 | + |
| 22 | +```svelte |
| 23 | +<script> |
| 24 | + /* eslint svelte/no-reactive-reassign: "error" */ |
| 25 | + let value = 0 |
| 26 | + $: reactiveValue = value * 2 |
| 27 | +
|
| 28 | + function handleClick() { |
| 29 | + /* ✓ GOOD */ |
| 30 | + value++ |
| 31 | + /* ✗ BAD */ |
| 32 | + reactiveValue = value * 3 |
| 33 | + reactiveValue++ |
| 34 | + } |
| 35 | +</script> |
| 36 | +
|
| 37 | +<!-- ✓ GOOD --> |
| 38 | +<input type="number" bind:value /> |
| 39 | +<!-- ✗ BAD --> |
| 40 | +<input type="number" bind:value={reactiveValue} /> |
| 41 | +``` |
| 42 | + |
| 43 | +</ESLintCodeBlock> |
| 44 | + |
| 45 | +## :wrench: Options |
| 46 | + |
| 47 | +```json |
| 48 | +{ |
| 49 | + "svelte/no-reactive-reassign": ["error", { |
| 50 | + "props": true |
| 51 | + }] |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- `props` ... If set to `true`, this rule warns against the modification of reactive value properties. Default is `true`. |
| 56 | + |
| 57 | +### `{ "props": true }` |
| 58 | + |
| 59 | +<ESLintCodeBlock> |
| 60 | + |
| 61 | +<!--eslint-skip--> |
| 62 | + |
| 63 | +```svelte |
| 64 | +<script> |
| 65 | + /* eslint svelte/no-reactive-reassign: ["error", { "props": true }] */ |
| 66 | + let value = 0 |
| 67 | + $: reactiveValue = { value: value * 2} |
| 68 | +
|
| 69 | + function handleClick() { |
| 70 | + /* ✓ GOOD */ |
| 71 | + value++ |
| 72 | + /* ✗ BAD */ |
| 73 | + reactiveValue.value++ |
| 74 | + reactiveValue = { value: reactiveValue.value + 1 } |
| 75 | + } |
| 76 | +</script> |
| 77 | +
|
| 78 | +<!-- ✓ GOOD --> |
| 79 | +<input type="number" bind:value /> |
| 80 | +<!-- ✗ BAD --> |
| 81 | +<input type="number" bind:value={reactiveValue.value} /> |
| 82 | +<MyComponent bind:objectValue={reactiveValue} /> |
| 83 | +``` |
| 84 | + |
| 85 | +</ESLintCodeBlock> |
| 86 | + |
| 87 | +### `{ "props": false }` |
| 88 | + |
| 89 | +<ESLintCodeBlock> |
| 90 | + |
| 91 | +<!--eslint-skip--> |
| 92 | + |
| 93 | +```svelte |
| 94 | +<script> |
| 95 | + /* eslint svelte/no-reactive-reassign: ["error", { "props": false }] */ |
| 96 | + let value = 0 |
| 97 | + $: reactiveValue = { value: value * 2} |
| 98 | +
|
| 99 | + function handleClick() { |
| 100 | + /* ✓ GOOD */ |
| 101 | + value++ |
| 102 | + /* OK */ |
| 103 | + reactiveValue.value++ |
| 104 | + /* ✗ BAD */ |
| 105 | + reactiveValue = { value: reactiveValue.value + 1 } |
| 106 | + } |
| 107 | +</script> |
| 108 | +
|
| 109 | +<!-- ✓ GOOD --> |
| 110 | +<input type="number" bind:value /> |
| 111 | +<!-- OK --> |
| 112 | +<input type="number" bind:value={reactiveValue.value} /> |
| 113 | +<!-- ✗ BAD --> |
| 114 | +<MyComponent bind:objectValue={reactiveValue} /> |
| 115 | +``` |
| 116 | + |
| 117 | +</ESLintCodeBlock> |
| 118 | + |
| 119 | +## :mag: Implementation |
| 120 | + |
| 121 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-reactive-reassign.ts) |
| 122 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-reactive-reassign.ts) |
0 commit comments