From f6e64811d1109cb34ad670297e650bd4075999a8 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 16 Aug 2024 15:20:28 -0400 Subject: [PATCH] fix #3838: print comments before `case` clauses --- CHANGELOG.md | 4 ++++ internal/js_parser/js_parser.go | 2 +- internal/js_printer/js_printer.go | 1 + internal/js_printer/js_printer_test.go | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2706d38654..d459923fec0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 8f598a8c05f..8da1a703ce4 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -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 { diff --git a/internal/js_printer/js_printer.go b/internal/js_printer/js_printer.go index d45e11193a5..3c5cab04571 100644 --- a/internal/js_printer/js_printer.go +++ b/internal/js_printer/js_printer.go @@ -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 { diff --git a/internal/js_printer/js_printer_test.go b/internal/js_printer/js_printer_test.go index e45f4e1adae..a17eb1792ae 100644 --- a/internal/js_printer/js_printer_test.go +++ b/internal/js_printer/js_printer_test.go @@ -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")