Skip to content

Commit b7aa111

Browse files
authored
fix: correct error message in useIndexOf rule (#7234)
1 parent c63026e commit b7aa111

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

.changeset/slimy-zoos-search.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#7233](https://github.com/biomejs/biome/issues/7233): The useIndexOf rule now correctly suggests using indexOf() instead of findIndex().
6+
7+
The diagnostic message was incorrectly recommending Array#findIndex() over Array#indexOf(), when it should recommend the opposite for simple equality checks.

crates/biome_js_analyze/src/lint/complexity/use_index_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl Rule for UseIndexOf {
175175
rule_category!(),
176176
node.range(),
177177
markup! {
178-
"Prefer "<Emphasis>"Array#"{old_member_name}"()"</Emphasis>" over "<Emphasis>"Array#"{new_member_name}"()"</Emphasis>" when looking for the index of an item.`"
178+
"Prefer "<Emphasis>"Array#"{new_member_name}"()"</Emphasis>" over "<Emphasis>"Array#"{old_member_name}"()"</Emphasis>" when looking for the index of an item."
179179
},
180180
).note(markup! {
181181
"This callback only tests for equality against a single value. This value can be passed directly to "<Emphasis>""{new_member_name}"()"</Emphasis>" instead."

crates/biome_js_analyze/tests/specs/complexity/useIndexOf/invalid.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ list.findLastIndex(x => x === undefined);
2929
```
3030
invalid.js:2:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3131
32-
i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
32+
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
3333
3434
1 │ const list = ['foo', 'bar', 'baz'];
3535
> 2 │ list.findIndex(bar => bar === 'foo');
@@ -53,7 +53,7 @@ invalid.js:2:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
5353
```
5454
invalid.js:3:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5555
56-
i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
56+
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
5757
5858
1 │ const list = ['foo', 'bar', 'baz'];
5959
2 │ list.findIndex(bar => bar === 'foo');
@@ -79,7 +79,7 @@ invalid.js:3:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
7979
```
8080
invalid.js:4:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8181
82-
i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
82+
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
8383
8484
2list.findIndex(bar => bar === 'foo');
8585
3list.findIndex((x) => x === 'foo');
@@ -105,7 +105,7 @@ invalid.js:4:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
105105
```
106106
invalid.js:5:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
107107
108-
i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
108+
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
109109
110110
3list.findIndex((x) => x === 'foo');
111111
4list.findIndex(x => 'foo' === x);
@@ -136,7 +136,7 @@ invalid.js:5:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
136136
```
137137
invalid.js:9:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
138138
139-
i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
139+
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.
140140
141141
7 │ });
142142
8list.findIndex((x, y) => x === 'foo');
@@ -162,7 +162,7 @@ invalid.js:9:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
162162
```
163163
invalid.js:11:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
164164
165-
i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
165+
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
166166
167167
9list.findIndex(x => x === undefined);
168168
10
@@ -188,7 +188,7 @@ invalid.js:11:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
188188
```
189189
invalid.js:12:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
190190
191-
i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
191+
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
192192
193193
11list.findLastIndex(bar => bar === 'foo');
194194
> 12list.findLastIndex((x) => x === 'foo');
@@ -213,7 +213,7 @@ invalid.js:12:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
213213
```
214214
invalid.js:13:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
215215
216-
i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
216+
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
217217
218218
11list.findLastIndex(bar => bar === 'foo');
219219
12list.findLastIndex((x) => x === 'foo');
@@ -239,7 +239,7 @@ invalid.js:13:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
239239
```
240240
invalid.js:14:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
241241
242-
i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
242+
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
243243
244244
12list.findLastIndex((x) => x === 'foo');
245245
13list.findLastIndex(x => 'foo' === x);
@@ -270,7 +270,7 @@ invalid.js:14:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
270270
```
271271
invalid.js:18:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
272272
273-
i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
273+
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.
274274
275275
16 │ });
276276
17list.findLastIndex((x, y) => x === 'foo');

0 commit comments

Comments
 (0)