Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] jsx-no-useless-fragment: add ignoreUnsafeChildren flag #2967

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/rules/jsx-no-useless-fragment.md
Expand Up @@ -52,3 +52,28 @@ const cat = <>meow</>

<Fragment key={item.id}>{item.value}</Fragment>
```

## Rule Options

```js
...
"react/jsx-no-useless-fragments": [<enabled>, { "ignoreNeedsMoreChildren": <boolean> }]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ignoreNeedsMoreChildren name could use some work, open to ideas

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something along the lines of ignorePossiblyUnsafeExprs?

...
```

### `ignoreNeedsMoreChildren` (default: `false`)

When `true` the rule will ignore errors related to fragments having enough
children.

Examples of **correct** code for this rule:

```jsx
<></>

<>{children}</>

<>{foo && <Foo/>}</>

<>{foo?.map(x => <Bar x={x}/>)}</>
```
4 changes: 3 additions & 1 deletion lib/rules/jsx-no-useless-fragment.js
Expand Up @@ -96,6 +96,8 @@ module.exports = {
create(context) {
const reactPragma = pragmaUtil.getFromContext(context);
const fragmentPragma = pragmaUtil.getFragmentFromContext(context);
const config = context.options[0] || {};
const ignoreNeedsMoreChildren = !!config.ignoreNeedsMoreChildren;

/**
* Test whether a node is an padding spaces trimmed by react runtime.
Expand Down Expand Up @@ -198,7 +200,7 @@ module.exports = {
return;
}

if (hasLessThanTwoChildren(node) && !isFragmentWithOnlyTextAndIsNotChild(node)) {
if (hasLessThanTwoChildren(node) && !isFragmentWithOnlyTextAndIsNotChild(node) && !ignoreNeedsMoreChildren) {
context.report({
node,
messageId: 'NeedsMoreChidren',
Expand Down
45 changes: 45 additions & 0 deletions tests/lib/rules/jsx-no-useless-fragment.js
Expand Up @@ -67,6 +67,51 @@ ruleTester.run('jsx-no-useless-fragment', rule, {
{
code: '<>{foos.map(foo => foo)}</>',
parser: parsers.BABEL_ESLINT
},
{
// component could require a ReactNode
code: '<></>',
output: null,
options: [{
ignoreNeedsMoreChildren: true
}],
parser: parsers.BABEL_ESLINT
},
{
// children could be undefined
code: '<>{children}</>',
output: null,
options: [{
ignoreNeedsMoreChildren: true
}],
parser: parsers.BABEL_ESLINT
},
{
// props.children could be undefined
code: '<>{props.children}</>',
output: null,
options: [{
ignoreNeedsMoreChildren: true
}],
parser: parsers.BABEL_ESLINT
},
{
// foo could be undefined
code: '<>{foo && <Foo/>}</>',
output: null,
options: [{
ignoreNeedsMoreChildren: true
}],
parser: parsers.BABEL_ESLINT
},
{
// foo could be undefined
code: '<>{foo?.map(x => <Bar id={x.id}/>)}</>',
output: null,
options: [{
ignoreNeedsMoreChildren: true
}],
parser: parsers.BABEL_ESLINT
}
],
invalid: [
Expand Down