Skip to content

Commit

Permalink
fix: Idx1 of ObjectLiteral and ArrayLiteral (#504)
Browse files Browse the repository at this point in the history
Fix Idx1 of ObjectLiteral  so that it points to the character immediately after the right brace.

Fix Idx1 of ArrayLiteral so that it points to the character immediately after the right bracket.
  • Loading branch information
tyamagu2 authored Jul 20, 2023
1 parent 0357209 commit d2ed0a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit d2ed0a7

Please sign in to comment.