|
1 | 1 | use crate::grit_context::{GritExecContext, GritQueryContext}; |
2 | 2 | use crate::grit_resolved_pattern::GritResolvedPattern; |
3 | 3 | use crate::grit_target_node::GritTargetSyntaxKind; |
| 4 | +use biome_js_syntax::JsSyntaxKind; |
4 | 5 | use grit_pattern_matcher::binding::Binding; |
5 | 6 | use grit_pattern_matcher::context::ExecContext; |
6 | 7 | use grit_pattern_matcher::pattern::{ |
7 | 8 | CodeSnippet, DynamicPattern, Matcher, Pattern, PatternName, ResolvedPattern, State, |
8 | 9 | }; |
9 | | -use grit_util::AnalysisLogs; |
10 | 10 | use grit_util::error::GritResult; |
| 11 | +use grit_util::{AnalysisLogs, AstNode}; |
11 | 12 |
|
12 | 13 | #[derive(Clone, Debug)] |
13 | 14 | pub struct GritCodeSnippet { |
@@ -42,11 +43,28 @@ impl Matcher<GritQueryContext> for GritCodeSnippet { |
42 | 43 | return Ok(false); |
43 | 44 | }; |
44 | 45 |
|
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); |
49 | 51 | } |
| 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) |
50 | 68 | } |
51 | 69 | } |
52 | 70 |
|
|
0 commit comments