Skip to content

Commit 33ffcd5

Browse files
fix(lint): extend matcher for GritCodeSnippet to cover edge case (#7786)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent cb4fdc2 commit 33ffcd5

9 files changed

Lines changed: 79 additions & 5 deletions

File tree

.changeset/ripe-rabbits-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#7601](https://github.com/biomejs/biome/issues/7601): Properly match Grit plugin's code snippet with only one child.

crates/biome_grit_patterns/src/grit_code_snippet.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::grit_context::{GritExecContext, GritQueryContext};
22
use crate::grit_resolved_pattern::GritResolvedPattern;
33
use crate::grit_target_node::GritTargetSyntaxKind;
4+
use biome_js_syntax::JsSyntaxKind;
45
use grit_pattern_matcher::binding::Binding;
56
use grit_pattern_matcher::context::ExecContext;
67
use grit_pattern_matcher::pattern::{
78
CodeSnippet, DynamicPattern, Matcher, Pattern, PatternName, ResolvedPattern, State,
89
};
9-
use grit_util::AnalysisLogs;
1010
use grit_util::error::GritResult;
11+
use grit_util::{AnalysisLogs, AstNode};
1112

1213
#[derive(Clone, Debug)]
1314
pub struct GritCodeSnippet {
@@ -42,11 +43,28 @@ impl Matcher<GritQueryContext> for GritCodeSnippet {
4243
return Ok(false);
4344
};
4445

45-
if let Some((_, pattern)) = self.patterns.iter().find(|(kind, _)| *kind == node.kind()) {
46-
pattern.execute(resolved, state, context, logs)
47-
} else {
48-
Ok(false)
46+
// First, try to match with the exact kind (fast path)
47+
if let Some((_, pattern)) = self.patterns.iter().find(|(kind, _)| *kind == node.kind())
48+
&& pattern.execute(resolved, state, context, logs)?
49+
{
50+
return Ok(true);
4951
}
52+
53+
// If node has a single child, try matching against the child
54+
// This handles wrapper nodes like JS_IDENTIFIER_EXPRESSION wrapping JS_REFERENCE_IDENTIFIER
55+
if node.kind() == GritTargetSyntaxKind::JsSyntaxKind(JsSyntaxKind::JS_IDENTIFIER_EXPRESSION)
56+
&& let Some(child) = node.first_child()
57+
&& child.next_sibling().is_none()
58+
{
59+
let child_binding = GritResolvedPattern::from_node_binding(child);
60+
for (_, pattern) in &self.patterns {
61+
if pattern.execute(&child_binding, state, context, logs)? {
62+
return Ok(true);
63+
}
64+
}
65+
}
66+
67+
Ok(false)
5068
}
5169
}
5270

crates/biome_grit_patterns/src/pattern_compiler/snippet_compiler.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,25 @@ mod tests {
494494
use grit_util::Parser;
495495
use regex::Regex;
496496

497+
#[test]
498+
fn test_snippet_node_from_tree() {
499+
let snippet: SnippetTree<GritTargetTree> =
500+
GritJsParser.parse_snippet("", "buildConfig", "");
501+
let node = node_from_tree(&snippet).expect("no node found");
502+
let formatted = format!("{node:#?}");
503+
insta::assert_snapshot!(&formatted, @r#"
504+
GritTargetNode {
505+
node: JsLanguage(
506+
Node(
507+
508+
0: [email protected] "buildConfig" [] []
509+
,
510+
),
511+
),
512+
}
513+
"#);
514+
}
515+
497516
#[test]
498517
fn test_node_from_tree() {
499518
let snippet = GritJsParser.parse_snippet("", "console.log('hello')", "");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`$fn($args)` where {
2+
$fn <: `buildConfig`
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/biome_grit_patterns/tests/spec_tests.rs
3+
expression: matchBacktickSnippet
4+
---
5+
SnapshotResult {
6+
messages: [],
7+
matched_ranges: [
8+
"1:1-1:28",
9+
],
10+
rewritten_files: [],
11+
created_files: [],
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
buildConfig({ foo: "bar" });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`$fn($args)` where {
2+
$fn <: "buildConfig"
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/biome_grit_patterns/tests/spec_tests.rs
3+
expression: matchStringLiteral
4+
---
5+
SnapshotResult {
6+
messages: [],
7+
matched_ranges: [
8+
"1:1-1:28",
9+
],
10+
rewritten_files: [],
11+
created_files: [],
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
buildConfig({ foo: "bar" });

0 commit comments

Comments
 (0)