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

[Fix] jsx-no-useless-fragments: Allow whitespace when allowExpressions is true #3061

Merged
merged 1 commit into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Fixed
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)

[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061

## [7.25.1] - 2021.08.29

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/jsx-no-useless-fragment.md
Expand Up @@ -64,4 +64,8 @@ Examples of **correct** code for the rule, when `"allowExpressions"` is `true`:

```jsx
<>{foo}</>

<>
{foo}
</>
```
13 changes: 9 additions & 4 deletions lib/rules/jsx-no-useless-fragment.js
Expand Up @@ -77,10 +77,6 @@ function containsCallExpression(node) {
&& node.expression.type === 'CallExpression';
}

function isFragmentWithSingleExpression(node) {
return node && node.children.length === 1 && node.children[0].type === 'JSXExpressionContainer';
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -115,6 +111,15 @@ module.exports = {
&& arrayIncludes(node.raw, '\n');
}

function isFragmentWithSingleExpression(node) {
const children = node && node.children.filter((child) => !isPaddingSpaces(child));
return (
children
&& children.length === 1
&& children[0].type === 'JSXExpressionContainer'
);
}

/**
* Test whether a JSXElement has less than two children, excluding paddings spaces.
* @param {JSXElement|JSXFragment} node
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/jsx-no-useless-fragment.js
Expand Up @@ -72,6 +72,15 @@ ruleTester.run('jsx-no-useless-fragment', rule, {
code: '<>{moo}</>',
parser: parsers.BABEL_ESLINT,
options: [{allowExpressions: true}]
},
{
code: `
<>
{moo}
</>
`,
parser: parsers.BABEL_ESLINT,
options: [{allowExpressions: true}]
}
],
invalid: [
Expand Down