From 72143b17ff7ed617714e77ccd9a51e49bdecc9ed Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 27 Mar 2021 09:36:13 -0700 Subject: [PATCH] Fix handling of queries with field names at the roots of patterns Fixes #1018 --- cli/src/tests/query_test.rs | 18 ++++++++++++++++++ lib/src/query.c | 5 +++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 4220dccb..d94ebac0 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -991,6 +991,24 @@ fn test_query_matches_with_negated_fields() { }); } +#[test] +fn test_query_matches_with_field_at_root() { + allocations::record(|| { + let language = get_language("javascript"); + let query = Query::new(language, "name: (identifier) @name").unwrap(); + assert_query_matches( + language, + &query, + " + a(); + function b() {} + class c extends d {} + ", + &[(0, vec![("name", "b")]), (0, vec![("name", "c")])], + ); + }); +} + #[test] fn test_query_matches_with_repeated_leaf_nodes() { allocations::record(|| { diff --git a/lib/src/query.c b/lib/src/query.c index d691dcc8..65dbe1fe 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -2756,8 +2756,9 @@ static inline bool ts_query_cursor__advance( do { // If this node matches the first step of the pattern, then add a new // state at the start of this pattern. - if (step->field && field_id != step->field) continue; - ts_query_cursor__add_state(self, pattern); + if (!step->field || field_id == step->field) { + ts_query_cursor__add_state(self, pattern); + } // Advance to the next pattern whose root node matches this node. i++;