Skip to content

noMagicArrayFlatDepth (JavaScript)

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

Disallow unexplained numbers as the depth argument of Array.prototype.flat().

The depth passed to flat() is usually 1 (the default) or Infinity. Any other number doesn’t say why the array needs to be flattened to that exact depth. Store the depth in a variable with a descriptive name, or add a comment inside the parentheses that explains it.

const items = array.flat(2);
code-block.js:1:26 lint/nursery/noMagicArrayFlatDepth ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The depth passed to flat() is an unexplained number.

> 1 │ const items = array.flat(2);
│ ^
2 │

ℹ A bare number doesn't say why the array needs to be flattened to this depth.

ℹ Store the depth in a variable with a descriptive name, or add a comment inside the parentheses that explains it.

ℹ 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.

const items = array.flat(99);
code-block.js:1:26 lint/nursery/noMagicArrayFlatDepth ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The depth passed to flat() is an unexplained number.

> 1 │ const items = array.flat(99);
│ ^^
2 │

ℹ A bare number doesn't say why the array needs to be flattened to this depth.

ℹ Store the depth in a variable with a descriptive name, or add a comment inside the parentheses that explains it.

ℹ 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.

const items = array.flat();
const allItems = array.flat(Infinity);
const allValues = array.flat(Number.POSITIVE_INFINITY);
const rows = array.flat(depth);
const cells = array.flat(/* The grid is always two levels deep. */ 2);