Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/slimy-zoos-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@biomejs/biome": patch
---

Fixed [#7233](https://github.com/biomejs/biome/issues/7233): The useIndexOf rule now correctly suggests using indexOf() instead of findIndex().

The diagnostic message was incorrectly recommending Array#findIndex() over Array#indexOf(), when it should recommend the opposite for simple equality checks.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Rule for UseIndexOf {
rule_category!(),
node.range(),
markup! {
"Prefer "<Emphasis>"Array#"{old_member_name}"()"</Emphasis>" over "<Emphasis>"Array#"{new_member_name}"()"</Emphasis>" when looking for the index of an item.`"
"Prefer "<Emphasis>"Array#"{new_member_name}"()"</Emphasis>" over "<Emphasis>"Array#"{old_member_name}"()"</Emphasis>" when looking for the index of an item."
},
Comment on lines +178 to 179
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify snapshots and references to the old phrasing.

To be safe, scan for any stale snapshots or docs still asserting the old message text and update them if needed. Also, please run the formatter as per repo conventions.

Run this from the repo root:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching for old, now-incorrect diagnostics…"
rg -n -C2 -P 'Prefer\s+Array#find(Index|LastIndex)\(\)\s+over\s+Array#(last)?indexOf\(\)'

echo
echo "Searching for the new, correct diagnostics…"
rg -n -C2 -P 'Prefer\s+Array#(last)?indexOf\(\)\s+over\s+Array#find(Last)?Index\(\)'

echo
echo "Reminder: format Rust/TOML before commit:"
echo "  just format"

Length of output: 3517


Update stale snapshots & run formatter

The snapshots under crates/biome_js_analyze/tests/specs/complexity/useIndexOf/invalid.js.snap still contain the old message:

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.

Please update those lines to:

i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

Then run:

just format

🤖 Prompt for AI Agents
In crates/biome_js_analyze/src/lint/complexity/use_index_of.rs around lines
178-179, the emitted message text was inverted; update the snapshot(s) under
crates/biome_js_analyze/tests/specs/complexity/useIndexOf/invalid.js.snap to
replace the old line "i Prefer Array#findIndex() over Array#indexOf() when
looking for the index of an item." with "i Prefer Array#indexOf() over
Array#findIndex() when looking for the index of an item." and then run the
formatter with the command `just format`.

).note(markup! {
"This callback only tests for equality against a single value. This value can be passed directly to "<Emphasis>""{new_member_name}"()"</Emphasis>" instead."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ list.findLastIndex(x => x === undefined);
```
invalid.js:2:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

1 │ const list = ['foo', 'bar', 'baz'];
> 2 │ list.findIndex(bar => bar === 'foo');
Expand All @@ -53,7 +53,7 @@ invalid.js:2:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:3:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

1 │ const list = ['foo', 'bar', 'baz'];
2 │ list.findIndex(bar => bar === 'foo');
Expand All @@ -79,7 +79,7 @@ invalid.js:3:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:4:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

2 │ list.findIndex(bar => bar === 'foo');
3 │ list.findIndex((x) => x === 'foo');
Expand All @@ -105,7 +105,7 @@ invalid.js:4:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:5:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

3 │ list.findIndex((x) => x === 'foo');
4 │ list.findIndex(x => 'foo' === x);
Expand Down Expand Up @@ -136,7 +136,7 @@ invalid.js:5:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:9:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findIndex() over Array#indexOf() when looking for the index of an item.`
i Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item.

7 │ });
8 │ list.findIndex((x, y) => x === 'foo');
Expand All @@ -162,7 +162,7 @@ invalid.js:9:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:11:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.

9 │ list.findIndex(x => x === undefined);
10 │
Expand All @@ -188,7 +188,7 @@ invalid.js:11:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:12:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.

11 │ list.findLastIndex(bar => bar === 'foo');
> 12 │ list.findLastIndex((x) => x === 'foo');
Expand All @@ -213,7 +213,7 @@ invalid.js:12:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:13:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.

11 │ list.findLastIndex(bar => bar === 'foo');
12 │ list.findLastIndex((x) => x === 'foo');
Expand All @@ -239,7 +239,7 @@ invalid.js:13:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:14:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.

12 │ list.findLastIndex((x) => x === 'foo');
13 │ list.findLastIndex(x => 'foo' === x);
Expand Down Expand Up @@ -270,7 +270,7 @@ invalid.js:14:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━
```
invalid.js:18:1 lint/complexity/useIndexOf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

i Prefer Array#findLastIndex() over Array#lastIndexOf() when looking for the index of an item.`
i Prefer Array#lastIndexOf() over Array#findLastIndex() when looking for the index of an item.

16 │ });
17 │ list.findLastIndex((x, y) => x === 'foo');
Expand Down