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++;