Skip to content
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

Merged
merged 4 commits into from Mar 7, 2024

Conversation

Tanujkanti4441
Copy link
Contributor

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:

/* eslint no-unused-vars: "error" */

class ClassWithSIB {
  static {
    // …
  }
}

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 is false

Is there anything you'd like reviewers to focus on?

Fixes: #17772

@Tanujkanti4441 Tanujkanti4441 requested a review from a team as a code owner March 5, 2024 16:22
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Mar 5, 2024
@github-actions github-actions bot added the rule Relates to ESLint's core rules label Mar 5, 2024
Copy link

netlify bot commented Mar 5, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 63839c6
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65e94f283d1add00083a522b
😎 Deploy Preview https://deploy-preview-18170--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@nzakas nzakas left a 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?

@@ -613,6 +618,14 @@ module.exports = {
continue;
}

if (type === "ClassName") {
const hasStaticBlock = def.node.body.body.filter(node => node.type === "StaticBlock");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 5, 2024
Copy link
Member

@nzakas nzakas left a 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.

@@ -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`.
Copy link
Member

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.

Suggested change
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@mdjermanovic mdjermanovic changed the title feat: add option to ignore SIB-classes in no-unused-vars feat: add ignoreClassWithStaticInitBlock option to no-unused-vars Mar 7, 2024
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mdjermanovic mdjermanovic merged commit 1c173dc into eslint:main Mar 7, 2024
19 checks passed
@Tanujkanti4441 Tanujkanti4441 deleted the new-ignore-option branch March 7, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
3 participants