close

no-magic-array-flat-depth

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-magic-array-flat-depth': 'error',
    },
  },
]);

Rule Details

Disallow a magic number as the depth argument in Array#flat(…).

Array#flat() is often used to flatten deeply nested arrays, but using a hard-coded depth like flat(2) or flat(99) is usually a sign the caller doesn't know the array's structure. Prefer flat(Infinity) for "fully flatten" or restructure the data so the depth is implicit.

Examples of incorrect code for this rule:

const array = [1, [2, [3]]];
array.flat(2);

Examples of correct code for this rule:

const array = [1, [2, [3]]];
array.flat(Infinity);
const array = [1, 2, 3];
array.flat();

Original Documentation