Skip to content

Commit

Permalink
feat(plugins/x): add auto-fix to 'prefer-shorthand-fragment', closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rEl1cx authored Jan 3, 2025
1 parent c442638 commit 3bfd9da
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ruleTester.run(RULE_NAME, rule, {
messageId: "preferShorthandFragment",
},
],
output: "<><div /></>",
},
{
code: /* tsx */ `<Fragment><div /></Fragment>`,
Expand All @@ -18,6 +19,24 @@ ruleTester.run(RULE_NAME, rule, {
messageId: "preferShorthandFragment",
},
],
output: "<><div /></>",
},
{
code: /* tsx */ `
<React.Fragment>
<div />
</React.Fragment>
`,
errors: [
{
messageId: "preferShorthandFragment",
},
],
output: `
<>
<div />
</>
`,
},
],
valid: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const RULE_NAME = "prefer-shorthand-fragment";

export const RULE_FEATURES = [
"CHK",
"FIX",
] as const satisfies RuleFeature[];

export type MessageID = CamelCase<typeof RULE_NAME>;
Expand All @@ -22,6 +23,7 @@ export default createRule<[], MessageID>({
description: "enforce using fragment syntax instead of 'Fragment' component",
[Symbol.for("rule_features")]: RULE_FEATURES,
},
fixable: "code",
messages: {
preferShorthandFragment: "Use fragment shorthand syntax instead of 'Fragment' component.",
},
Expand All @@ -36,6 +38,14 @@ export default createRule<[], MessageID>({
return O.some({
messageId: "preferShorthandFragment",
node,
fix: (fixer) => {
const { closingElement, openingElement } = node;
if (!closingElement) return [];
return [
fixer.replaceTextRange([openingElement.range[0], openingElement.range[1]], "<>"),
fixer.replaceTextRange([closingElement.range[0], closingElement.range[1]], "</>"),
];
},
});
}
return {
Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/rules/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
| [`prefer-react-namespace-import`](prefer-react-namespace-import) | 0️⃣ | `🔍` `🔧` | Enforces React is imported via a namespace import | |
| [`prefer-read-only-props`](prefer-read-only-props) | 0️⃣ | `🔍` `💭` | Enforces read-only props in components. | |
| [`prefer-shorthand-boolean`](prefer-shorthand-boolean) | 0️⃣ | `🔍` `🔧` | Enforces using shorthand syntax for boolean attributes. | |
| [`prefer-shorthand-fragment`](prefer-shorthand-fragment) | 0️⃣ | `🔍` | Enforces using shorthand syntax for fragments. | |
| [`prefer-shorthand-fragment`](prefer-shorthand-fragment) | 0️⃣ | `🔍` `🔧` | Enforces using shorthand syntax for fragments. | |
| [`use-jsx-vars`](use-jsx-vars) | 1️⃣ | | Helpes `eslint/no-unused-vars` to correctly detect variables used in JSX. | |

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/rules/prefer-shorthand-fragment.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ react-x/prefer-shorthand-fragment

**Features**

`🔍`
`🔍` `🔧`

## What it does

Expand Down
2 changes: 1 addition & 1 deletion website/pages/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

- [ ] `function-component-definition`
- [ ] `no-useless-fragment`
- [ ] `prefer-shorthand-fragment`
- [x] `prefer-shorthand-fragment`
- [x] `prefer-react-namespace-import`
- [x] `prefer-shorthand-boolean`

Expand Down

0 comments on commit 3bfd9da

Please sign in to comment.