From e3aad995f61de935622a479c5e284f348d49c946 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 11 Mar 2020 13:13:53 -0700 Subject: [PATCH] query: Fix handling of patterns with wildcards at the root --- cli/src/tests/query_test.rs | 30 ++++++++++++++++++++++++++++++ lib/src/query.c | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 008bd51a..87420501 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -434,6 +434,36 @@ fn test_query_matches_with_named_wildcard() { }); } +#[test] +fn test_query_matches_with_wildcard_at_the_root() { + allocations::record(|| { + let language = get_language("javascript"); + let query = Query::new( + language, + " + (* + (comment) @doc + . + (function_declaration + name: (identifier) @name)) + ", + ) + .unwrap(); + + let source = "/* one */ var x; /* two */ function y() {} /* three */ class Z {}"; + + let mut parser = Parser::new(); + parser.set_language(language).unwrap(); + let tree = parser.parse(source, None).unwrap(); + let mut cursor = QueryCursor::new(); + let matches = cursor.matches(&query, tree.root_node(), to_callback(source)); + + assert_eq!( + collect_matches(matches, &query, source), + &[(0, vec![("doc", "/* two */"), ("name", "y")]),] + ); + }); +} #[test] fn test_query_with_immediate_siblings() { allocations::record(|| { diff --git a/lib/src/query.c b/lib/src/query.c index da5da2a9..65144395 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -698,7 +698,7 @@ static TSQueryError ts_query__parse_pattern( // Parse the wildcard symbol if (stream->next == '*') { - symbol = NAMED_WILDCARD_SYMBOL; + symbol = depth > 0 ? NAMED_WILDCARD_SYMBOL : WILDCARD_SYMBOL; stream_advance(stream); }