Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [no-extra-parens] handle await with type assertion (
#5428)

* fix(eslint-plugin): [no-extra-parens] handle await with type assertion

* apply review
  • Loading branch information
yeonjuan committed Aug 7, 2022
1 parent df4e05e commit e03826f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 14 additions & 2 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Expand Up @@ -122,7 +122,19 @@ export default util.createRule<Options, MessageIds>({
}
},
// AssignmentExpression
// AwaitExpression
AwaitExpression(node) {
if (util.isTypeAssertion(node.argument)) {
// reduces the precedence of the node so the rule thinks it needs to be wrapped
return rules.AwaitExpression({
...node,
argument: {
...node.argument,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}
return rules.AwaitExpression(node);
},
BinaryExpression: binaryExp,
CallExpression: callExp,
// ClassDeclaration
Expand All @@ -148,7 +160,7 @@ export default util.createRule<Options, MessageIds>({
});
}
if (util.isTypeAssertion(node.alternate)) {
// reduces the precedence of the node so the rule thinks it needs to be rapped
// reduces the precedence of the node so the rule thinks it needs to be wrapped
return rules.ConditionalExpression({
...node,
alternate: {
Expand Down
19 changes: 18 additions & 1 deletion packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Expand Up @@ -19,6 +19,8 @@ ruleTester.run('no-extra-parens', rule, {
valid: [
...batchedSingleLineTests({
code: `
async function f(arg: any) { await (arg as Promise<void>); }
async function f(arg: Promise<any>) { await arg; }
(0).toString();
(function(){}) ? a() : b();
(/^a$/).test(x);
Expand Down Expand Up @@ -238,6 +240,9 @@ for (a of (b));
typeof (a);
a<import('')>((1));
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>)); }
`,
output: `
a = b * c;
Expand All @@ -248,7 +253,9 @@ for (a of b);
typeof a;
a<import('')>(1);
new a<import('')>(1);
a<(A)>((1));
a<(A)>(1);
async function f(arg: Promise<any>) { await arg; }
async function f(arg: any) { await (arg as Promise<void>); }
`,
errors: [
{
Expand Down Expand Up @@ -296,6 +303,16 @@ a<(A)>((1));
line: 10,
column: 8,
},
{
messageId: 'unexpected',
line: 11,
column: 45,
},
{
messageId: 'unexpected',
line: 12,
column: 37,
},
],
}),
...batchedSingleLineTests({
Expand Down

0 comments on commit e03826f

Please sign in to comment.