Skip to content

Commit

Permalink
feat: add ignoreClassWithStaticInitBlock option to no-unused-vars (
Browse files Browse the repository at this point in the history
…#18170)

* feat: add option to ignore SIB-classes

* add docs and tests

* add test

* update docs
  • Loading branch information
Tanujkanti4441 committed Mar 7, 2024
1 parent d961eeb commit 1c173dc
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
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 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.

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 }]
}
]
});

0 comments on commit 1c173dc

Please sign in to comment.