Skip to content

Commit

Permalink
fix: Correctly generate (a ?? b) as T (#15258)
Browse files Browse the repository at this point in the history
* fix

* update tests

* revert
  • Loading branch information
liuxingbaoyu committed Dec 8, 2022
1 parent b0878e0 commit 67cb9f1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
13 changes: 10 additions & 3 deletions packages/babel-generator/src/node/parentheses.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
@@ -1,3 +1,6 @@
(<T> x).y;
(x as T).y;
x!.y;

(point1 ?? point2 as Point);
(point1 ?? point2) as Point;
@@ -1,3 +1,5 @@
(<T> x).y;
(x as T).y;
x!.y;
x!.y;
point1 ?? (point2 as Point);
((point1 ?? point2) as Point);
@@ -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;
};

0 comments on commit 67cb9f1

Please sign in to comment.