unicorn/no-unnecessary-array-splice-count Pedantic
它的作用
禁止将 .length 或 Infinity 作为 Array#splice() 或 Array#toSpliced() 的 deleteCount 或 skipCount 参数传入。
为什么这不好?
调用 Array#splice(start, deleteCount) 或 Array#toSpliced(start, skipCount) 时, 省略 deleteCount 或 skipCount 参数会删除或跳过 start 之后的所有元素。 使用 .length 或 Infinity 是不必要的,并且会使代码更冗长。
示例
以下是此规则的错误代码示例:
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 中新增。
