fix(lib): do not return field names for extras
This commit is contained in:
parent
946acfd70f
commit
61d0395543
2 changed files with 10 additions and 3 deletions
|
|
@ -296,7 +296,9 @@ fn test_parent_of_zero_width_node() {
|
|||
fn test_node_field_name_for_child() {
|
||||
let mut parser = Parser::new();
|
||||
parser.set_language(&get_language("c")).unwrap();
|
||||
let tree = parser.parse("int w = x + y;", None).unwrap();
|
||||
let tree = parser
|
||||
.parse("int w = x + /* y is special! */ y;", None)
|
||||
.unwrap();
|
||||
let translation_unit_node = tree.root_node();
|
||||
let declaration_node = translation_unit_node.named_child(0).unwrap();
|
||||
|
||||
|
|
@ -311,12 +313,14 @@ fn test_node_field_name_for_child() {
|
|||
binary_expression_node.field_name_for_child(1),
|
||||
Some("operator")
|
||||
);
|
||||
// The comment should not have a field name, as it's just an extra
|
||||
assert_eq!(binary_expression_node.field_name_for_child(2), None);
|
||||
assert_eq!(
|
||||
binary_expression_node.field_name_for_child(2),
|
||||
binary_expression_node.field_name_for_child(3),
|
||||
Some("right")
|
||||
);
|
||||
// Negative test - Not a valid child index
|
||||
assert_eq!(binary_expression_node.field_name_for_child(3), None);
|
||||
assert_eq!(binary_expression_node.field_name_for_child(4), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -646,6 +646,9 @@ const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) {
|
|||
while (ts_node_child_iterator_next(&iterator, &child)) {
|
||||
if (ts_node__is_relevant(child, true)) {
|
||||
if (index == child_index) {
|
||||
if (ts_node_is_extra(child)) {
|
||||
return NULL;
|
||||
}
|
||||
const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
|
||||
if (field_name) return field_name;
|
||||
return inherited_field_name;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue