Handle null parent in {next,prev}_sibling

This commit is contained in:
Max Brunsfeld 2014-08-25 11:28:09 -07:00
parent cef6827182
commit 1535ebd21c
2 changed files with 17 additions and 2 deletions

View file

@ -73,6 +73,10 @@ describe("Node", []() {
ts_node_release(array);
ts_node_release(number);
});
it("returns null if the node has no parent", [&]() {
AssertThat(ts_node_parent(root), Equals<TSNode *>(nullptr));
});
});
describe("next_sibling and prev_sibling", [&]() {
@ -90,6 +94,11 @@ describe("Node", []() {
ts_node_release(number2);
ts_node_release(number3);
});
it("returns null when the node has no parent", [&]() {
AssertThat(ts_node_next_sibling(root), Equals<TSNode *>(nullptr));
AssertThat(ts_node_prev_sibling(root), Equals<TSNode *>(nullptr));
});
});
describe("find_for_range", [&]() {

View file

@ -53,11 +53,17 @@ const char *ts_node_string(const TSNode *node) {
TSNode *ts_node_parent(TSNode *child) { return child->parent; }
TSNode *ts_node_prev_sibling(TSNode *child) {
return ts_node_child(child->parent, child->index - 1);
if (child->parent)
return ts_node_child(child->parent, child->index - 1);
else
return NULL;
}
TSNode *ts_node_next_sibling(TSNode *child) {
return ts_node_child(child->parent, child->index + 1);
if (child->parent)
return ts_node_child(child->parent, child->index + 1);
else
return NULL;
}
size_t ts_node_child_count(const TSNode *parent) {