Skip to content
← Back to rules

unicorn/no-unnecessary-slice-end 迂腐

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

作用

禁止在 slice(...) 中不必要地传入第二个参数, 对于那些这样做不会改变结果的情况。

为什么这不好?

在不使用第二个参数调用 .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);

如何使用

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

json
{
  "rules": {
    "unicorn/no-unnecessary-slice-end": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则已在 v0.16.10 中添加。

参考资料