useIndexOf
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/complexity/useIndexOf¥Diagnostic Category:
lint/complexity/useIndexOf -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/prefer-array-index-of相同¥Same as
unicorn/prefer-array-index-of
-
¥How to configure
{ "linter": { "rules": { "complexity": { "useIndexOf": "error" } } }}¥Description
查找项目索引时,优先使用 Array#{indexOf,lastIndexOf}() 而不是 Array#{findIndex,findLastIndex}()。
¥Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item.
Array#findIndex() 和 Array#findLastIndex() 旨在满足更复杂的需求。如果你只是在查找给定项所在的索引,那么代码可以简化为使用 Array#indexOf() 或 Array#lastIndexOf()。这适用于任何使用字面量、变量或任何没有显式副作用的表达式进行的搜索。但是,如果你要查找的表达式依赖于与函数相关的项(例如函数的参数、函数自身等),则此情况仍然有效。
¥Array#findIndex() and Array#findLastIndex() are intended for more complex needs.
If you are just looking for the index where the given item is present, then the code can be simplified to use Array#indexOf() or Array#lastIndexOf().
This applies to any search with a literal, a variable, or any expression that doesn’t have any explicit side effects.
However, if the expression you are looking for relies on an item related to the function (its arguments, the function self, etc.), the case is still valid.
¥Examples
¥Invalid
const index = foo.findIndex(x => x === 'foo');code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
> 1 │ const index = foo.findIndex(x => x === ‘foo’);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.
ℹ Unsafe fix: Replace Array#findIndex() with Array#indexOf()
1 │ - const·index·=·foo.findIndex(x·=>·x·===·‘foo’);
1 │ + const·index·=·foo.indexOf(‘foo’);
2 2 │
const index = foo.findIndex(x => 'foo' === x);code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
> 1 │ const index = foo.findIndex(x => ‘foo’ === x);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.
ℹ Unsafe fix: Replace Array#findIndex() with Array#indexOf()
1 │ - const·index·=·foo.findIndex(x·=>·‘foo’·===·x);
1 │ + const·index·=·foo.indexOf(‘foo’·);
2 2 │
const index = foo.findIndex(x => { return x === 'foo';});code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
> 1 │ const index = foo.findIndex(x => {
│ ^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘foo’;
> 3 │ });
│ ^^
4 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to indexOf() instead.
ℹ Unsafe fix: Replace Array#findIndex() with Array#indexOf()
1 │ - const·index·=·foo.findIndex(x·=>·{
2 │ - ·····return·x·===·‘foo’;
3 │ - });
1 │ + const·index·=·foo.indexOf(‘foo’);
4 2 │
const index = foo.findLastIndex(x => 'foo' === x);code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
> 1 │ const index = foo.findLastIndex(x => ‘foo’ === x);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.
ℹ Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()
1 │ - const·index·=·foo.findLastIndex(x·=>·‘foo’·===·x);
1 │ + const·index·=·foo.lastIndexOf(‘foo’·);
2 2 │
const index = foo.findLastIndex(x => { return x === 'bar';});code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
> 1 │ const index = foo.findLastIndex(x => {
│ ^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘bar’;
> 3 │ });
│ ^^
4 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.
ℹ Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()
1 │ - const·index·=·foo.findLastIndex(x·=>·{
2 │ - ·····return·x·===·‘bar’;
3 │ - });
1 │ + const·index·=·foo.lastIndexOf(‘bar’);
4 2 │
const index = foo.findLastIndex(function(x) { return x === 'foo';});code-block.js:1:15 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
> 1 │ const index = foo.findLastIndex(function(x) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 2 │ return x === ‘foo’;
> 3 │ });
│ ^^
4 │
ℹ This callback only tests for equality against a single value. This value can be passed directly to lastIndexOf() instead.
ℹ Unsafe fix: Replace Array#findLastIndex() with Array#lastIndexOf()
1 │ - const·index·=·foo.findLastIndex(function(x)·{
2 │ - ·····return·x·===·‘foo’;
3 │ - });
1 │ + const·index·=·foo.lastIndexOf(‘foo’);
4 2 │
¥Valid
const index = foo.indexOf('foo');const index = foo.findIndex(x => x !== 'foo');const index = foo.findIndex((x, index) => x === index);const index = foo.findIndex(x => (x === 'foo') && isValid());const index = foo.findIndex(x => y === 'foo');const index = foo.findIndex(x => y.x === 'foo');const index = foo.findIndex(x => { const bar = getBar(); return x === bar;});const index = foo.findIndex(function(x) { const bar = getBar(); return x === bar;});const index = foo.lastIndexOf('foo');const index = foo.findLastIndex(x => x !== 'foo');const index = foo.findLastIndex((x, index) => x === index);const index = foo.findLastIndex(x => (x === 'foo') && isValid());const index = foo.findLastIndex(x => y === 'foo');const index = foo.findLastIndex(x => y.x === 'foo');¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号