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 all 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
50 changes: 49 additions & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2949,8 +2949,56 @@ func (p *Printer) emitPartiallyEmittedExpression(node *ast.PartiallyEmittedExpre
}
}

func (p *Printer) commentWillEmitNewLine(comment ast.CommentRange) bool {
return comment.Kind == ast.KindSingleLineCommentTrivia || comment.HasTrailingNewLine
}

func (p *Printer) willEmitLeadingNewLine(node *ast.Expression) bool {
return false // !!! check if node will emit a leading comment that contains a trailing newline
if p.currentSourceFile == nil {
return false
}

text := p.currentSourceFile.Text()
pos := node.Pos()

// Get leading comment ranges
var leadingCommentRanges []ast.CommentRange
for commentRange := range scanner.GetLeadingCommentRanges(p.emitContext.Factory.AsNodeFactory(), text, pos) {
leadingCommentRanges = append(leadingCommentRanges, commentRange)
}

if len(leadingCommentRanges) > 0 {
parseNode := p.emitContext.ParseNode(node.AsNode())
if parseNode != nil && parseNode.Parent != nil && ast.IsParenthesizedExpression(parseNode.Parent) {
return true
}
}

// Check if any leading comment will emit a newline
for _, comment := range leadingCommentRanges {
if p.commentWillEmitNewLine(comment) {
return true
}
}

// Check synthetic leading comments (currently stubbed out in this codebase)
// if some(getSyntheticLeadingComments(node), commentWillEmitNewLine)) return true;

// Handle PartiallyEmittedExpression recursively
if node.Kind == ast.KindPartiallyEmittedExpression {
partiallyEmitted := node.AsPartiallyEmittedExpression()
if node.Pos() != partiallyEmitted.Expression.Pos() {
// Check trailing comments at the expression position
for trailingComment := range scanner.GetTrailingCommentRanges(p.emitContext.Factory.AsNodeFactory(), text, partiallyEmitted.Expression.Pos()) {
if p.commentWillEmitNewLine(trailingComment) {
return true
}
}
}
return p.willEmitLeadingNewLine(partiallyEmitted.Expression)
}

return false
}

func (p *Printer) emitExpressionNoASI(node *ast.Expression, precedence ast.OperatorPrecedence) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class Foo {
getThing: () => Promise.resolve('')
};
foo() {
return
/* TODO: Avoid using type assertions, please refactor. */ this.client
.getThing();
return (
/* TODO: Avoid using type assertions, please refactor. */ (this.client
.getThing()));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
--- old.commentEmitOnParenthesizedAssertionInReturnStatement.js
+++ new.commentEmitOnParenthesizedAssertionInReturnStatement.js
@@= skipped -20, +20 lines =@@
getThing: () => Promise.resolve('')
@@= skipped -21, +21 lines =@@
};
foo() {
- return (
+ return
/* TODO: Avoid using type assertions, please refactor. */ this.client
return (
- /* TODO: Avoid using type assertions, please refactor. */ this.client
- .getThing());
+ .getThing();
+ /* TODO: Avoid using type assertions, please refactor. */ (this.client
+ .getThing()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class Foo {
getThing: () => Promise.resolve('')
};
foo() {
return
/* TODO: please refactor */ this.client
.getThing();
return (
/* TODO: please refactor */ (this.client
.getThing()));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
--- old.commentEmitOnParenthesizedAssertionInReturnStatement2.js
+++ new.commentEmitOnParenthesizedAssertionInReturnStatement2.js
@@= skipped -18, +18 lines =@@
getThing: () => Promise.resolve('')
@@= skipped -19, +19 lines =@@
};
foo() {
- return (
+ return
/* TODO: please refactor */ this.client
return (
- /* TODO: please refactor */ this.client
- .getThing());
+ .getThing();
+ /* TODO: please refactor */ (this.client
+ .getThing()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ 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
Expand Down Expand Up @@ -102,17 +102,17 @@ function t7() {
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,11 +9,10 @@
- return cooked;
-};
function t1() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
function t2() {
- return (
Expand Down Expand Up @@ -58,23 +57,20 @@
+ 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,9 +67,9 @@ function t10() {

//// [returnStatementNoAsiAfterTransform.js]
function t1() {
return
return (
// comment
a;
(a));
}
function t2() {
return
Expand Down Expand Up @@ -102,17 +102,17 @@ function t7() {
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
@@ -1,14 +1,11 @@
--- old.returnStatementNoAsiAfterTransform(target=esnext).js
+++ new.returnStatementNoAsiAfterTransform(target=esnext).js
@@= skipped -66, +66 lines =@@

//// [returnStatementNoAsiAfterTransform.js]
@@= skipped -68, +68 lines =@@
function t1() {
- return (
+ return
return (
// comment
- a);
+ a;
+ (a));
}
function t2() {
- return (
Expand Down Expand Up @@ -53,23 +50,20 @@
+ 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,9 +67,9 @@ function *t10() {

//// [yieldStatementNoAsiAfterTransform.js]
function* t1() {
yield
yield (
// comment
a;
(a));
}
function* t2() {
yield
Expand Down Expand Up @@ -102,17 +102,17 @@ function* t7() {
a ``;
}
function* t8() {
yield
yield (
// comment
a;
(a));
}
function* t9() {
yield
yield (
// comment
a;
(a));
}
function* t10() {
yield
yield (
// comment
a;
(a));
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@
- }
- });
+function* t1() {
+ yield
+ yield (
+ // comment
+ a;
+ (a));
+}
+function* t2() {
+ yield
Expand Down Expand Up @@ -190,17 +190,17 @@
+ a ``;
+}
+function* t8() {
+ yield
+ yield (
+ // comment
+ a;
+ (a));
+}
+function* t9() {
+ yield
+ yield (
+ // comment
+ a;
+ (a));
+}
+function* t10() {
+ yield
+ yield (
+ // comment
+ a;
+ (a));
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function *t10() {

//// [yieldStatementNoAsiAfterTransform.js]
function* t1() {
yield
yield (
// comment
a;
(a));
}
function* t2() {
yield
Expand Down Expand Up @@ -102,17 +102,17 @@ function* t7() {
a ``;
}
function* t8() {
yield
yield (
// comment
a;
(a));
}
function* t9() {
yield
yield (
// comment
a;
(a));
}
function* t10() {
yield
yield (
// comment
a;
(a));
}
Loading