fix(linter/unicorn): avoid prefer-array-find rest destructuring false positive#23654
Open
ColemanDunn wants to merge 1 commit into
Open
fix(linter/unicorn): avoid prefer-array-find rest destructuring false positive#23654ColemanDunn wants to merge 1 commit into
ColemanDunn wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
cbda5f5 to
e9ad446
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two issues in
unicorn/prefer-array-find:filter(), wherefind()is not an equivalent replacement.findLast()-style patterns. The upstream eslint-plugin-unicorn docs cover these cases, so this brings Oxlint's docs closer to the equivalent rule.False positive
On this line, Oxlint currently reports a false positive diagnostic from
unicorn/prefer-array-find:That diagnostic is incorrect because the code keeps both the first matching element and the remaining matching elements. Replacing it with
find()would droprestElements, so it is not an equivalent transformation.The fix is to only report array destructuring when there is no rest element. This still reports valid cases like:
Docs mismatch
Oxlint already detects more than the docs describe. In addition to
filter(...)[0]and.shift(), the implementation handles simple array destructuring and last-match cases using.at(-1)or.pop().This PR updates the docs to include those supported patterns and their
find()/findLast()replacements, matching the upstream eslint-plugin-unicorn docs more closely for the cases Oxlint currently implements.Upstream parity mismatch still exists
eslint-plugin-unicornalso documents and detects.filter(fn).slice(-1)[0]-style last-element patterns. Oxlint does not currently detect that pattern, so this PR does not document it. That can be handled as a separate parity follow-up.Tests
cargo fmt -p oxc_lintercargo test -p oxc_linter prefer_array_find -- --nocaptureoxlint -D unicorn/prefer-array-findAI assistance
AI assistance was used to prepare this change. I have reviewed the code and remain responsible for the submission.