Skip to content
← Back to rules

unicorn/no-unnecessary-array-splice-count Pedantic

🛠️ An auto-fix is available for this rule.

它的作用

禁止将 .lengthInfinity 作为 Array#splice()Array#toSpliced()deleteCountskipCount 参数传入。

为什么这不好?

调用 Array#splice(start, deleteCount)Array#toSpliced(start, skipCount) 时, 省略 deleteCountskipCount 参数会删除或跳过 start 之后的所有元素。 使用 .lengthInfinity 是不必要的,并且会使代码更冗长。

示例

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

js
array.splice(1, array.length);
array.splice(1, Infinity);
array.splice(1, Number.POSITIVE_INFINITY);
array.toSpliced(1, array.length);

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

js
array.splice(1);
array.toSpliced(1);

如何使用

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

json
{
  "rules": {
    "unicorn/no-unnecessary-array-splice-count": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-unnecessary-array-splice-count": "error",
  },
});
bash
oxlint --deny unicorn/no-unnecessary-array-splice-count

版本

此规则在 v1.20.0 中新增。

参考资料