Skip to content

fix(eslint-plugin): [no-extra-parens] handle type assertion in extends clause #5901

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

Merged
merged 5 commits into from
Oct 30, 2022
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
26 changes: 24 additions & 2 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,30 @@ export default util.createRule<Options, MessageIds>({
},
BinaryExpression: binaryExp,
CallExpression: callExp,
// ClassDeclaration
// ClassExpression
ClassDeclaration(node) {
if (node.superClass?.type === AST_NODE_TYPES.TSAsExpression) {
return rules.ClassDeclaration({
...node,
superClass: {
...node.superClass,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}
return rules.ClassDeclaration(node);
},
ClassExpression(node) {
if (node.superClass?.type === AST_NODE_TYPES.TSAsExpression) {
return rules.ClassExpression({
...node,
superClass: {
...node.superClass,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}
return rules.ClassExpression(node);
},
ConditionalExpression(node) {
// reduces the precedence of the node so the rule thinks it needs to be wrapped
if (util.isTypeAssertion(node.test)) {
Expand Down
32 changes: 32 additions & 0 deletions packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ t.true((me.get as SinonStub).calledWithExactly('/foo', other));
t.true((<SinonStub>me.get).calledWithExactly('/foo', other));
(requestInit.headers as Headers).get('Cookie');
(<Headers> requestInit.headers).get('Cookie');
class Foo {}
class Foo extends (Bar as any) {}
const foo = class {};
const foo = class extends (Bar as any) {}
`,
parserOptions: {
ecmaFeatures: {
Expand Down Expand Up @@ -254,6 +258,10 @@ new a<import('')>((1));
a<(A)>((1));
async function f(arg: Promise<any>) { await (arg); }
async function f(arg: any) { await ((arg as Promise<void>)); }
class Foo extends ((Bar as any)) {}
class Foo extends (Bar) {}
const foo = class extends ((Bar as any)) {}
const foo = class extends (Bar) {}
`,
output: `
a = b * c;
Expand All @@ -267,6 +275,10 @@ new a<import('')>(1);
a<(A)>(1);
async function f(arg: Promise<any>) { await arg; }
async function f(arg: any) { await (arg as Promise<void>); }
class Foo extends (Bar as any) {}
class Foo extends Bar {}
const foo = class extends (Bar as any) {}
const foo = class extends Bar {}
`,
errors: [
{
Expand Down Expand Up @@ -324,6 +336,26 @@ async function f(arg: any) { await (arg as Promise<void>); }
line: 12,
column: 37,
},
{
messageId: 'unexpected',
line: 13,
column: 20,
},
{
messageId: 'unexpected',
line: 14,
column: 19,
},
{
messageId: 'unexpected',
line: 15,
column: 28,
},
{
messageId: 'unexpected',
line: 16,
column: 27,
},
],
}),
...batchedSingleLineTests({
Expand Down