Skip to content

Commit

Permalink
fix: panic on BadStatement (robertkrimen#495)
Browse files Browse the repository at this point in the history
Add BadStatement case to walk.go to fix panic if there's bad code.
  • Loading branch information
amaicode authored and sg3des committed Jul 17, 2023
1 parent 58bcaf5 commit 21298ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Walk(v Visitor, n Node) {
Walk(v, n.Right)
}
case *BadExpression:
case *BadStatement:
case *BinaryExpression:
if n != nil {
Walk(v, n.Left)
Expand Down
19 changes: 19 additions & 0 deletions ast/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,22 @@ func Test_issue261(t *testing.T) {
})
}
}

func TestBadStatement(t *testing.T) {
source := `
var abc;
break; do {
} while(true);
`
program, err := parser.ParseFile(nil, "", source, 0)
require.ErrorContains(t, err, "Illegal break statement")

w := &walker{
source: source,
seen: make(map[ast.Node]struct{}),
}

require.NotPanics(t, func() {
ast.Walk(w, program)
})
}

0 comments on commit 21298ab

Please sign in to comment.