Skip to content
← Back to rules

unicorn/no-unnecessary-slice-end 严格

An auto-fix is available for this rule.

它的作用

省略结束参数会默认将其设为对象的 .length。 显式传递该参数或使用 Infinity 是多余的。

为什么这是不好的?

在 JavaScript 中,省略结束索引会使 .slice() 自动运行到目标末尾, 因此显式传入其长度或 Infinity 是冗余的。

示例

此规则的 错误 代码示例:

js
const foo = string.slice(1, string.length);
const foo = string.slice(1, Infinity);
const foo = string.slice(1, Number.POSITIVE_INFINITY);

此规则的 正确 代码示例:

js
const foo = string.slice(1);

如何使用

通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/no-unnecessary-slice-end": "error"
  }
}
bash
oxlint --deny unicorn/no-unnecessary-slice-end

参考资料