Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Idx1 of ObjectLiteral and ArrayLiteral #504

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (al *ArrayLiteral) Idx0() file.Idx {

// Idx1 implements Node.
func (al *ArrayLiteral) Idx1() file.Idx {
return al.RightBracket
return al.RightBracket + 1
}

// expression implements Expression.
Expand Down Expand Up @@ -343,7 +343,7 @@ func (ol *ObjectLiteral) Idx0() file.Idx {

// Idx1 implements Node.
func (ol *ObjectLiteral) Idx1() file.Idx {
return ol.RightBrace
return ol.RightBrace + 1
}

// expression implements Expression.
Expand Down
14 changes: 14 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,20 @@ func TestPosition(t *testing.T) {
node = block.List[0].(*ast.ForInStatement)
is(node.Idx0(), 14)
is(parser.slice(node.Idx0(), node.Idx1()), "for (p in o) { console.log(p); }")

parser = newParser("", "x = {x: 1}", 1, nil)
program, err = parser.parse()
is(err, nil)
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ObjectLiteral)
is(node.Idx0(), 5)
is(parser.slice(node.Idx0(), node.Idx1()), "{x: 1}")

parser = newParser("", "x = [1, 2]", 1, nil)
program, err = parser.parse()
is(err, nil)
node = program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.AssignExpression).Right.(*ast.ArrayLiteral)
is(node.Idx0(), 5)
is(parser.slice(node.Idx0(), node.Idx1()), "[1, 2]")
})
}

Expand Down