feat: support querying missing nodes

Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
This commit is contained in:
Riley Bruins 2024-12-14 11:57:36 -08:00 committed by GitHub
parent cd94dbd57f
commit 495fe2a6c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 190 additions and 7 deletions

View file

@ -193,6 +193,36 @@ fn test_query_errors_on_invalid_syntax() {
]
.join("\n")
);
// MISSING keyword with full pattern
assert_eq!(
Query::new(
&get_language("c"),
r"(MISSING (function_declarator (identifier))) "
)
.unwrap_err()
.message,
[
r"(MISSING (function_declarator (identifier))) ",
r" ^",
]
.join("\n")
);
// MISSING keyword with multiple identifiers
assert_eq!(
Query::new(
&get_language("c"),
r"(MISSING function_declarator function_declarator) "
)
.unwrap_err()
.message,
[
r"(MISSING function_declarator function_declarator) ",
r" ^",
]
.join("\n")
);
});
}
@ -767,6 +797,74 @@ fn test_query_matches_capturing_error_nodes() {
});
}
#[test]
fn test_query_matches_capturing_missing_nodes() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
&language,
r#"
(MISSING
; Comments should be valid
) @missing
(MISSING
; Comments should be valid
";"
; Comments should be valid
) @missing-semicolon
"#,
)
.unwrap();
// Missing anonymous nodes
assert_query_matches(
&language,
&query,
"
x = function(a) { b; } function(c) { d; }
// ^ MISSING semicolon here
",
&[
(0, vec![("missing", "")]),
(1, vec![("missing-semicolon", "")]),
],
);
let language = get_language("c");
let query = Query::new(
&language,
"(MISSING field_identifier) @missing-field-ident
(MISSING identifier) @missing-ident
(MISSING) @missing-anything",
)
.unwrap();
// Missing named nodes
assert_query_matches(
&language,
&query,
"
int main() {
if (a.) {
// ^ MISSING field_identifier here
b();
c();
if (*) d();
// ^ MISSING identifier here
}
}
",
&[
(0, vec![("missing-field-ident", "")]),
(2, vec![("missing-anything", "")]),
(1, vec![("missing-ident", "")]),
(2, vec![("missing-anything", "")]),
],
);
});
}
#[test]
fn test_query_matches_with_extra_children() {
allocations::record(|| {