Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit 99a4bc5

Browse files
authored
yamlpath: handle interceding list comments correctly (#25)
1 parent 62ce7bf commit 99a4bc5

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

yamlpath/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,17 @@ impl Document {
545545
return Err(QueryError::ExhaustedList(idx, children.len()));
546546
};
547547

548+
// If we're in a block_sequence, there's an intervening `block_sequence_item`
549+
// getting in the way of our `block_node`/`flow_node`.
548550
if child.kind_id() == self.block_sequence_item_id {
549-
// If we're in a block_sequence, there's an intervening `block_sequence_item`
550-
// getting in the way of our `flow_node`.
551-
return child.named_child(0).ok_or_else(|| {
552-
QueryError::MissingChild(child.kind().into(), "block_sequence_item".into())
553-
});
551+
// NOTE: We can't just get the first named child here, since there might
552+
// be interceding comments.
553+
return child
554+
.named_children(&mut cur)
555+
.find(|c| c.kind_id() == self.block_node_id || c.kind_id() == self.flow_node_id)
556+
.ok_or_else(|| {
557+
QueryError::MissingChild(child.kind().into(), "block_sequence_item".into())
558+
});
554559
} else if child.kind_id() == self.flow_pair_id {
555560
// Similarly, if our index happens to be a `flow_pair`, we need to
556561
// get the `value` child to get the next `flow_node`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
testcase:
2+
foo:
3+
- # hello!
4+
bar: baz
5+
6+
multiple:
7+
- # comment 1
8+
# comment 2
9+
bar: baz
10+
11+
many-children:
12+
- # foo
13+
foo: bar
14+
- # bar
15+
bar: baz
16+
17+
queries:
18+
- query: [foo, 0, bar]
19+
expected: " bar: baz"
20+
21+
- query: [multiple, 0, bar]
22+
expected: " bar: baz"
23+
24+
- query: [many-children, 1, bar]
25+
expected: " bar: baz"

0 commit comments

Comments
 (0)