Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly generate (a ?? b) as T #15258

Merged
merged 3 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
};