diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 6ef683bf..34e59209 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -5171,3 +5171,20 @@ fn test_query_compiler_oob_access() { // UBSAN should not report any OOB access assert!(Query::new(&language, "(package_declaration _ (_) @name _)").is_ok()); } + +#[test] +fn test_query_wildcard_with_immediate_first_child() { + let language = get_language("javascript"); + let query = Query::new(&language, "(_ . (identifier) @firstChild)").unwrap(); + let source = "function name(one, two, three) { }"; + + assert_query_matches( + &language, + &query, + source, + &[ + (0, vec![("firstChild", "name")]), + (0, vec![("firstChild", "one")]), + ], + ); +} diff --git a/lib/src/query.c b/lib/src/query.c index eb10bbc2..10587669 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -2740,7 +2740,7 @@ TSQuery *ts_query_new( // there is a parent node, and capture it if necessary. if (step->symbol == WILDCARD_SYMBOL && step->depth == 0 && !step->field) { QueryStep *second_step = &self->steps.contents[start_step_index + 1]; - if (second_step->symbol != WILDCARD_SYMBOL && second_step->depth == 1) { + if (second_step->symbol != WILDCARD_SYMBOL && second_step->depth == 1 && !second_step->is_immediate) { wildcard_root_alternative_index = step->alternative_index; start_step_index += 1; step = second_step;