From 67cb9f189aac56c5f7ef09088f9f3ee075e1e639 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 8 Dec 2022 16:40:46 +0800 Subject: [PATCH] fix: Correctly generate `(a ?? b) as T` (#15258) * fix * update tests * revert --- packages/babel-generator/src/node/parentheses.ts | 13 ++++++++++--- .../typescript/cast-need-parentheses/input.js | 3 +++ .../typescript/cast-need-parentheses/output.js | 4 +++- .../ts-as-in-conditional/output.js | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/babel-generator/src/node/parentheses.ts b/packages/babel-generator/src/node/parentheses.ts index 2e269d8355a6..94b4f95c3d59 100644 --- a/packages/babel-generator/src/node/parentheses.ts +++ b/packages/babel-generator/src/node/parentheses.ts @@ -90,6 +90,14 @@ const enum CheckParam { forOfHead = 1 << 5, } +function isTSTypeExpression(node: t.Node) { + return ( + isTSAsExpression(node) || + isTSSatisfiesExpression(node) || + isTSTypeAssertion(node) + ); +} + const isClassExtendsClause = ( node: t.Node, parent: t.Node, @@ -369,9 +377,7 @@ export function ConditionalExpression( isBinary(parent) || isConditionalExpression(parent, { test: node }) || isAwaitExpression(parent) || - isTSTypeAssertion(parent) || - isTSAsExpression(parent) || - isTSSatisfiesExpression(parent) + isTSTypeExpression(parent) ) { return true; } @@ -406,6 +412,7 @@ export function LogicalExpression( node: t.LogicalExpression, parent: t.Node, ): boolean { + if (isTSTypeExpression(parent)) return true; switch (node.operator) { case "||": if (!isLogicalExpression(parent)) return false; diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js index 4c924ac9b0b8..64f70d1291d3 100644 --- a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js +++ b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js @@ -1,3 +1,6 @@ ( x).y; (x as T).y; x!.y; + +(point1 ?? point2 as Point); +(point1 ?? point2) as Point; diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js index ec263a68cde1..1da451b58465 100644 --- a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js +++ b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/output.js @@ -1,3 +1,5 @@ ( x).y; (x as T).y; -x!.y; \ No newline at end of file +x!.y; +point1 ?? (point2 as Point); +((point1 ?? point2) as Point); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js index 02ecb63af4f0..fd699a6166fa 100644 --- a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional/output.js @@ -1,4 +1,4 @@ a => { var _a$c; - ((a !== null && a !== void 0 && a.b as any) && (_a$c = a.c) !== null && _a$c !== void 0 && _a$c.d as any) ? 0 : 1; + ((((a !== null && a !== void 0 && a.b) as any) && (_a$c = a.c) !== null && _a$c !== void 0 && _a$c.d) as any) ? 0 : 1; };