Skip to content

Commit

Permalink
fix: starting position of if statement (robertkrimen#501)
Browse files Browse the repository at this point in the history
Fix IfStatement Idx0 to point to the right place. I was pointing to the right parenthesis
after the test expression.
  • Loading branch information
tyamagu2 authored Jul 17, 2023
1 parent ea8bcc3 commit f70d418
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,8 @@ func TestPosition(t *testing.T) {
is(err, nil)
block := program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement)
node = block.List[0].(*ast.IfStatement)
is(node.Idx0(), 21)
is(node.Idx0(), 14)
is(parser.slice(node.Idx0(), node.Idx1()), "if (abc) { throw 'failed'; }")
node = node.(*ast.IfStatement).Consequent.(*ast.BlockStatement).List[0].(*ast.ThrowStatement)
is(node.Idx0(), 39)
})
Expand Down
4 changes: 2 additions & 2 deletions parser/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,15 @@ func (p *parser) parseIfStatement() ast.Statement {
if p.mode&StoreComments != 0 {
comments = p.comments.FetchAll()
}
p.expect(token.IF)
pos := p.expect(token.IF)
var ifComments []*ast.Comment
if p.mode&StoreComments != 0 {
ifComments = p.comments.FetchAll()
}

p.expect(token.LEFT_PARENTHESIS)
node := &ast.IfStatement{
If: p.idx,
If: pos,
Test: p.parseExpression(),
}
p.expect(token.RIGHT_PARENTHESIS)
Expand Down

0 comments on commit f70d418

Please sign in to comment.