unicorn/no-magic-array-flat-depth

Disallow an unexplained number as the depth of .flat()

Someone reading rows.flat(3) can't tell why the depth is 3, or whether it was picked to mean "all the way down". Say why in a comment, give the number a name, or use Infinity to flatten every level.

A depth of 1 is allowed, because it's the default.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-magic-array-flat-depth" = "error" # or "warn"

Examples#

example.js1 problem
const cells = tables.flat(2);
example.jsNo problems
const cells = tables.flat(2 /* table, then row */);

const TABLE_DEPTH = 2;
const sameCells = tables.flat(TABLE_DEPTH);

const leaves = tree.flat(Infinity);
const rows = tables.flat(1);