Skip to content

Commit

Permalink
fix #3838: print comments before case clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 16, 2024
1 parent 9c13ae1 commit f6e6481
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
This release fixes a regression that caused certain errors relating to variable declarations to be reported at an incorrect location. The regression was introduced in version 0.18.7 of esbuild.
* Print comments before case clauses in switch statements ([#3838](https://github.com/evanw/esbuild/issues/3838))
With this release, esbuild will attempt to print comments that come before case clauses in switch statements. This is similar to what esbuild already does for comments inside of certain types of expressions. Note that these types of comments are not printed if minification is enabled (specifically whitespace minification).
## 0.23.0
**_This release deliberately contains backwards-incompatible changes._** To avoid automatically picking up releases like this, you should either be pinning the exact version of `esbuild` in your `package.json` file (recommended) or be using a version range syntax that only accepts patch upgrades such as `^0.22.0` or `~0.22.0`. See npm's documentation about [semver](https://docs.npmjs.com/cli/v6/using-npm/semver/) for more information.
Expand Down
2 changes: 1 addition & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7346,7 +7346,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
for p.lexer.Token != js_lexer.TCloseBrace {
var value js_ast.Expr
body := []js_ast.Stmt{}
caseLoc := p.lexer.Loc()
caseLoc := p.saveExprCommentsHere()

if p.lexer.Token == js_lexer.TDefault {
if foundDefault {
Expand Down
1 change: 1 addition & 0 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4577,6 +4577,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {
for _, c := range s.Cases {
p.printSemicolonIfNeeded()
p.printIndent()
p.printExprCommentsAtLoc(c.Loc)
p.addSourceMapping(c.Loc)

if c.ValueOrNil.Data != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/js_printer/js_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ func TestObject(t *testing.T) {
expectPrintedTarget(t, 5, "function foo(__proto__) { return { __proto__ } }", "function foo(__proto__) {\n return { __proto__: __proto__ };\n}\n")
}

func TestSwitch(t *testing.T) {
// Ideally comments on case clauses would be preserved
expectPrinted(t, "switch (x) { /* 1 */ case 1: /* 2 */ case 2: /* default */ default: break }",
"switch (x) {\n /* 1 */\n case 1:\n /* 2 */\n case 2:\n /* default */\n default:\n break;\n}\n")
}

func TestFor(t *testing.T) {
// Make sure "in" expressions are forbidden in the right places
expectPrinted(t, "for ((a in b);;);", "for ((a in b); ; ) ;\n")
Expand Down

0 comments on commit f6e6481

Please sign in to comment.