Skip to content

Commit b36ef4b

Browse files
ribru17amaanq
authored andcommitted
fix(lib)!: child_containing_descendant now returns direct children
Previously, `child_containing_descendant` would return `null` when called on a node's direct parent. In my opinion, this doesn't make much sense; it seems like a node would contain itself. This (breaking) commit changes the function so that it can return direct children.
1 parent 6b1ebd3 commit b36ef4b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

cli/src/tests/node_test.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ fn test_node_child() {
182182
object_node.child_containing_descendant(null_node).unwrap(),
183183
pair_node
184184
);
185-
assert_eq!(pair_node.child_containing_descendant(null_node), None);
185+
assert_eq!(
186+
pair_node.child_containing_descendant(null_node).unwrap(),
187+
null_node
188+
);
189+
assert_eq!(null_node.child_containing_descendant(null_node), None);
186190
}
187191

188192
#[test]
@@ -287,7 +291,13 @@ fn test_parent_of_zero_width_node() {
287291
root.child_containing_descendant(block).unwrap(),
288292
function_definition
289293
);
290-
assert_eq!(function_definition.child_containing_descendant(block), None);
294+
assert_eq!(
295+
function_definition
296+
.child_containing_descendant(block)
297+
.unwrap(),
298+
block
299+
);
300+
assert_eq!(block.child_containing_descendant(block), None);
291301

292302
let code = "<script></script>";
293303
parser.set_language(&get_language("html")).unwrap();
@@ -480,7 +490,11 @@ fn test_node_named_child() {
480490
object_node.child_containing_descendant(null_node).unwrap(),
481491
pair_node
482492
);
483-
assert_eq!(pair_node.child_containing_descendant(null_node), None);
493+
assert_eq!(
494+
pair_node.child_containing_descendant(null_node).unwrap(),
495+
null_node
496+
);
497+
assert_eq!(null_node.child_containing_descendant(null_node), None);
484498
}
485499

486500
#[test]

lib/src/node.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ TSNode ts_node_parent(TSNode self) {
550550

551551
while (true) {
552552
TSNode next_node = ts_node_child_containing_descendant(node, self);
553-
if (ts_node_is_null(next_node)) break;
553+
if (next_node.id == self.id) break;
554554
node = next_node;
555555
}
556556

@@ -567,10 +567,12 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) {
567567
if (
568568
!ts_node_child_iterator_next(&iter, &self)
569569
|| ts_node_start_byte(self) > start_byte
570-
|| self.id == subnode.id
571570
) {
572571
return ts_node__null();
573572
}
573+
if (self.id == subnode.id) {
574+
return self;
575+
}
574576

575577
// Here we check the current self node and *all* of its zero-width token siblings that follow.
576578
// If any of these nodes contain the target subnode, we return that node. Otherwise, we restore the node we started at
@@ -585,7 +587,7 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) {
585587
}
586588
ts_node_child_iterator_next(&iter, &self);
587589
if (self.id == subnode.id) {
588-
return ts_node__null();
590+
return self;
589591
}
590592
}
591593
self = old;

0 commit comments

Comments
 (0)