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 Set or WeakSet from an array literal and immediately adding a new element with {Set,WeakSet}.add(…).
  • Assign variable to a Map or WeakMap from 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],
]);