Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ripe-rabbits-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#7601](https://github.com/biomejs/biome/issues/7601): Properly match Grit plugin's code snippet with only one child.
28 changes: 23 additions & 5 deletions crates/biome_grit_patterns/src/grit_code_snippet.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::grit_context::{GritExecContext, GritQueryContext};
use crate::grit_resolved_pattern::GritResolvedPattern;
use crate::grit_target_node::GritTargetSyntaxKind;
use biome_js_syntax::JsSyntaxKind;
use grit_pattern_matcher::binding::Binding;
use grit_pattern_matcher::context::ExecContext;
use grit_pattern_matcher::pattern::{
CodeSnippet, DynamicPattern, Matcher, Pattern, PatternName, ResolvedPattern, State,
};
use grit_util::AnalysisLogs;
use grit_util::error::GritResult;
use grit_util::{AnalysisLogs, AstNode};

#[derive(Clone, Debug)]
pub struct GritCodeSnippet {
Expand Down Expand Up @@ -42,11 +43,28 @@ impl Matcher<GritQueryContext> for GritCodeSnippet {
return Ok(false);
};

if let Some((_, pattern)) = self.patterns.iter().find(|(kind, _)| *kind == node.kind()) {
pattern.execute(resolved, state, context, logs)
} else {
Ok(false)
// First, try to match with the exact kind (fast path)
if let Some((_, pattern)) = self.patterns.iter().find(|(kind, _)| *kind == node.kind())
&& pattern.execute(resolved, state, context, logs)?
{
return Ok(true);
}

// If node has a single child, try matching against the child
// This handles wrapper nodes like JS_IDENTIFIER_EXPRESSION wrapping JS_REFERENCE_IDENTIFIER
if node.kind() == GritTargetSyntaxKind::JsSyntaxKind(JsSyntaxKind::JS_IDENTIFIER_EXPRESSION)
&& let Some(child) = node.first_child()
&& child.next_sibling().is_none()
{
let child_binding = GritResolvedPattern::from_node_binding(child);
for (_, pattern) in &self.patterns {
if pattern.execute(&child_binding, state, context, logs)? {
return Ok(true);
}
}
}

Ok(false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,25 @@ mod tests {
use grit_util::Parser;
use regex::Regex;

#[test]
fn test_snippet_node_from_tree() {
let snippet: SnippetTree<GritTargetTree> =
GritJsParser.parse_snippet("", "buildConfig", "");
let node = node_from_tree(&snippet).expect("no node found");
let formatted = format!("{node:#?}");
insta::assert_snapshot!(&formatted, @r#"
GritTargetNode {
node: JsLanguage(
Node(
0: JS_REFERENCE_IDENTIFIER@0..11
0: IDENT@0..11 "buildConfig" [] []
,
),
),
}
"#);
}

#[test]
fn test_node_from_tree() {
let snippet = GritJsParser.parse_snippet("", "console.log('hello')", "");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`$fn($args)` where {
$fn <: `buildConfig`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/biome_grit_patterns/tests/spec_tests.rs
expression: matchBacktickSnippet
---
SnapshotResult {
messages: [],
matched_ranges: [
"1:1-1:28",
],
rewritten_files: [],
created_files: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buildConfig({ foo: "bar" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`$fn($args)` where {
$fn <: "buildConfig"
}
12 changes: 12 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/matchStringLiteral.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/biome_grit_patterns/tests/spec_tests.rs
expression: matchStringLiteral
---
SnapshotResult {
messages: [],
matched_ranges: [
"1:1-1:28",
],
rewritten_files: [],
created_files: [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buildConfig({ foo: "bar" });