Skip to content

Commit

Permalink
more source mappings for switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 10, 2022
1 parent 975015d commit 8fc9476
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
8 changes: 5 additions & 3 deletions internal/js_ast/js_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions internal/js_printer/js_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3532,13 +3532,15 @@ 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++

for _, c := range s.Cases {
p.printSemicolonIfNeeded()
p.printIndent()
p.addSourceMapping(c.Loc)

if c.ValueOrNil.Data != nil {
p.print("case")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8fc9476

Please sign in to comment.