-
-
Notifications
You must be signed in to change notification settings - Fork 963
feat(lint): add ignore option to no-unused-variables #9397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dyc3
merged 13 commits into
biomejs:next
from
mvarendorff:feat/no-unused-variables-ignore-pattern
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5fb27c1
feat(lint): add ignorePattern option for noUnusedVariables
mvarendorff a3385de
chore: changeset
mvarendorff e612596
chore: cleanup todos and add missing docs
mvarendorff 1436234
chore: fix docs typo
mvarendorff 85a42a4
feat(lint): Rewrite ignore_pattern to ignore
mvarendorff d014ded
chore: cleanup
mvarendorff 4bcc2b3
chore: update docs string
mvarendorff 90e0d39
chore: shorten to unwrap_or_default()
mvarendorff 40645f8
fix: docs, codestyle, code review
mvarendorff 003d3e8
fix: remove parameters config since that is handled by another rule
mvarendorff f4bd708
[autofix.ci] apply automated fixes
autofix-ci[bot] 571147c
chore: update+fix docs, add test, address review
mvarendorff eeceada
fix: ignore doc codeblock with multiple diagnostics
mvarendorff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Added `ignore` option to the [noUnusedVariables](https://biomejs.dev/linter/rules/no-unused-variables/) rule. The option allows excluding identifiers by providing a list of ignored names. It also allows excluding kinds of identifiers from this rule entirely, which may be useful when loading classes dynamically. | ||
|
|
||
| For example, unused classes as well as all unused variables, functions, etc. called "unused" may be ignored entirely with the following configuration: | ||
|
|
||
| ```json | ||
| { | ||
| "ignore": { | ||
| "*": ["unused"], | ||
| "class": ["*"] | ||
| } | ||
| } | ||
| ``` |
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
2 changes: 2 additions & 0 deletions
2
crates/biome_js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreAllPattern.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // With ignore: { "*": ["Ignored"] }, variables should be reported that don't match the pattern | ||
| const unusedVariable = 0; |
34 changes: 34 additions & 0 deletions
34
...iome_js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreAllPattern.js.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| assertion_line: 155 | ||
| expression: invalidIgnoreAllPattern.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| // With ignore: { "*": ["Ignored"] }, variables should be reported that don't match the pattern | ||
| const unusedVariable = 0; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalidIgnoreAllPattern.js:2:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This variable unusedVariable is unused. | ||
|
|
||
| 1 │ // With ignore: { "*": ["Ignored"] }, variables should be reported that don't match the pattern | ||
| > 2 │ const unusedVariable = 0; | ||
| │ ^^^^^^^^^^^^^^ | ||
| 3 │ | ||
|
|
||
| i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs. | ||
|
|
||
| i Unsafe fix: If this is intentional, prepend unusedVariable with an underscore. | ||
|
|
||
| 1 1 │ // With ignore: { "*": ["Ignored"] }, variables should be reported that don't match the pattern | ||
| 2 │ - const·unusedVariable·=·0; | ||
| 2 │ + const·_unusedVariable·=·0; | ||
| 3 3 │ | ||
|
|
||
|
|
||
| ``` |
13 changes: 13 additions & 0 deletions
13
...js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreAllPattern.options.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", | ||
| "linter": { | ||
| "rules": { | ||
| "correctness": { | ||
| "noUnusedVariables": { | ||
| "level": "error", | ||
| "options": {"ignore": {"*": ["Ignored"]}} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
...s/biome_js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreClassPattern.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // With ignore: { "class": ["ignored"] }, variables should still be reported even if they match the pattern | ||
| const ignored = 0; |
34 changes: 34 additions & 0 deletions
34
...me_js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreClassPattern.js.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| assertion_line: 155 | ||
| expression: invalidIgnoreClassPattern.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| // With ignore: { "class": ["ignored"] }, variables should still be reported even if they match the pattern | ||
| const ignored = 0; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalidIgnoreClassPattern.js:2:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This variable ignored is unused. | ||
|
|
||
| 1 │ // With ignore: { "class": ["ignored"] }, variables should still be reported even if they match the pattern | ||
| > 2 │ const ignored = 0; | ||
| │ ^^^^^^^ | ||
| 3 │ | ||
|
|
||
| i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs. | ||
|
|
||
| i Unsafe fix: If this is intentional, prepend ignored with an underscore. | ||
|
|
||
| 1 1 │ // With ignore: { "class": ["ignored"] }, variables should still be reported even if they match the pattern | ||
| 2 │ - const·ignored·=·0; | ||
| 2 │ + const·_ignored·=·0; | ||
| 3 3 │ | ||
|
|
||
|
|
||
| ``` |
13 changes: 13 additions & 0 deletions
13
..._analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreClassPattern.options.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", | ||
| "linter": { | ||
| "rules": { | ||
| "correctness": { | ||
| "noUnusedVariables": { | ||
| "level": "error", | ||
| "options": {"ignore": {"class": ["ignored"]}} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
...iome_js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreWildcardPattern.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| const unusedVariable = 0; | ||
| function unusedFunction(unusedParameter) {} | ||
| class UnusedClass {} |
64 changes: 64 additions & 0 deletions
64
...js_analyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreWildcardPattern.js.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| assertion_line: 155 | ||
| expression: invalidIgnoreWildcardPattern.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| const unusedVariable = 0; | ||
| function unusedFunction(unusedParameter) {} | ||
| class UnusedClass {} | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalidIgnoreWildcardPattern.js:2:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This variable unusedVariable is unused. | ||
|
|
||
| 1 │ // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| > 2 │ const unusedVariable = 0; | ||
| │ ^^^^^^^^^^^^^^ | ||
| 3 │ function unusedFunction(unusedParameter) {} | ||
| 4 │ class UnusedClass {} | ||
|
|
||
| i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs. | ||
|
|
||
| i Unsafe fix: If this is intentional, prepend unusedVariable with an underscore. | ||
|
|
||
| 1 1 │ // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| 2 │ - const·unusedVariable·=·0; | ||
| 2 │ + const·_unusedVariable·=·0; | ||
| 3 3 │ function unusedFunction(unusedParameter) {} | ||
| 4 4 │ class UnusedClass {} | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalidIgnoreWildcardPattern.js:3:10 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| ! This function unusedFunction is unused. | ||
|
|
||
| 1 │ // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| 2 │ const unusedVariable = 0; | ||
| > 3 │ function unusedFunction(unusedParameter) {} | ||
| │ ^^^^^^^^^^^^^^ | ||
| 4 │ class UnusedClass {} | ||
| 5 │ | ||
|
|
||
| i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs. | ||
|
|
||
| i Unsafe fix: If this is intentional, prepend unusedFunction with an underscore. | ||
|
|
||
| 1 1 │ // With ignore: { "class": ["*"] }, all other language constructs should still be reported if they are unused | ||
| 2 2 │ const unusedVariable = 0; | ||
| 3 │ - function·unusedFunction(unusedParameter)·{} | ||
| 3 │ + function·_unusedFunction(unusedParameter)·{} | ||
| 4 4 │ class UnusedClass {} | ||
| 5 5 │ | ||
|
|
||
|
|
||
| ``` |
13 changes: 13 additions & 0 deletions
13
...alyze/tests/specs/correctness/noUnusedVariables/invalidIgnoreWildcardPattern.options.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", | ||
| "linter": { | ||
| "rules": { | ||
| "correctness": { | ||
| "noUnusedVariables": { | ||
| "level": "error", | ||
| "options": {"ignore": {"class": ["*"]}} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
crates/biome_js_analyze/tests/specs/correctness/noUnusedVariables/validFunctionParameter.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| /* should not generate diagnostics */ | ||
|
|
||
| function a(a: number) {} | ||
| a(1); | ||
|
|
||
| function b({a, b}: {a: number, b: string}) {} | ||
| b({a: 1, b: "2"}); |
16 changes: 16 additions & 0 deletions
16
...biome_js_analyze/tests/specs/correctness/noUnusedVariables/validFunctionParameter.ts.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| assertion_line: 155 | ||
| expression: validFunctionParameter.ts | ||
| --- | ||
| # Input | ||
| ```ts | ||
| /* should not generate diagnostics */ | ||
|
|
||
| function a(a: number) {} | ||
| a(1); | ||
|
|
||
| function b({a, b}: {a: number, b: string}) {} | ||
| b({a: 1, b: "2"}); | ||
|
|
||
| ``` |
13 changes: 13 additions & 0 deletions
13
crates/biome_js_analyze/tests/specs/correctness/noUnusedVariables/validIgnoredPattern.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* should not generate diagnostics */ | ||
|
|
||
| // With ignore: { "*": ["ignored"] }, unused variables named ignored should be ignored | ||
| const ignored = 1; | ||
| const secondVar = 2; | ||
| console.log(secondVar); | ||
|
|
||
| // With ignore: { "class": ["IgnoredClass"] }, unused classes named IgnoredClass should be ignored | ||
| class IgnoredClass {} | ||
|
|
||
| // With ignore: { "function": ["*"] }, all unused functions should be ignored | ||
| function unusedFunction() {} | ||
| function anotherUnusedFunction() {} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.