From f07d7857652521a9c3ec3fd08052221803504250 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sat, 6 Aug 2022 21:53:30 +0900 Subject: [PATCH 1/2] fix(eslint-plugin): [no-extra-parens] handle await with type assertion --- packages/eslint-plugin/src/rules/no-extra-parens.ts | 13 +++++++++++++ .../tests/rules/no-extra-parens.test.ts | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-extra-parens.ts b/packages/eslint-plugin/src/rules/no-extra-parens.ts index 24659aedfe4..c6f4a0e3518 100644 --- a/packages/eslint-plugin/src/rules/no-extra-parens.ts +++ b/packages/eslint-plugin/src/rules/no-extra-parens.ts @@ -123,6 +123,19 @@ export default util.createRule({ }, // AssignmentExpression // AwaitExpression + AwaitExpression(node) { + if (util.isTypeAssertion(node.argument)) { + // reduces the precedence of the node so the rule thinks it needs to be rapped + return rules.AwaitExpression({ + ...node, + argument: { + ...node.argument, + type: AST_NODE_TYPES.SequenceExpression as any, + }, + }); + } + return rules.AwaitExpression(node); + }, BinaryExpression: binaryExp, CallExpression: callExp, // ClassDeclaration diff --git a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts index 5f5dc37896f..61f9bb9fcce 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts @@ -19,6 +19,7 @@ ruleTester.run('no-extra-parens', rule, { valid: [ ...batchedSingleLineTests({ code: ` +async function f(arg: any) { await (arg as Promise); } (0).toString(); (function(){}) ? a() : b(); (/^a$/).test(x); @@ -238,6 +239,8 @@ for (a of (b)); typeof (a); a((1)); new a((1)); +a<(A)>((1)); +async function f(arg: any) { await ((arg as Promise)); } `, output: ` a = b * c; @@ -248,7 +251,8 @@ for (a of b); typeof a; a(1); new a(1); -a<(A)>((1)); +a<(A)>(1); +async function f(arg: any) { await (arg as Promise); } `, errors: [ { @@ -296,6 +300,11 @@ a<(A)>((1)); line: 10, column: 8, }, + { + messageId: 'unexpected', + line: 11, + column: 37, + }, ], }), ...batchedSingleLineTests({ From dc3656dbcdaed720abfb4fa979a52fe64cce7538 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sun, 7 Aug 2022 19:26:07 +0900 Subject: [PATCH 2/2] apply review --- packages/eslint-plugin/src/rules/no-extra-parens.ts | 5 ++--- .../eslint-plugin/tests/rules/no-extra-parens.test.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-extra-parens.ts b/packages/eslint-plugin/src/rules/no-extra-parens.ts index c6f4a0e3518..71bfc4f10dc 100644 --- a/packages/eslint-plugin/src/rules/no-extra-parens.ts +++ b/packages/eslint-plugin/src/rules/no-extra-parens.ts @@ -122,10 +122,9 @@ export default util.createRule({ } }, // AssignmentExpression - // AwaitExpression AwaitExpression(node) { if (util.isTypeAssertion(node.argument)) { - // 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.AwaitExpression({ ...node, argument: { @@ -161,7 +160,7 @@ export default util.createRule({ }); } 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: { diff --git a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts index 61f9bb9fcce..5a72b6015a2 100644 --- a/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts +++ b/packages/eslint-plugin/tests/rules/no-extra-parens.test.ts @@ -20,6 +20,7 @@ ruleTester.run('no-extra-parens', rule, { ...batchedSingleLineTests({ code: ` async function f(arg: any) { await (arg as Promise); } +async function f(arg: Promise) { await arg; } (0).toString(); (function(){}) ? a() : b(); (/^a$/).test(x); @@ -240,6 +241,7 @@ typeof (a); a((1)); new a((1)); a<(A)>((1)); +async function f(arg: Promise) { await (arg); } async function f(arg: any) { await ((arg as Promise)); } `, output: ` @@ -252,6 +254,7 @@ typeof a; a(1); new a(1); a<(A)>(1); +async function f(arg: Promise) { await arg; } async function f(arg: any) { await (arg as Promise); } `, errors: [ @@ -303,6 +306,11 @@ async function f(arg: any) { await (arg as Promise); } { messageId: 'unexpected', line: 11, + column: 45, + }, + { + messageId: 'unexpected', + line: 12, column: 37, }, ],