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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/src/rules/no-unused-vars.md
Expand Up @@ -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!


Examples of **incorrect** code for the `{ "ignoreClassWithStaticInitBlock": true }` option

::: incorrect

```js
/*eslint no-unused-vars: ["error", { "ignoreClassWithStaticInitBlock": true }]*/

class Foo {
static myProperty = "some string";
static mymethod() {
return "some string";
}
}

class Bar {
static {
let baz; // unused variable
}
}
```

:::

Examples of **correct** code for the `{ "ignoreClassWithStaticInitBlock": true }` option

::: correct

```js
/*eslint no-unused-vars: ["error", { "ignoreClassWithStaticInitBlock": true }]*/

class Foo {
static {
let bar = "some string";

console.log(bar);
}
}
```

:::

## When Not To Use It

If you don't want to be notified about unused variables or function arguments, you can safely turn this rule off.
15 changes: 14 additions & 1 deletion lib/rules/no-unused-vars.js
Expand Up @@ -70,6 +70,9 @@ module.exports = {
},
destructuredArrayIgnorePattern: {
type: "string"
},
ignoreClassWithStaticInitBlock: {
type: "boolean"
}
},
additionalProperties: false
Expand All @@ -92,7 +95,8 @@ module.exports = {
vars: "all",
args: "after-used",
ignoreRestSiblings: false,
caughtErrors: "all"
caughtErrors: "all",
ignoreClassWithStaticInitBlock: false
};

const firstOption = context.options[0];
Expand All @@ -105,6 +109,7 @@ module.exports = {
config.args = firstOption.args || config.args;
config.ignoreRestSiblings = firstOption.ignoreRestSiblings || config.ignoreRestSiblings;
config.caughtErrors = firstOption.caughtErrors || config.caughtErrors;
config.ignoreClassWithStaticInitBlock = firstOption.ignoreClassWithStaticInitBlock || config.ignoreClassWithStaticInitBlock;

if (firstOption.varsIgnorePattern) {
config.varsIgnorePattern = new RegExp(firstOption.varsIgnorePattern, "u");
Expand Down Expand Up @@ -613,6 +618,14 @@ module.exports = {
continue;
}

if (type === "ClassName") {
const hasStaticBlock = def.node.body.body.some(node => node.type === "StaticBlock");

if (config.ignoreClassWithStaticInitBlock && hasStaticBlock) {
continue;
}
}

// skip catch variables
if (type === "CatchClause") {
if (config.caughtErrors === "none") {
Expand Down
54 changes: 54 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -445,6 +445,23 @@ ruleTester.run("no-unused-vars", rule, {
{
code: "var a; a ??= 1;",
languageOptions: { ecmaVersion: 2021 }
},

// ignore class with static initialization block https://github.com/eslint/eslint/issues/17772
{
code: "class Foo { static {} }",
options: [{ ignoreClassWithStaticInitBlock: true }],
languageOptions: { ecmaVersion: 2022 }
},
{
code: "class Foo { static {} }",
options: [{ ignoreClassWithStaticInitBlock: true, varsIgnorePattern: "^_" }],
languageOptions: { ecmaVersion: 2022 }
},
{
code: "class Foo { static {} }",
options: [{ ignoreClassWithStaticInitBlock: false, varsIgnorePattern: "^Foo" }],
languageOptions: { ecmaVersion: 2022 }
}
],
invalid: [
Expand Down Expand Up @@ -1557,6 +1574,43 @@ function foo1() {
c = foo1`,
languageOptions: { ecmaVersion: 2020 },
errors: [{ ...assignedError("c"), line: 10, column: 1 }]
},

// ignore class with static initialization block https://github.com/eslint/eslint/issues/17772
{
code: "class Foo { static {} }",
options: [{ ignoreClassWithStaticInitBlock: false }],
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("Foo"), line: 1, column: 7 }]
},
{
code: "class Foo { static {} }",
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("Foo"), line: 1, column: 7 }]
},
{
code: "class Foo { static { var bar; } }",
options: [{ ignoreClassWithStaticInitBlock: true }],
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("bar"), line: 1, column: 26 }]
},
{
code: "class Foo {}",
options: [{ ignoreClassWithStaticInitBlock: true }],
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("Foo"), line: 1, column: 7 }]
},
{
code: "class Foo { static bar; }",
options: [{ ignoreClassWithStaticInitBlock: true }],
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("Foo"), line: 1, column: 7 }]
},
{
code: "class Foo { static bar() {} }",
options: [{ ignoreClassWithStaticInitBlock: true }],
languageOptions: { ecmaVersion: 2022 },
errors: [{ ...definedError("Foo"), line: 1, column: 7 }]
}
]
});