Commit 1c15288
refactor(linter): extract duplicated
This PR addresses review comments from PR #12988 by extracting the
duplicated `is_jsx_fragment` function that was present in both
`jsx_fragments.rs` and `jsx_no_useless_fragment.rs` rules.
## Changes Made
### Code Deduplication
- **Extracted** `is_jsx_fragment` function from both React linting rules
- **Moved** the function to `crates/oxc_linter/src/utils/react.rs` as a
shared utility
- **Updated** both `jsx_fragments` and `jsx_no_useless_fragment` rules
to use the shared function
- **Removed** duplicate implementations and cleaned up unused imports
### Self-Closing Fragment Handling
The review also mentioned handling self-closing fragments (e.g.,
`<Fragment />`). Upon investigation, this was already correctly
implemented in the original code:
```rust
let Some(closing_element) = &jsx_elem.closing_element else {
return; // Skip self-closing elements
};
```
Self-closing fragments cannot be converted to shorthand syntax (`<></>`)
and should not trigger the rule, which is the correct behavior already
in place.
## Function Details
The extracted `is_jsx_fragment` function recognizes both forms of React
fragments:
- `<Fragment>` (identifier reference)
- `<React.Fragment>` (member expression)
```rust
pub fn is_jsx_fragment(elem: &JSXOpeningElement) -> bool {
match &elem.name {
JSXElementName::IdentifierReference(ident) => ident.name == "Fragment",
JSXElementName::MemberExpression(mem_expr) => {
if let JSXMemberExpressionObject::IdentifierReference(ident) = &mem_expr.object {
ident.name == "React" && mem_expr.property.name == "Fragment"
} else {
false
}
}
_ => false,
}
}
```
## Testing
- ✅ All existing tests continue to pass (755/755)
- ✅ Specific jsx-related tests pass (49/49)
- ✅ Both `jsx_fragments` and `jsx_no_useless_fragment` rules maintain
their behavior
- ✅ Self-closing fragment test cases verify correct behavior (included
in existing test suite)
This refactoring eliminates code duplication while maintaining full
backward compatibility and correctness of both linting rules.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>is_jsx_fragment function to shared utils (#13093)1 parent 0b61338 commit 1c15288
File tree
3 files changed
+22
-38
lines changed- crates/oxc_linter/src
- rules/react
- utils
3 files changed
+22
-38
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | | - | |
| 7 | + | |
11 | 8 | | |
12 | 9 | | |
13 | 10 | | |
| |||
178 | 175 | | |
179 | 176 | | |
180 | 177 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | 178 | | |
198 | 179 | | |
199 | 180 | | |
| |||
Lines changed: 2 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | | - | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
321 | 322 | | |
322 | 323 | | |
323 | 324 | | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | 325 | | |
341 | 326 | | |
342 | 327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
0 commit comments