Skip to content

Commit

Permalink
fix(grit): match object literal (#3447)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr committed Jul 15, 2024
1 parent d761fb9 commit ad44329
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/biome_grit_patterns/src/grit_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'a> Binding<'a, GritQueryContext> for GritBinding<'a> {

fn list_items(&self) -> Option<impl Iterator<Item = GritTargetNode<'a>> + Clone> {
match self {
Self::Node(node) if node.is_list() => Some(node.children()),
Self::Node(node) if node.is_list() => Some(node.named_children()),
_ => None,
}
}
Expand Down
49 changes: 49 additions & 0 deletions crates/biome_grit_patterns/src/grit_target_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ impl<'a> GritTargetNode<'a> {
})
}

pub fn named_children(&self) -> impl Iterator<Item = Self> + Clone {
NamedChildrenIterator::new(self)
}

#[inline]
pub fn end_byte(&self) -> u32 {
self.text_trimmed_range().end().into()
Expand Down Expand Up @@ -415,6 +419,47 @@ impl<'a> Iterator for ChildrenIterator<'a> {
}
}

#[derive(Clone, Debug)]
pub struct NamedChildrenIterator<'a> {
cursor: Option<GritTargetNodeCursor<'a>>,
}

impl<'a> NamedChildrenIterator<'a> {
fn new(node: &GritTargetNode<'a>) -> Self {
let mut cursor = GritTargetNodeCursor::new(node);
let mut cursor = cursor.goto_first_child().then_some(cursor);
if let Some(c) = cursor.as_mut() {
while c.is_at_token() {
if !c.goto_next_sibling() {
cursor = None;
break;
}
}
}
Self { cursor }
}
}

impl<'a> Iterator for NamedChildrenIterator<'a> {
type Item = GritTargetNode<'a>;

fn next(&mut self) -> Option<Self::Item> {
let c = self.cursor.as_mut()?;
let node = c.node();
if c.goto_next_sibling() {
while c.is_at_token() {
if !c.goto_next_sibling() {
self.cursor = None;
break;
}
}
} else {
self.cursor = None;
}
Some(node)
}
}

#[derive(Clone, Debug)]
struct GritTargetNodeCursor<'a> {
node: GritTargetNode<'a>,
Expand All @@ -428,6 +473,10 @@ impl<'a> GritTargetNodeCursor<'a> {
root: node.clone(),
}
}

fn is_at_token(&self) -> bool {
self.node.is_token()
}
}

impl<'a> AstCursor for GritTargetNodeCursor<'a> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fn pattern_arg_from_slot(
if slot.contains_list() {
let mut nodes_list: Vec<Pattern<GritQueryContext>> = match &slot {
GritSyntaxSlot::Node(node) => node
.children()
.named_children()
.map(|n| pattern_from_node(&n, context_range, range_map, context, is_rhs))
.collect::<Result<_, CompileError>>()?,
_ => Vec::new(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`{ foo: 1, bar: "bar" }`
23 changes: 23 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/objectLiteral.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/biome_grit_patterns/tests/spec_tests.rs
expression: objectLiteral
---
SnapshotResult {
messages: [],
matched_ranges: [
Range {
start: Position {
line: 3,
column: 15,
},
end: Position {
line: 6,
column: 6,
},
start_byte: 42,
end_byte: 85,
},
],
rewritten_files: [],
created_files: [],
}
7 changes: 7 additions & 0 deletions crates/biome_grit_patterns/tests/specs/ts/objectLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

function objectLiteral() {
const a = {
foo: 1,
bar: "bar",
}
}

0 comments on commit ad44329

Please sign in to comment.