Skip to content

Commit ccf5e85

Browse files
committed
fix(grit): correct JSX element slot indices for GritQL patterns
The attributes slot for JSX_SELF_CLOSING_ELEMENT and JSX_OPENING_ELEMENT was incorrectly mapped to index 2, but should be index 3 (accounting for the type_arguments slot at index 2). Changes: - Fix attributes slot index from 2 to 3 for JSX_SELF_CLOSING_ELEMENT - Fix attributes slot index from 2 to 3 for JSX_OPENING_ELEMENT - Add type_arguments slot (index 2) for both element types - Add name and initializer slots for JSX_ATTRIBUTE This enables patterns like jsx_self_closing_element(name = "div") to correctly match JSX elements by their element name.
1 parent 7139aad commit ccf5e85

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

.changeset/fix-jsx-slot-indices.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"biome_grit_patterns": patch
3+
---
4+
5+
Fix incorrect slot indices for JSX elements in GritQL patterns.
6+
7+
The `attributes` slot for `JSX_SELF_CLOSING_ELEMENT` and `JSX_OPENING_ELEMENT` was incorrectly mapped to index 2, but should be index 3 (after `type_arguments` at index 2).
8+
9+
Also adds the missing `type_arguments` slot and adds slots for `JSX_ATTRIBUTE` (`name` and `initializer`).
10+
11+
This enables patterns like `jsx_self_closing_element(name = "div")` to correctly match JSX elements by their name.

crates/biome_grit_patterns/src/grit_target_language/js_target_language.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ impl GritTargetLanguageImpl for JsTargetLanguage {
112112
("children", 1),
113113
("closing_element", 2),
114114
],
115-
JSX_SELF_CLOSING_ELEMENT => &[("name", 1), ("attributes", 2)],
116-
JSX_OPENING_ELEMENT => &[("name", 1), ("attributes", 2)],
115+
JSX_SELF_CLOSING_ELEMENT => &[("name", 1), ("type_arguments", 2), ("attributes", 3)],
116+
JSX_OPENING_ELEMENT => &[("name", 1), ("type_arguments", 2), ("attributes", 3)],
117117
JSX_CLOSING_ELEMENT => &[("name", 2)],
118-
JSX_ATTRIBUTE => &[],
118+
JSX_ATTRIBUTE => &[("name", 0), ("initializer", 1)],
119119
JSX_EXPRESSION_CHILD => &[("expression", 1)],
120120
JSX_TEXT => &[],
121121
JSX_NAMESPACE_NAME => &[("namespace", 0), ("name", 2)],
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
engine biome(1.0)
2+
language js(jsx)
3+
4+
// Test that JSX element slots work correctly
5+
// Match self-closing and opening elements with name "div"
6+
or {
7+
jsx_self_closing_element(name = "div"),
8+
jsx_opening_element(name = "div")
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/biome_grit_patterns/tests/spec_tests.rs
3+
assertion_line: 94
4+
expression: jsx_slots
5+
---
6+
SnapshotResult {
7+
messages: [],
8+
matched_ranges: [
9+
"3:3-3:8",
10+
"4:5-4:12",
11+
],
12+
rewritten_files: [],
13+
created_files: [],
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test file for JSX slot matching
2+
const App = () => (
3+
<div>
4+
<div />
5+
<span />
6+
<img src="test.png" />
7+
<button onClick={() => {}}>Click</button>
8+
</div>
9+
);

0 commit comments

Comments
 (0)