Skip to content

Commit

Permalink
fix #2814: additional comment-related regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 11, 2023
1 parent e4cf1b3 commit 6346398
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 62 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## Unreleased

* Fix additional comment-related regressions ([#2814](https://github.com/evanw/esbuild/issues/2814))

This release fixes more edge cases where the new comment preservation behavior that was added in 0.16.14 could introduce syntax errors. Specifically:

```js
x = () => (/* comment */ {})
for ((/* comment */ let).x of y) ;
function *f() { yield (/* comment */class {}) }
```

These cases caused esbuild to generate code with a syntax error in version 0.16.14 or above. These bugs have now been fixed.

## 0.16.16

* Fix a regression caused by comment preservation ([#2805](https://github.com/evanw/esbuild/issues/2805))
Expand Down
16 changes: 16 additions & 0 deletions internal/bundler_tests/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7684,6 +7684,22 @@ func TestCommentPreservation(t *testing.T) {
/*a*/ a ? /*b*/ b : /*c*/ c,
a /*a*/ ? b /*b*/ : c /*c*/,
)
for (/*foo*/a;;);
for (;/*foo*/a;);
for (;;/*foo*/a);
for (/*foo*/a in b);
for (a in /*foo*/b);
for (/*foo*/a of b);
for (a of /*foo*/b);
if (/*foo*/a);
with (/*foo*/a);
while (/*foo*/a);
do {} while (/*foo*/a);
switch (/*foo*/a) {}
`,
},
entryPaths: []string{"/entry.js"},
Expand Down
66 changes: 66 additions & 0 deletions internal/bundler_tests/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,72 @@ console.log(
),
a ? b : c
);
for (
/*foo*/
a;
;
)
;
for (
;
/*foo*/
a;
)
;
for (
;
;
/*foo*/
a
)
;
for (
/*foo*/
a in b
)
;
for (
a in
/*foo*/
b
)
;
for (
/*foo*/
a of b
)
;
for (
a of
/*foo*/
b
)
;
if (
/*foo*/
a
)
;
with (
/*foo*/
a
)
;
while (
/*foo*/
a
)
;
do {
} while (
/*foo*/
a
);
switch (
/*foo*/
a
) {
}

================================================================================
TestCommentPreservationImportAssertions
Expand Down
20 changes: 16 additions & 4 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ func (p *parser) parseParenExpr(loc logger.Loc, level js_ast.L, opts parenExprOp
panic(js_lexer.LexerPanic{})
}
value := js_ast.JoinAllWithComma(items)
p.markExprAsParenthesized(value)
p.markExprAsParenthesized(value, loc, isAsync)
return value
}

Expand Down Expand Up @@ -3137,7 +3137,7 @@ func (p *parser) parsePrefix(level js_ast.L, errors *deferredErrors, flags exprF
p.allowIn = true

value := p.parseExpr(js_ast.LLowest)
p.markExprAsParenthesized(value)
p.markExprAsParenthesized(value, loc, false)
p.lexer.Expect(js_lexer.TCloseParen)

p.allowIn = oldAllowIn
Expand Down Expand Up @@ -6848,7 +6848,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
badLetRange = logger.Range{}
initOrNil = stmt
} else {
initOrNil = js_ast.Stmt{Loc: initLoc, Data: &js_ast.SExpr{Value: expr}}
initOrNil = js_ast.Stmt{Loc: expr.Loc, Data: &js_ast.SExpr{Value: expr}}
}
}

Expand Down Expand Up @@ -10309,7 +10309,19 @@ func (p *parser) maybeRelocateVarsToTopLevel(decls []js_ast.Decl, mode relocateV
return js_ast.Stmt{Loc: value.Loc, Data: &js_ast.SExpr{Value: value}}, true
}

func (p *parser) markExprAsParenthesized(value js_ast.Expr) {
func (p *parser) markExprAsParenthesized(value js_ast.Expr, openParenLoc logger.Loc, isAsync bool) {
// Don't lose comments due to parentheses. For example, we don't want to lose
// the comment here:
//
// ( /* comment */ (foo) );
//
if !isAsync {
if comments, ok := p.exprComments[openParenLoc]; ok {
delete(p.exprComments, openParenLoc)
p.exprComments[value.Loc] = append(comments, p.exprComments[value.Loc]...)
}
}

switch e := value.Data.(type) {
case *js_ast.EArray:
e.IsParenthesized = true
Expand Down
Loading

0 comments on commit 6346398

Please sign in to comment.