Skip to content

noImmediateMutation (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noImmediateMutation": "error"
}
}
}
}

Disallow changing a value right after it is created.

Adding to an array, object, Set, or Map in the statement after it is created splits its initial contents in two. The same goes for declaring a variable and assigning it in the next statement. Put the contents in the initial value instead.

const array = [1, 2];
array.push(3, 4);
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This array is changed right after it is created.

1 │ const array = [1, 2];
> 2 │ array.push(3, 4);
│ ^^^^^^^^^^
3 │

ℹ The array is created here.

> 1 │ const array = [1, 2];
│ ^^^^^^
2 │ array.push(3, 4);
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·array·=·[1,·2];
2 │ - array.push(3,·4);
1 │ + const·array·=·[1,·2,·3,·4];
3 2 │

const array = [3, 4];
array.unshift(1, 2);
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This array is changed right after it is created.

1 │ const array = [3, 4];
> 2 │ array.unshift(1, 2);
│ ^^^^^^^^^^^^^
3 │

ℹ The array is created here.

> 1 │ const array = [3, 4];
│ ^^^^^^
2 │ array.unshift(1, 2);
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·array·=·[3,·4];
2 │ - array.unshift(1,·2);
1 │ + const·array·=·[1,·2,·3,·4];
3 2 │

const object = { foo: 1 };
object.bar = 2;
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This object is changed right after it is created.

1 │ const object = { foo: 1 };
> 2 │ object.bar = 2;
│ ^^^^^^^^^^^^
3 │

ℹ The object is created here.

> 1 │ const object = { foo: 1 };
│ ^^^^^^^^^^
2 │ object.bar = 2;
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·object·=·{·foo:·1·};
2 │ - object.bar·=·2;
1 │ + const·object·=·{·foo:·1,·bar:·2·};
3 2 │

const object = { foo: 1 };
Object.assign(object, { bar: 2 });
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This object is changed right after it is created.

1 │ const object = { foo: 1 };
> 2 │ Object.assign(object, { bar: 2 });
│ ^^^^^^^^^^^^^
3 │

ℹ The object is created here.

> 1 │ const object = { foo: 1 };
│ ^^^^^^^^^^
2 │ Object.assign(object, { bar: 2 });
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·object·=·{·foo:·1·};
2 │ - Object.assign(object,·{·bar:·2·});
1 │ + const·object·=·{·foo:·1,·bar:·2·};
3 2 │

const set = new Set([1, 2]);
set.add(3);
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This Set is changed right after it is created.

1 │ const set = new Set([1, 2]);
> 2 │ set.add(3);
│ ^^^^^^^
3 │

ℹ The Set is created here.

> 1 │ const set = new Set([1, 2]);
│ ^^^^^^^^^^^^^^^
2 │ set.add(3);
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·set·=·new·Set([1,·2]);
2 │ - set.add(3);
1 │ + const·set·=·new·Set([1,·2,·3]);
3 2 │

const map = new Map([["foo", 1]]);
map.set("bar", 2);
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This Map is changed right after it is created.

1 │ const map = new Map([["foo", 1]]);
> 2 │ map.set("bar", 2);
│ ^^^^^^^
3 │

ℹ The Map is created here.

> 1 │ const map = new Map([["foo", 1]]);
│ ^^^^^^^^^^^^^^^^^^^^^
2 │ map.set("bar", 2);
3 │

ℹ Creating a value and changing it in the next statement splits its initial contents across two statements, which makes them harder to read.

ℹ Include the change in the initial value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move the change into the initial value.

1 │ - const·map·=·new·Map([["foo",·1]]);
2 │ - map.set("bar",·2);
1 │ + const·map·=·new·Map([["foo",·1],·["bar",·2]]);
3 2 │

let foo;
foo = 1;
code-block.js:2:1 lint/nursery/noImmediateMutation  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This variable is assigned right after it is declared.

1 │ let foo;
> 2 │ foo = 1;
│ ^^^^^^^
3 │

ℹ The declaration is here.

> 1 │ let foo;
│ ^^^^^^^^
2 │ foo = 1;
3 │

ℹ Declaring a variable and assigning its first value in two statements makes the code longer and harder to follow.

ℹ Assign the value in the declaration instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Assign the value in the declaration.

1 │ - let·foo;
2 │ - foo·=·1;
1 │ + let·foo·=·1;
3 2 │

const array = [1, 2, 3, 4];
const object = { foo: 1, bar: 2, ...other };
const set = new Set([1, 2, 3]);
const map = new Map([
["foo", 1],
["bar", 2],
]);
const items = [];
items.push(items.length);
let foo = 1;