Skip to content
← Back to rules

unicorn/no-useless-collection-argument 样式

💡 A suggestion is available for this rule.

功能说明

禁止在 SetMapWeakSetWeakMap 中使用无用的值或回退值。

为什么这很糟糕?

在构造 SetMapWeakSetWeakMap 时,传入空数组或空字符串是不必要的,因为它们接受空值。

对于可能为空的值,提供回退值也是不必要的。

示例

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

js
const set = new Set([]);
const set = new Set("");

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

js
const set = new Set();

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

js
const set = new Set(foo ?? []);
const set = new Set(foo ?? "");

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

js
const set = new Set(foo);

如何使用

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

json
{
  "rules": {
    "unicorn/no-useless-collection-argument": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-useless-collection-argument": "error",
  },
});
bash
oxlint --deny unicorn/no-useless-collection-argument

版本

此规则于 v1.28.0 中添加。

参考资料