|
| 1 | +# Disallow immediate mutation after variable assignment |
| 2 | + |
| 3 | +💼🚫 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the ☑️ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). |
| 4 | + |
| 5 | +🔧💡 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). |
| 6 | + |
| 7 | +<!-- end auto-generated rule header --> |
| 8 | +<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> |
| 9 | + |
| 10 | +When you create a variable and immediately mutate it, you should instead include those changes in the initial value. |
| 11 | + |
| 12 | +- Assign variable to an array literal and immediately mutate with `Array#{push,unshift}(…)`. |
| 13 | +- Assign variable to an object literal and immediately assign another property. |
| 14 | +- Assign variable to an object literal and immediately mutate with `Object.assign(…)`. |
| 15 | +- Assign variable to a `Set` or `WeakSet` from an array literal and immediately adding an new element with `{Set,WeakSet}.add(…)`. |
| 16 | +- Assign variable to a `Map` or `WeakMap` from an array literal and immediately set another key with `{Map,WeakMap}.set(…, …)`. |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +```js |
| 21 | +// ❌ |
| 22 | +const array = [1, 2]; |
| 23 | +array.push(3, 4); |
| 24 | + |
| 25 | +// ✅ |
| 26 | +const array = [1, 2, 3, 4]; |
| 27 | +``` |
| 28 | + |
| 29 | +```js |
| 30 | +// ❌ |
| 31 | +const array = [3, 4]; |
| 32 | +array.unshift(1, 2); |
| 33 | + |
| 34 | +// ✅ |
| 35 | +const array = [1, 2, 3, 4]; |
| 36 | +``` |
| 37 | + |
| 38 | +```js |
| 39 | +// ❌ |
| 40 | +const object = {foo: 1}; |
| 41 | +object.bar = 2; |
| 42 | + |
| 43 | +// ✅ |
| 44 | +const object = {foo: 1, bar: 2}; |
| 45 | +``` |
| 46 | + |
| 47 | +```js |
| 48 | +// ❌ |
| 49 | +const object = {foo: 1}; |
| 50 | +Object.assign(object, {bar: 2}); |
| 51 | + |
| 52 | +// ✅ |
| 53 | +const object = {foo: 1, bar: 2}; |
| 54 | +``` |
| 55 | + |
| 56 | +```js |
| 57 | +// ❌ |
| 58 | +const object = {foo: 1}; |
| 59 | +Object.assign(object, bar); |
| 60 | + |
| 61 | +// ✅ |
| 62 | +const object = {foo: 1, ...bar}; |
| 63 | +``` |
| 64 | + |
| 65 | +```js |
| 66 | +// ❌ |
| 67 | +const set = new Set([1, 2]); |
| 68 | +set.add(3); |
| 69 | + |
| 70 | +// ✅ |
| 71 | +const set = new Set([1, 2, 3]); |
| 72 | +``` |
| 73 | + |
| 74 | +```js |
| 75 | +// ❌ |
| 76 | +const weakSet = new WeakSet([foo, bar]); |
| 77 | +weakSet.add(baz); |
| 78 | + |
| 79 | +// ✅ |
| 80 | +const weakSet = new WeakSet([foo, bar, baz]); |
| 81 | +``` |
| 82 | + |
| 83 | +```js |
| 84 | +// ❌ |
| 85 | +const map = new Map([ |
| 86 | + ['foo', 1], |
| 87 | +]); |
| 88 | +map.set('bar', 2); |
| 89 | + |
| 90 | +// ✅ |
| 91 | +const map = new Map([ |
| 92 | + ['foo', 1], |
| 93 | + ['bar', 2], |
| 94 | +]); |
| 95 | +``` |
| 96 | + |
| 97 | +```js |
| 98 | +// ❌ |
| 99 | +const weakMap = new WeakMap([ |
| 100 | + [foo, 1], |
| 101 | +]); |
| 102 | +weakMap.set(bar, 2); |
| 103 | + |
| 104 | +// ✅ |
| 105 | +const weakMap = new WeakMap([ |
| 106 | + [foo, 1], |
| 107 | + [bar, 2], |
| 108 | +]); |
| 109 | +``` |
0 commit comments