Skip to content

Commit 9610a84

Browse files
fix(lib): restrict pattern_map optimization when a wildcard step has an immediate first child
Co-authored-by: Amaan Qureshi <[email protected]>
1 parent 9d1ac21 commit 9610a84

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cli/src/tests/query_test.rs

+17
Original file line numberDiff line numberDiff line change
@@ -5171,3 +5171,20 @@ fn test_query_compiler_oob_access() {
51715171
// UBSAN should not report any OOB access
51725172
assert!(Query::new(&language, "(package_declaration _ (_) @name _)").is_ok());
51735173
}
5174+
5175+
#[test]
5176+
fn test_query_wildcard_with_immediate_first_child() {
5177+
let language = get_language("javascript");
5178+
let query = Query::new(&language, "(_ . (identifier) @firstChild)").unwrap();
5179+
let source = "function name(one, two, three) { }";
5180+
5181+
assert_query_matches(
5182+
&language,
5183+
&query,
5184+
source,
5185+
&[
5186+
(0, vec![("firstChild", "name")]),
5187+
(0, vec![("firstChild", "one")]),
5188+
],
5189+
);
5190+
}

lib/src/query.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,7 @@ TSQuery *ts_query_new(
27402740
// there is a parent node, and capture it if necessary.
27412741
if (step->symbol == WILDCARD_SYMBOL && step->depth == 0 && !step->field) {
27422742
QueryStep *second_step = &self->steps.contents[start_step_index + 1];
2743-
if (second_step->symbol != WILDCARD_SYMBOL && second_step->depth == 1) {
2743+
if (second_step->symbol != WILDCARD_SYMBOL && second_step->depth == 1 && !second_step->is_immediate) {
27442744
wildcard_root_alternative_index = step->alternative_index;
27452745
start_step_index += 1;
27462746
step = second_step;

0 commit comments

Comments
 (0)