unicorn/no-immediate-mutation
Disallow changing an array, object, Set, or Map in the statement right after creating it
If you create a value on one line and fill it on the next, you're using two statements to say one thing. Put everything in the initializer, so you can read the whole value in one place.
The rule looks at the statement right after the one that creates the value:
| Created with | Reported next |
|---|---|
| An array literal | .push(), .unshift() |
| An object literal | Assigning a property, Object.assign() |
new Set(), new WeakSet() | .add() |
new Map(), new WeakMap() | .set() |
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-immediate-mutation" = "error" # or "warn"Examples#
example.js2 problems
const fruits = ["apple", "pear"];
fruits.push("plum")Do not call push() immediately after initializing an array.;
const user = { name: "Ada" };
user.role = "admin"Do not assign a property immediately after initializing an object literal.;example.js2 problems
const seen = new Set(["apple"]);
seen.add("pear")Do not call .add() immediately after initializing a Set.;
const prices = new Map([["apple", 1]]);
prices.set("pear", 2)Do not call .set() immediately after initializing a Map.;example.jsNo problems
const fruits = ["apple", "pear", "plum"];
const user = { name: "Ada", role: "admin" };
const seen = new Set(["apple", "pear"]);
const prices = new Map([
["apple", 1],
["pear", 2],
]);A change that reads the variable itself can't move into the initializer, so it's allowed:
example.jsNo problems
const fruits = ["apple", "pear"];
fruits.push(fruits.length);A change that comes after another statement is allowed too:
example.jsNo problems
const fruits = ["apple", "pear"];
console.log(fruits);
fruits.push("plum");