Skip to content
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

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

Merged
merged 3 commits into from Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
},
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