Skip to content

Commit 67cb9f1

Browse files
authored
fix: Correctly generate (a ?? b) as T (#15258)
* fix * update tests * revert
1 parent b0878e0 commit 67cb9f1

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

  • packages
    • babel-generator
    • babel-plugin-proposal-optional-chaining/test/fixtures/transparent-expr-wrappers/ts-as-in-conditional

‎packages/babel-generator/src/node/parentheses.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ const enum CheckParam {
9090
forOfHead = 1 << 5,
9191
}
9292

93+
function isTSTypeExpression(node: t.Node) {
94+
return (
95+
isTSAsExpression(node) ||
96+
isTSSatisfiesExpression(node) ||
97+
isTSTypeAssertion(node)
98+
);
99+
}
100+
93101
const isClassExtendsClause = (
94102
node: t.Node,
95103
parent: t.Node,
@@ -369,9 +377,7 @@ export function ConditionalExpression(
369377
isBinary(parent) ||
370378
isConditionalExpression(parent, { test: node }) ||
371379
isAwaitExpression(parent) ||
372-
isTSTypeAssertion(parent) ||
373-
isTSAsExpression(parent) ||
374-
isTSSatisfiesExpression(parent)
380+
isTSTypeExpression(parent)
375381
) {
376382
return true;
377383
}
@@ -406,6 +412,7 @@ export function LogicalExpression(
406412
node: t.LogicalExpression,
407413
parent: t.Node,
408414
): boolean {
415+
if (isTSTypeExpression(parent)) return true;
409416
switch (node.operator) {
410417
case "||":
411418
if (!isLogicalExpression(parent)) return false;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
(<T> x).y;
22
(x as T).y;
33
x!.y;
4+
5+
(point1 ?? point2 as Point);
6+
(point1 ?? point2) as Point;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
(<T> x).y;
22
(x as T).y;
3-
x!.y;
3+
x!.y;
4+
point1 ?? (point2 as Point);
5+
((point1 ?? point2) as Point);
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
a => {
22
var _a$c;
3-
((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;
3+
((((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;
44
};

0 commit comments

Comments
 (0)