feat(linter/no-unused-vars): add fixer to remove unused catch bindings#17567
Conversation
CodSpeed Performance ReportMerging #17567 will not alter performanceComparing Summary
Footnotes
|
There was a problem hiding this comment.
Pull request overview
This PR adds an autofix feature to the no-unused-vars linter rule for removing unused catch bindings. When a catch parameter is declared but never used, the fixer will safely remove it by transforming catch (e) { } into catch { }.
Key changes:
- Added
find_prev_token_fromhelper method to search backwards for tokens while skipping comments - Implemented autofix logic for unused catch parameters that deletes the parentheses and parameter
- Added comprehensive test coverage for various scenarios including different spacing, comments, TypeScript type annotations, and destructuring patterns
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
crates/oxc_linter/src/context/mod.rs |
Added find_prev_token_from helper method to search backwards for tokens, complementing the existing find_next_token_from method |
crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs |
Implemented autofix logic for CatchParameter that finds and removes the surrounding parentheses and unused parameter |
crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs |
Added 15 test cases covering various scenarios and enabled fix expectations for the catch parameter tests |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
# Oxlint ### 💥 BREAKING CHANGES - f7da875 oxlint: [**BREAKING**] Remove oxc_language_server binary (#17457) (Boshen) ### 🚀 Features - 659c23e linter: Init note field boilerplate (#17589) (Shrey Sudhir) - 6870b64 parser: Add TS1363 error code (#17609) (Sysix) - 6154c8c linter/eslint-plugin-vitest: Implemented vitest/warn-todo rule (#17228) (Said Atrahouch) - 0043cd6 linter/eslint-plugin-vitest: Implement consistent-vitest-vi rule (#17389) (Said Atrahouch) - a6d773d linter: Add full TS support to eslint/no-useless-constructor (#17592) (camc314) - f02c0e7 linter/eslint: Implement complexity (#17569) (Nguyen Tran) - bc7aae7 linter/no-unused-vars: Add fixer to remove unused catch bindings (#17567) (Don Isaac) - 9e8ec78 linter/only-throw-error rule: Add `allowRethrowing` option for (#17554) (camc314) - b67e819 linter: Add fixer for `unicorn/prefer-response-static-json` rule (#17559) (Mikhail Baev) - 44b0361 linter/vue: Implement no-this-in-before-route-enter (#17525) (yefan) - ee34716 linter/react: Implement no-will-update-set-state (#17530) (Kenzo Wada) - 3088e1d linter/react: Implement no-this-in-sfc (#17535) (Kenzo Wada) - 29a2868 linter/jsx-a11y: Implement no-static-element-interactions (#17538) (Kenzo Wada) - eadf057 linter: Enable tsconfig auto discovery by default (#17489) (Boshen) - 12a7d6e website_linter: Add a count of rules with fixes available to rules table. (#17476) (Connor Shea) ### 🐛 Bug Fixes - a702f13 oxlint/lsp: Correct position for "disable for this file" with shebang (#17613) (Sysix) - 19fdfb6 linter: Panic in `sort-keys` rule with Unicode numeric characters (#17629) (Adel Rodríguez) - 2e8f469 vscode: Search for `node_modules/.bin/oxlint.exe` too (bun setup) (#17597) (Sysix) - be39906 linter/aria-proptypes: Allow template literals with expressions for string-type ARIA props (#17460) (Jökull Sólberg Auðunsson) - 529901c linter: Include JS plugin rules when calculating total rule count (#17520) (connorshea) - 96ef2cc linter: Print total rule # when using a single nested config (#17517) (connorshea) - 9ad0f29 oxlint: Do not enable external plugin store when no external linter is passed (#17498) (Sysix) - 174375d oxfmt,oxlint: Disable mimalloc for 32-bit Arm targets (#17473) (Yaksh Bariya) - ff70fe9 linter/no-standalone-expect: Allows expect in wrapper functions passed to test blocks (#17427) (Copilot) - dab232f linter/catch-or-return: Handle arrow functions with implicit returns correctly (#17440) (Copilot) - a38892a linter: Update no-unnecessary-template-expression docs and test case (#17453) (camc314) ### ⚡ Performance - 605dbf1 vscode: Restrict searching for oxlint/oxfmt binaries only 3 levels deep + 10s timeout (#17345) (Sysix) ### 📚 Documentation - 884fb63 linter/react: Improve docs for jsx-curly-brace-presence (#17579) (connorshea) - 1d3ee07 linter: Improve rule explanation for `vue/no-this-in-before-route-enter`. (#17581) (connorshea) - 5f189f8 linter/arrow-body-style: Correctly document default mode option (#17566) (Rägnar O'ock) - bb2e8e4 linter: Add a note to the `typescript/no-var-requires` rule about the missing `allow` option (#17551) (connorshea) - 655afc1 linter: Improve docs for `import/extensions` and add a few more tests (#17539) (connorshea) - 7e5fc90 linter: Update list of plugins that are reserved. (#17516) (connorshea) # Oxfmt ### 💥 BREAKING CHANGES - f7da875 oxlint: [**BREAKING**] Remove oxc_language_server binary (#17457) (Boshen) ### 🚀 Features - 8fd4ea9 oxfmt: `options.embeddedLanguageFormatting` is now `"auto"` by default (#17649) (leaysgur) ### 🐛 Bug Fixes - c9b5d7d formatter/sort_imports: Handle alignable_comment correctly (#17646) (leaysgur) - 453222d formatter: Missing comment handling for end-of-line comments in member chains (#17659) (Dunqing) - 0805ff2 formatter: Incorrect inline comment placement in try-catch (#17657) (Dunqing) - 3a0c782 formatter: Don't move comments into optional call parentheses (#17582) (magic-akari) - 174375d oxfmt,oxlint: Disable mimalloc for 32-bit Arm targets (#17473) (Yaksh Bariya) ### ⚡ Performance - abb28dc oxfmt: Turn of pretty print from sort-package-json (#17452) (Boshen)
Part of #16832. This PR adds a dangerous suggestion for unused function parameters in `no-unused-vars`, renaming them to the ignored `_name` form. For example, with `args: "all"` and `argsIgnorePattern: "^_"`: ```js function f(unused) {} ``` can be fixed with `--fix-dangerously` to: ```js function f(_unused) {} ``` It intentionally only covers simple function parameters. Catch bindings are already handled by #17567, and the remaining fixer categories from #16832 are left for follow-up PRs. This PR does not cover default parameters, destructuring parameters, rest bindings, type parameters, or multi-declarator cleanup. ## Manual verification I also verified the change against the actual `oxlint` CLI. Input: ```js function decl(unused, used) { console.log(used); } const expr = function(unused) { return 1; }; const arrow = (unused) => 1; const obj = { method(unused) { return 1; } }; function conflict(unused, _unused) { console.log(_unused); } ``` After running `oxlint --fix-dangerously` with `no-unused-vars` configured as `{ "args": "all", "argsIgnorePattern": "^_" }`: ```js function decl(_unused, used) { console.log(used); } const expr = function(_unused) { return 1; }; const arrow = (_unused) => 1; const obj = { method(_unused) { return 1; } }; function conflict(_unused0, _unused) { console.log(_unused); } ``` I also verified that `--fix-suggestions` does not apply this dangerous suggestion, and that default/destructuring parameters are left unchanged. ## Reviewer note The existing diagnostic help for unused parameters still says: ```text Consider removing this parameter. ``` This PR keeps that wording unchanged and only adds the dangerous rename suggestion. I can adjust the help text in a follow-up if maintainers prefer. ## AI usage AI assistance was used for implementation support, code navigation, test planning, manual verification, and self-review. I reviewed, tested, and take responsibility for the submitted changes. Original prompt / instructions given to AI were in Japanese. The English version below is an LLM translation for reviewer convenience. ### Original Japanese prompt > `no_unused_vars` の unused function parameter fixer だけを小さなスコープで実装する。 > `function f(unused) {}` のような未使用の関数パラメータに対して、削除ではなく `_unused` 形式へ rename する dangerous suggestion を追加する。 > catch bindings、multi-declarator cleanup、type parameters、rest bindings、destructuring parameters などは今回の PR では扱わない。 > 既存の fixer 実装とテスト構造に合わせ、rename conflict は既存の `_x`, `_x0`, `_x1` のような handling に乗せる。 > function declaration、function expression、arrow function、method parameter のテストを追加する。 ### English translation by LLM > Implement only the unused function parameter fixer for `no_unused_vars` in a small scope. > Add a dangerous suggestion for unused function parameters such as `function f(unused) {}`, renaming them to the ignored `_unused` form instead of deleting them. > Do not cover catch bindings, multi-declarator cleanup, type parameters, rest bindings, destructuring parameters, or other fixer categories in this PR. > Follow the existing fixer implementation and test structure, and reuse the existing rename conflict handling such as `_x`, `_x0`, `_x1`. > Add tests for function declarations, function expressions, arrow functions, and method parameters. --------- Co-authored-by: Cameron <[email protected]>
What This PR Does
Adds an safe-suggestion autofix to
no-unused-varsfor deleting unused catch bindings.TODO
[ ] useOOS, thinking of making another PR to do thismemchrfor SIMD