Skip to content

Commit f6e6481

Browse files
committed
fix #3838: print comments before case clauses
1 parent 9c13ae1 commit f6e6481

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
2929
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.
3030
31+
* Print comments before case clauses in switch statements ([#3838](https://github.com/evanw/esbuild/issues/3838))
32+
33+
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).
34+
3135
## 0.23.0
3236
3337
**_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.

internal/js_parser/js_parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7346,7 +7346,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
73467346
for p.lexer.Token != js_lexer.TCloseBrace {
73477347
var value js_ast.Expr
73487348
body := []js_ast.Stmt{}
7349-
caseLoc := p.lexer.Loc()
7349+
caseLoc := p.saveExprCommentsHere()
73507350

73517351
if p.lexer.Token == js_lexer.TDefault {
73527352
if foundDefault {

internal/js_printer/js_printer.go

+1
Original file line numberDiff line numberDiff line change
@@ -4577,6 +4577,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) {
45774577
for _, c := range s.Cases {
45784578
p.printSemicolonIfNeeded()
45794579
p.printIndent()
4580+
p.printExprCommentsAtLoc(c.Loc)
45804581
p.addSourceMapping(c.Loc)
45814582

45824583
if c.ValueOrNil.Data != nil {

internal/js_printer/js_printer_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ func TestObject(t *testing.T) {
517517
expectPrintedTarget(t, 5, "function foo(__proto__) { return { __proto__ } }", "function foo(__proto__) {\n return { __proto__: __proto__ };\n}\n")
518518
}
519519

520+
func TestSwitch(t *testing.T) {
521+
// Ideally comments on case clauses would be preserved
522+
expectPrinted(t, "switch (x) { /* 1 */ case 1: /* 2 */ case 2: /* default */ default: break }",
523+
"switch (x) {\n /* 1 */\n case 1:\n /* 2 */\n case 2:\n /* default */\n default:\n break;\n}\n")
524+
}
525+
520526
func TestFor(t *testing.T) {
521527
// Make sure "in" expressions are forbidden in the right places
522528
expectPrinted(t, "for ((a in b);;);", "for ((a in b); ; ) ;\n")

0 commit comments

Comments
 (0)