Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/eslint-plugin/src/rules/no-unused-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type Options = [
caughtErrors?: 'all' | 'none';
caughtErrorsIgnorePattern?: string;
destructuredArrayIgnorePattern?: string;
ignoreClassWithStaticInitBlock?: boolean;
reportUsedIgnorePattern?: boolean;
},
];
Expand All @@ -42,6 +43,7 @@ interface TranslatedOptions {
caughtErrors: 'all' | 'none';
caughtErrorsIgnorePattern?: RegExp;
destructuredArrayIgnorePattern?: RegExp;
ignoreClassWithStaticInitBlock: boolean;
reportUsedIgnorePattern: boolean;
}

Expand Down Expand Up @@ -97,6 +99,9 @@ export default createRule<Options, MessageIds>({
destructuredArrayIgnorePattern: {
type: 'string',
},
ignoreClassWithStaticInitBlock: {
type: 'boolean',
},
reportUsedIgnorePattern: {
type: 'boolean',
},
Expand All @@ -122,6 +127,7 @@ export default createRule<Options, MessageIds>({
args: 'after-used',
ignoreRestSiblings: false,
caughtErrors: 'all',
ignoreClassWithStaticInitBlock: false,
reportUsedIgnorePattern: false,
};

Expand All @@ -133,6 +139,9 @@ export default createRule<Options, MessageIds>({
options.ignoreRestSiblings =
firstOption.ignoreRestSiblings ?? options.ignoreRestSiblings;
options.caughtErrors = firstOption.caughtErrors ?? options.caughtErrors;
options.ignoreClassWithStaticInitBlock =
firstOption.ignoreClassWithStaticInitBlock ??
options.ignoreClassWithStaticInitBlock;
options.reportUsedIgnorePattern =
firstOption.reportUsedIgnorePattern ??
options.reportUsedIgnorePattern;
Expand Down Expand Up @@ -423,6 +432,16 @@ export default createRule<Options, MessageIds>({
continue;
}

if (def.type === TSESLint.Scope.DefinitionType.ClassName) {
const hasStaticBlock = def.node.body.body.some(
node => node.type === AST_NODE_TYPES.StaticBlock,
);

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

// skip catch variables
if (def.type === TSESLint.Scope.DefinitionType.CatchClause) {
if (options.caughtErrors === 'none') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,39 @@ console.log(a + c);
],
parserOptions: { ecmaVersion: 6 },
},

// ignore class with static initialization block https://github.com/eslint/eslint/issues/17772
{
code: `
class Foo {
static {}
}
`,
options: [{ ignoreClassWithStaticInitBlock: true }],
parserOptions: { ecmaVersion: 2022 },
},
{
code: `
class Foo {
static {}
}
`,
options: [
{ ignoreClassWithStaticInitBlock: true, varsIgnorePattern: '^_' },
],
parserOptions: { ecmaVersion: 2022 },
},
{
code: `
class Foo {
static {}
}
`,
options: [
{ ignoreClassWithStaticInitBlock: false, varsIgnorePattern: '^Foo' },
],
parserOptions: { ecmaVersion: 2022 },
},
],
invalid: [
{
Expand Down Expand Up @@ -2965,5 +2998,64 @@ try {
],
errors: [usedIgnoredError('_err', '. Used args must not match /^_/u')],
},

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.