Skip to content

Commit

Permalink
fix: idx1 of variable expression without initializer
Browse files Browse the repository at this point in the history
Fix Idx1 of VariableExpression so that it points to the character right after the name literal if the expression does not have an initializer.
  • Loading branch information
tyamagu2 committed Jul 21, 2023
1 parent 1257cb3 commit c9cddc3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ func (ve *VariableExpression) Idx0() file.Idx {
// Idx1 implements Node.
func (ve *VariableExpression) Idx1() file.Idx {
if ve.Initializer == nil {
return file.Idx(int(ve.Idx) + len(ve.Name) + 1)
return file.Idx(int(ve.Idx) + len(ve.Name))
}
return ve.Initializer.Idx1()
}
Expand Down
14 changes: 14 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,20 @@ func TestPosition(t *testing.T) {
node = block.List[0].(*ast.ExpressionStatement).Expression.(*ast.UnaryExpression)
is(node.Idx0(), 14)
is(parser.slice(node.Idx0(), node.Idx1()), "xyz++")

parser = newParser("", "(function(){ var abc, xyz = 1; })", 1, nil)
program, err = parser.parse()
is(err, nil)
block = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral).Body.(*ast.BlockStatement)
node = block.List[0].(*ast.VariableStatement)
is(node.Idx0(), 14)
is(parser.slice(node.Idx0(), node.Idx1()), "var abc, xyz = 1")
node = block.List[0].(*ast.VariableStatement).List[0].(*ast.VariableExpression)
is(node.Idx0(), 18)
is(parser.slice(node.Idx0(), node.Idx1()), "abc")
node = block.List[0].(*ast.VariableStatement).List[1].(*ast.VariableExpression)
is(node.Idx0(), 23)
is(parser.slice(node.Idx0(), node.Idx1()), "xyz = 1")
})
}

Expand Down

0 comments on commit c9cddc3

Please sign in to comment.