unicorn/prefer-array-index-of 样式
它的作用
强制在回调函数只是简单的严格相等比较时,使用 indexOf 或 lastIndexOf,而不是 findIndex 或 findLastIndex
为什么这不好?
当 indexOf(value) 能以更简洁、更清晰的方式完成同样的事情时,使用 findIndex(x => x === value) 就显得不必要地冗长。它还避免了创建回调函数的开销。
示例
以下是此规则的错误代码示例:
js
values.findIndex((x) => x === "foo");
values.findLastIndex((x) => x === "bar");以下是此规则的正确代码示例:
js
values.indexOf("foo");
values.lastIndexOf("bar");如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/prefer-array-index-of": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-array-index-of": "error",
},
});bash
oxlint --deny unicorn/prefer-array-index-of版本
此规则是在 v0.16.12 中添加的。
