Skip to main content

no-assign-mutated-array

Disallow assigning values returned from mutating array methods.

Targeted pattern scopeโ€‹

โš ๏ธ This rule requires type information to run. Configure type-aware linting (parserOptions.project or projectService) before enabling it.

This rule targets calls to fill, reverse, and sort on array-like values.

It reports when these calls are used as values (for example assignment, argument, return), not when used as standalone statements.

What this rule reportsโ€‹

This rule reports assignments or argument passing of values returned by fill, reverse, and sort when those methods mutate an existing array reference.

These methods mutate in place and return the same array instance. Assigning their return value often reads like a copy operation even though it mutates shared state.

Why this rule existsโ€‹

Using the returned value of mutating methods often looks copy-like while still mutating shared state, which causes subtle bugs.

โŒ Incorrectโ€‹

const names = [
"c",
"a",
"b",
];
const sorted = names.sort();
const names = [
"c",
"a",
"b",
];
print(names.reverse());
const sorted = users.sort();
return sorted;

โœ… Correctโ€‹

const names = [
"c",
"a",
"b",
];
names.sort();
const names = [
"c",
"a",
"b",
];
const sorted = names.slice().sort();
const names = [
"c",
"a",
"b",
];
const sorted = names.map((name) => name).reverse();
const sorted = Array.from(users).sort();

Behavior and migration notesโ€‹

This rule reports only and does not provide an autofix.

It intentionally does not report when the mutating call is rooted in a new array expression or array factory chain (for example Array.from(...).sort()).

Optionsโ€‹

This rule has no options.

Additional examplesโ€‹

const result = source.slice().sort();
// โœ… valid because `slice()` creates a new array before sort

const result2 = source.sort();
// โŒ reported because `sort()` mutates `source`

ESLint flat config exampleโ€‹

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/no-assign-mutated-array": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your codebase intentionally relies on mutating array methods and treats assigned return values as an accepted pattern.

Package documentationโ€‹

Rule catalog ID: R016

Further readingโ€‹

Adoption resourcesโ€‹

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.