Skip to content

Commit e3aad99

Browse files
committed
query: Fix handling of patterns with wildcards at the root
1 parent 741eed0 commit e3aad99

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

cli/src/tests/query_test.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,36 @@ fn test_query_matches_with_named_wildcard() {
434434
});
435435
}
436436

437+
#[test]
438+
fn test_query_matches_with_wildcard_at_the_root() {
439+
allocations::record(|| {
440+
let language = get_language("javascript");
441+
let query = Query::new(
442+
language,
443+
"
444+
(*
445+
(comment) @doc
446+
.
447+
(function_declaration
448+
name: (identifier) @name))
449+
",
450+
)
451+
.unwrap();
452+
453+
let source = "/* one */ var x; /* two */ function y() {} /* three */ class Z {}";
454+
455+
let mut parser = Parser::new();
456+
parser.set_language(language).unwrap();
457+
let tree = parser.parse(source, None).unwrap();
458+
let mut cursor = QueryCursor::new();
459+
let matches = cursor.matches(&query, tree.root_node(), to_callback(source));
460+
461+
assert_eq!(
462+
collect_matches(matches, &query, source),
463+
&[(0, vec![("doc", "/* two */"), ("name", "y")]),]
464+
);
465+
});
466+
}
437467
#[test]
438468
fn test_query_with_immediate_siblings() {
439469
allocations::record(|| {

lib/src/query.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ static TSQueryError ts_query__parse_pattern(
698698

699699
// Parse the wildcard symbol
700700
if (stream->next == '*') {
701-
symbol = NAMED_WILDCARD_SYMBOL;
701+
symbol = depth > 0 ? NAMED_WILDCARD_SYMBOL : WILDCARD_SYMBOL;
702702
stream_advance(stream);
703703
}
704704

0 commit comments

Comments
 (0)