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
feat: add ignoreClassWithStaticInitBlock
option to no-unused-vars
#18170
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Can you also please update the documentation?
lib/rules/no-unused-vars.js
Outdated
@@ -613,6 +618,14 @@ module.exports = { | |||
continue; | |||
} | |||
|
|||
if (type === "ClassName") { | |||
const hasStaticBlock = def.node.body.body.filter(node => node.type === "StaticBlock"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const hasStaticBlock = def.node.body.body.filter(node => node.type === "StaticBlock"); | |
const hasStaticBlock = def.node.body.body.some(node => node.type === "StaticBlock"); |
filter
always returns an array, so this would always be truthy.
/* eslint no-unused-vars: ["error", { ignoreClassWithStaticInitBlock: true }] */
class C {} // false negative
We should also add some tests where this option is true
and class doesn't have static blocks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Would like @mdjermanovic to review before merging.
docs/src/rules/no-unused-vars.md
Outdated
@@ -410,6 +410,51 @@ var bar; | |||
|
|||
::: | |||
|
|||
### ignoreClassWithStaticInitBlock | |||
|
|||
The `ignoreClassWithStaticInitBlock` option is a boolean (default: `false`). Static initialization blocks or Static blocks was introduced in ES2022. Static blocks lets us to initialize static variables and execute code during the evaluation of defined class. It means that one don't need to create an instance of class to run the code written inside the static block. This option ignore the classes with static initialization block when sets to `true`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just cleaning up the language a bit.
The `ignoreClassWithStaticInitBlock` option is a boolean (default: `false`). Static initialization blocks or Static blocks was introduced in ES2022. Static blocks lets us to initialize static variables and execute code during the evaluation of defined class. It means that one don't need to create an instance of class to run the code written inside the static block. This option ignore the classes with static initialization block when sets to `true`. | |
The `ignoreClassWithStaticInitBlock` option is a boolean (default: `false`). Static initialization blocks allow you to initialize static variables and execute code during the evaluation of a class definition, meaning the static block code is executed without creating a new instance of the class. When set to `true`, this option ignores classes containing static initialization blocks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
no-unused-vars
ignoreClassWithStaticInitBlock
option to no-unused-vars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What rule do you want to change?
no-unused-vars
What change do you want to make (place an "X" next to just one item)?
[ ] Generate more warnings
[x] Generate fewer warnings
[ ] Implement autofix
[ ] Implement suggestions
How will the change be implemented (place an "X" next to just one item)?
[x] A new option
[ ] A new default behavior
[ ] Other
Please provide some example code that this change will affect:
What does the rule currently do for this code?
rule reports this code.
What will the rule do after it's changed?
rule will not report the classes as unused that have static initialization block.
What changes did you make? (Give an overview)
added a new option called
ignoreClassWithStaticInitBlock
that is a boolean and by default it isfalse
Is there anything you'd like reviewers to focus on?
Fixes: #17772