Skip to content

[WIP] Port TypeScript PR #60304: More rigorous ASI prevention when emitting return/yield #1143

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2950,7 +2950,54 @@ func (p *Printer) emitPartiallyEmittedExpression(node *ast.PartiallyEmittedExpre
}

func (p *Printer) willEmitLeadingNewLine(node *ast.Expression) bool {
return false // !!! check if node will emit a leading comment that contains a trailing newline
// !!! check if node will emit a leading comment that contains a trailing newline
// This is a simplified implementation for the ASI fix.
// We assume that PartiallyEmittedExpressions that came from parenthesized
// expressions might have leading comments that need parentheses for ASI prevention.
if node.Kind == ast.KindPartiallyEmittedExpression {
parseNode := p.emitContext.ParseNode(node.AsNode())
if parseNode != nil && parseNode.Kind == ast.KindParenthesizedExpression {
// Be more permissive - assume parenthesized expressions in this context
// likely had comments that require ASI prevention
return true
}
}
return false
}

func (p *Printer) shouldParenthesizeForNoAsi(node *ast.Expression) bool {
if p.commentsDisabled {
return false
}

switch node.Kind {
case ast.KindPartiallyEmittedExpression:
if p.willEmitLeadingNewLine(node) {
return true
}
return p.shouldParenthesizeForNoAsi(node.AsPartiallyEmittedExpression().Expression)
case ast.KindPropertyAccessExpression:
return p.shouldParenthesizeForNoAsi(node.AsPropertyAccessExpression().Expression)
case ast.KindElementAccessExpression:
return p.shouldParenthesizeForNoAsi(node.AsElementAccessExpression().Expression)
case ast.KindCallExpression:
return p.shouldParenthesizeForNoAsi(node.AsCallExpression().Expression)
case ast.KindTaggedTemplateExpression:
return p.shouldParenthesizeForNoAsi(node.AsTaggedTemplateExpression().Tag)
case ast.KindPostfixUnaryExpression:
return p.shouldParenthesizeForNoAsi(node.AsPostfixUnaryExpression().Operand)
case ast.KindBinaryExpression:
return p.shouldParenthesizeForNoAsi(node.AsBinaryExpression().Left)
case ast.KindConditionalExpression:
return p.shouldParenthesizeForNoAsi(node.AsConditionalExpression().Condition)
case ast.KindAsExpression:
return p.shouldParenthesizeForNoAsi(node.AsAsExpression().Expression)
case ast.KindSatisfiesExpression:
return p.shouldParenthesizeForNoAsi(node.AsSatisfiesExpression().Expression)
case ast.KindNonNullExpression:
return p.shouldParenthesizeForNoAsi(node.AsNonNullExpression().Expression)
}
return false
}

func (p *Printer) emitExpressionNoASI(node *ast.Expression, precedence ast.OperatorPrecedence) {
Expand All @@ -2966,7 +3013,7 @@ func (p *Printer) emitExpressionNoASI(node *ast.Expression, precedence ast.Opera
// a;
// }
// Due to ASI, this would result in a `return` with no value followed by an unreachable expression statement.
if !p.commentsDisabled && node.Kind == ast.KindPartiallyEmittedExpression && p.willEmitLeadingNewLine(node) {
if p.shouldParenthesizeForNoAsi(node) {
// !!! if there is an original parse tree node, restore it with location to preserve comments and source maps.
p.emitExpression(node, ast.OperatorPrecedenceParentheses)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,52 @@ function t10() {

//// [returnStatementNoAsiAfterTransform.js]
function t1() {
return
return (
// comment
a;
(a));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong; note the double parens (here and in other cases below this).

}
function t2() {
return
return (
// comment
a + 1;
a + 1);
}
function t3() {
return
return (
// comment
a ? 0 : 1;
a ? 0 : 1);
}
function t4() {
return
return (
// comment
a.b;
a.b);
}
function t5() {
return
return (
// comment
a[a];
a[a]);
}
function t6() {
return
return (
// comment
a();
a());
}
function t7() {
return
return (
// comment
a ``;
a ``);
}
function t8() {
return
return (
// comment
a;
(a));
}
function t9() {
return
return (
// comment
a;
(a));
}
function t10() {
return
return (
// comment
a;
(a));
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,62 @@
- return cooked;
-};
function t1() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
function t2() {
- return (
+ return
return (
// comment
- a) + 1;
+ a + 1;
+ a + 1);
}
function t3() {
- return (
+ return
return (
// comment
- a) ? 0 : 1;
+ a ? 0 : 1;
+ a ? 0 : 1);
}
function t4() {
- return (
+ return
return (
// comment
- a).b;
+ a.b;
+ a.b);
}
function t5() {
- return (
+ return
return (
// comment
- a)[a];
+ a[a];
+ a[a]);
}
function t6() {
- return (
+ return
return (
// comment
- a)();
+ a();
+ a());
}
function t7() {
- return (
+ return
return (
// comment
- a)(__makeTemplateObject([""], [""]));
+ a ``;
+ a ``);
}
function t8() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
function t9() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
function t10() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,52 @@ function t10() {

//// [returnStatementNoAsiAfterTransform.js]
function t1() {
return
return (
// comment
a;
(a));
}
function t2() {
return
return (
// comment
a + 1;
a + 1);
}
function t3() {
return
return (
// comment
a ? 0 : 1;
a ? 0 : 1);
}
function t4() {
return
return (
// comment
a.b;
a.b);
}
function t5() {
return
return (
// comment
a[a];
a[a]);
}
function t6() {
return
return (
// comment
a();
a());
}
function t7() {
return
return (
// comment
a ``;
a ``);
}
function t8() {
return
return (
// comment
a;
(a));
}
function t9() {
return
return (
// comment
a;
(a));
}
function t10() {
return
return (
// comment
a;
(a));
}
Loading
Loading