Skip to content

Commit 5d1be54

Browse files
authored
fix(lib): correct next sibling of zero width node
1 parent 51dfe3d commit 5d1be54

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

cli/src/tests/node_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,33 @@ fn test_parent_of_zero_width_node() {
306306
assert_eq!(parent, script_element);
307307
}
308308

309+
#[test]
310+
fn test_next_sibling_of_zero_width_node() {
311+
let grammar_json = load_grammar_file(
312+
&fixtures_dir()
313+
.join("test_grammars")
314+
.join("next_sibling_from_zwt")
315+
.join("grammar.js"),
316+
None,
317+
)
318+
.unwrap();
319+
320+
let (parser_name, parser_code) = generate_parser_for_grammar(&grammar_json).unwrap();
321+
322+
let mut parser = Parser::new();
323+
let language = get_test_language(&parser_name, &parser_code, None);
324+
parser.set_language(&language).unwrap();
325+
326+
let tree = parser.parse("abdef", None).unwrap();
327+
328+
let root_node = tree.root_node();
329+
let missing_c = root_node.child(2).unwrap();
330+
assert!(missing_c.is_missing());
331+
assert_eq!(missing_c.kind(), "c");
332+
let node_d = root_node.child(3).unwrap();
333+
assert_eq!(missing_c.next_sibling().unwrap(), node_d);
334+
}
335+
309336
#[test]
310337
fn test_node_field_name_for_child() {
311338
let mut parser = Parser::new();

lib/src/node.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous)
263263
TSNode child;
264264
NodeChildIterator iterator = ts_node_iterate_children(&node);
265265
while (ts_node_child_iterator_next(&iterator, &child)) {
266-
if (iterator.position.bytes < target_end_byte) continue;
267-
if (ts_node_start_byte(child) <= ts_node_start_byte(self)) {
266+
if (iterator.position.bytes <= target_end_byte) continue;
267+
if (ts_node_start_byte(child) < ts_node_start_byte(self)) {
268268
if (ts_node__subtree(child).ptr != ts_node__subtree(self).ptr) {
269269
child_containing_target = child;
270270
}
@@ -541,8 +541,8 @@ TSNode ts_node_parent(TSNode self) {
541541
if (node.id == self.id) return ts_node__null();
542542

543543
while (true) {
544-
TSNode next_node = ts_node_child_containing_descendant(node, self);
545-
if (ts_node_is_null(next_node)) break;
544+
TSNode next_node = ts_node_child_with_descendant(node, self);
545+
if (next_node.id == self.id || ts_node_is_null(next_node)) break;
546546
node = next_node;
547547
}
548548

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
===========================
2+
missing c node
3+
===========================
4+
5+
abdef
6+
7+
---
8+
9+
(source
10+
(MISSING "c"))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = grammar({
2+
name: "next_sibling_from_zwt",
3+
extras: $ => [
4+
/\s|\\\r?\n/,
5+
],
6+
7+
rules: {
8+
source: $ => seq(
9+
'a',
10+
$._bc,
11+
'd',
12+
'e',
13+
'f',
14+
),
15+
16+
_bc: $ => seq(
17+
'b',
18+
'c',
19+
),
20+
}
21+
});
22+

0 commit comments

Comments
 (0)