From 8fc947654a747588f7a1b677f24184cb45b52574 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sat, 10 Sep 2022 08:17:47 +0800 Subject: [PATCH] more source mappings for switch statements --- internal/js_ast/js_ast.go | 8 +++++--- internal/js_parser/js_parser.go | 11 +++++++---- internal/js_printer/js_printer.go | 3 +++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/js_ast/js_ast.go b/internal/js_ast/js_ast.go index 11b5c604d6d..68215b9c0ca 100644 --- a/internal/js_ast/js_ast.go +++ b/internal/js_ast/js_ast.go @@ -972,12 +972,14 @@ type STry struct { type Case struct { ValueOrNil Expr // If this is nil, this is "default" instead of "case" Body []Stmt + Loc logger.Loc } type SSwitch struct { - Test Expr - Cases []Case - BodyLoc logger.Loc + Test Expr + Cases []Case + BodyLoc logger.Loc + CloseBraceLoc logger.Loc } // This object represents all of these types of import statements: diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 0f877cdcfc4..641418d7a6e 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -6479,6 +6479,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() if p.lexer.Token == js_lexer.TDefault { if foundDefault { @@ -6505,14 +6506,16 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { } } - cases = append(cases, js_ast.Case{ValueOrNil: value, Body: body}) + cases = append(cases, js_ast.Case{ValueOrNil: value, Body: body, Loc: caseLoc}) } + closeBraceLoc := p.lexer.Loc() p.lexer.Expect(js_lexer.TCloseBrace) return js_ast.Stmt{Loc: loc, Data: &js_ast.SSwitch{ - Test: test, - BodyLoc: bodyLoc, - Cases: cases, + Test: test, + Cases: cases, + BodyLoc: bodyLoc, + CloseBraceLoc: closeBraceLoc, }} case js_lexer.TTry: diff --git a/internal/js_printer/js_printer.go b/internal/js_printer/js_printer.go index 0f749e6337f..32249bd48e9 100644 --- a/internal/js_printer/js_printer.go +++ b/internal/js_printer/js_printer.go @@ -3532,6 +3532,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) { p.printExpr(s.Test, js_ast.LLowest, 0) p.print(")") p.printSpace() + p.addSourceMapping(s.BodyLoc) p.print("{") p.printNewline() p.options.Indent++ @@ -3539,6 +3540,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) { for _, c := range s.Cases { p.printSemicolonIfNeeded() p.printIndent() + p.addSourceMapping(c.Loc) if c.ValueOrNil.Data != nil { p.print("case") @@ -3569,6 +3571,7 @@ func (p *printer) printStmt(stmt js_ast.Stmt, flags printStmtFlags) { p.options.Indent-- p.printIndent() + p.addSourceMapping(s.CloseBraceLoc) p.print("}") p.printNewline() p.needsSemicolon = false