Skip to content

Commit

Permalink
fix(eslint-plugin): [no-extra-parens] handle type assertion in extend…
Browse files Browse the repository at this point in the history
…s clause (#5901)

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

* Add test cases

* Handle class expression

* Add test cases
  • Loading branch information
yeonjuan committed Oct 30, 2022
1 parent 891b087 commit 8ed7219
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
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
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

0 comments on commit 8ed7219

Please sign in to comment.