fix(lib): prevent wildcards from incorrectly marking child patterns as infallible

When a pattern appears under a wildcard parent (like "(_ (expr))"), we
were incorrectly marking it as infallible. The parent_pattern_guaranteed
flag only means the pattern will match after finding the right wildcard
parent, not that any wildcard parent will work.
This commit is contained in:
Amaan Qureshi 2025-01-03 22:31:50 -05:00
parent a7e6d01144
commit 5f379da544
2 changed files with 31 additions and 1 deletions

View file

@ -5520,3 +5520,33 @@ fn f() {
);
assert_eq!(matches, flipped_matches);
}
#[test]
fn test_wildcard_parent_allows_fallible_child_patterns() {
let language = get_language("javascript");
let mut parser = Parser::new();
parser.set_language(&language).unwrap();
let source_code = r#"
function foo() {
"bar"
}
"#;
let query = Query::new(
&language,
"(function_declaration
(_
(expression_statement)
)
) @part",
)
.unwrap();
assert_query_matches(
&language,
&query,
source_code,
&[(0, vec![("part", "function foo() {\n \"bar\"\n}")])],
);
}

View file

@ -3004,7 +3004,7 @@ bool ts_query__step_is_fallible(
return (
next_step->depth != PATTERN_DONE_MARKER &&
next_step->depth > step->depth &&
!next_step->parent_pattern_guaranteed
(!next_step->parent_pattern_guaranteed || step->symbol == WILDCARD_SYMBOL)
);
}