Skip to content
← Back to rules

unicorn/no-magic-array-flat-depth Restriction

它的作用

禁止为 Array.prototype.flat 的深度使用魔法数字。

为什么这不好?

魔法数字很难理解和维护。 在调用 Array.prototype.flat 时,通常会使用 1Infinity。如果你使用的是其他数字, 最好添加注释来解释所提供深度的原因。

示例

以下是此规则的错误代码示例:

javascript
array.flat(2);
array.flat(20);

以下是此规则的正确代码示例:

javascript
array.flat(2 /* 说明 */);
array.flat(1);
array.flat();
array.flat(Infinity);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-magic-array-flat-depth": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-magic-array-flat-depth": "error",
  },
});
bash
oxlint --deny unicorn/no-magic-array-flat-depth

版本

此规则添加于 v0.4.2。

参考资料