smacker/go-tree-sitter

FieldNameForChild returns false results

vjerci opened this issue · 2 comments

When selecting ruby language and when doing this query

log.info($ARGUMENTS)

If we take the tree we will get this tree:

[program](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [1, 0]
  [call](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 20]
    receiver: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 3]
    method: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 4] - [0, 8]
    arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 8] - [0, 20]
      [global_variable](https://tree-sitter.github.io/tree-sitter/playground#) [0, 9] - [0, 19]

As we can see there are 3 field names receiver, method and arguments

Now iterating or namedChildCount of call node with following code

for j := 0; j < int(node.NamedChildCount()); j++ {
	log.Println("it is "+ strconv.Itoa(j), node.FieldNameForChild(j))
}

we get results

it is 0 method
it is 1 arguments
it is 2

As we can see order is messed up (method should be on index 1, on index 0 it should be receiver)
field name receiver is also missing.

I've noticed similar behavior when traversing the tree through the Node interface vs using the TreeCursor. The TreeCursor seems to find the correct Field and the Node.FieldNameForChild doesn't.

Can confirm the same behavior here.