From d0e7612c38ec3340bd25b952643dc82b2b1f4606 Mon Sep 17 00:00:00 2001 From: "Arend van Beelen jr." Date: Thu, 19 Sep 2024 13:32:12 +0200 Subject: [PATCH] fix(grit): fix node walking --- crates/biome_grit_patterns/src/grit_target_node.rs | 3 +++ crates/biome_grit_patterns/tests/quick_test.rs | 13 ++++++++++--- .../tests/specs/ts/containsSnippet.grit | 3 +++ .../tests/specs/ts/containsSnippet.snap | 13 +++++++++++++ .../tests/specs/ts/containsSnippet.ts | 3 +++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 crates/biome_grit_patterns/tests/specs/ts/containsSnippet.grit create mode 100644 crates/biome_grit_patterns/tests/specs/ts/containsSnippet.snap create mode 100644 crates/biome_grit_patterns/tests/specs/ts/containsSnippet.ts diff --git a/crates/biome_grit_patterns/src/grit_target_node.rs b/crates/biome_grit_patterns/src/grit_target_node.rs index eefdd9a28a24..93b8f247bde0 100644 --- a/crates/biome_grit_patterns/src/grit_target_node.rs +++ b/crates/biome_grit_patterns/src/grit_target_node.rs @@ -506,6 +506,9 @@ impl<'a> AstCursor for GritTargetNodeCursor<'a> { } fn goto_next_sibling(&mut self) -> bool { + if self.node == self.root { + return false; + } match self.node.next_sibling() { Some(sibling) => { self.node = sibling; diff --git a/crates/biome_grit_patterns/tests/quick_test.rs b/crates/biome_grit_patterns/tests/quick_test.rs index da32c54aeda7..0c2980137d92 100644 --- a/crates/biome_grit_patterns/tests/quick_test.rs +++ b/crates/biome_grit_patterns/tests/quick_test.rs @@ -7,7 +7,12 @@ use biome_js_syntax::JsFileSource; #[ignore] #[test] fn test_query() { - let parse_grit_result = parse_grit("`foo.$x && foo.$x()`"); + let parse_grit_result = parse_grit( + "`console.log($args)` where { + $args <: contains `world` +} +", + ); if !parse_grit_result.diagnostics().is_empty() { panic!("Cannot parse query:\n{:?}", parse_grit_result.diagnostics()); } @@ -23,7 +28,9 @@ fn test_query() { println!("Diagnostics from compiling query:\n{:?}", query.diagnostics); } - let body = r#"foo.bar && foo.bar(); + let body = r#"console.log("hello, world"); +console.log("hello", world); +console.log(`hello ${world}`); "#; let file = GritTargetFile { @@ -32,5 +39,5 @@ fn test_query() { }; let results = query.execute(file).expect("could not execute query"); - println!("Results: {results:?}"); + println!("Results: {results:#?}"); } diff --git a/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.grit b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.grit new file mode 100644 index 000000000000..4f6b7843b646 --- /dev/null +++ b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.grit @@ -0,0 +1,3 @@ +`console.log($args)` where { + $args <: contains `world` +} diff --git a/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.snap b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.snap new file mode 100644 index 000000000000..12b50a315dab --- /dev/null +++ b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.snap @@ -0,0 +1,13 @@ +--- +source: crates/biome_grit_patterns/tests/spec_tests.rs +expression: containsSnippet +--- +SnapshotResult { + messages: [], + matched_ranges: [ + "2:1-2:28", + "3:1-3:30", + ], + rewritten_files: [], + created_files: [], +} diff --git a/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.ts b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.ts new file mode 100644 index 000000000000..2be0c6975408 --- /dev/null +++ b/crates/biome_grit_patterns/tests/specs/ts/containsSnippet.ts @@ -0,0 +1,3 @@ +console.log("hello, world"); +console.log("hello", world); +console.log(`hello ${world}`);