no-immediate-mutation
March 27, 2026 Β· View on GitHub
π Disallow immediate mutation after variable assignment.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
When you create a variable and immediately mutate it, you should instead include those changes in the initial value.
- Assign variable to an array literal and immediately mutate with
Array#{push,unshift}(β¦). - Assign variable to an object literal and immediately assign another property.
- Assign variable to an object literal and immediately mutate with
Object.assign(β¦). - Assign variable to a
SetorWeakSetfrom an array literal and immediately adding a new element with{Set,WeakSet}.add(β¦). - Assign variable to a
MaporWeakMapfrom an array literal and immediately set another key with{Map,WeakMap}.set(β¦, β¦).
Examples
// β
const array = [1, 2];
array.push(3, 4);
// β
const array = [1, 2, 3, 4];
// β
const array = [3, 4];
array.unshift(1, 2);
// β
const array = [1, 2, 3, 4];
// β
const object = {foo: 1};
object.bar = 2;
// β
const object = {foo: 1, bar: 2};
// β
const object = {foo: 1};
Object.assign(object, {bar: 2});
// β
const object = {foo: 1, bar: 2};
// β
const object = {foo: 1};
Object.assign(object, bar);
// β
const object = {foo: 1, ...bar};
// β
const set = new Set([1, 2]);
set.add(3);
// β
const set = new Set([1, 2, 3]);
// β
const weakSet = new WeakSet([foo, bar]);
weakSet.add(baz);
// β
const weakSet = new WeakSet([foo, bar, baz]);
// β
const map = new Map([
['foo', 1],
]);
map.set('bar', 2);
// β
const map = new Map([
['foo', 1],
['bar', 2],
]);
// β
const weakMap = new WeakMap([
[foo, 1],
]);
weakMap.set(bar, 2);
// β
const weakMap = new WeakMap([
[foo, 1],
[bar, 2],
]);