From 9c713bb6e7efdb33f8206889e2dbc6d7c003f5d9 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 8 Dec 2022 05:34:30 +0800 Subject: [PATCH 1/3] fix --- packages/babel-generator/src/node/parentheses.ts | 13 ++++++++++--- .../typescript/cast-need-parentheses/input.js | 3 --- .../typescript/cast-need-parentheses/input.ts | 6 ++++++ .../typescript/cast-need-parentheses/output.js | 4 +++- 4 files changed, 19 insertions(+), 7 deletions(-) delete mode 100644 packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js create mode 100644 packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts 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 deleted file mode 100644 index 4c924ac9b0b8..000000000000 --- a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js +++ /dev/null @@ -1,3 +0,0 @@ -( x).y; -(x as T).y; -x!.y; diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts new file mode 100644 index 000000000000..64f70d1291d3 --- /dev/null +++ b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts @@ -0,0 +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 From 2b6aaacb96bdc24cdc2dae5fc8f41d6c57cab7d9 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 8 Dec 2022 05:44:45 +0800 Subject: [PATCH 2/3] update tests --- .../transparent-expr-wrappers/ts-as-in-conditional/output.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; }; From 26e195edabcb47a9fecaf0ee3f9d6e2fc32c630a Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Thu, 8 Dec 2022 06:39:08 +0800 Subject: [PATCH 3/3] revert --- .../typescript/cast-need-parentheses/{input.ts => input.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/{input.ts => input.js} (100%) diff --git a/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts b/packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js similarity index 100% rename from packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.ts rename to packages/babel-generator/test/fixtures/typescript/cast-need-parentheses/input.js