Skip to content

Commit

Permalink
[Fix] jsx-no-useless-fragment: Allow insignificant whitespace when …
Browse files Browse the repository at this point in the history
…`allowExpressions: true`
  • Loading branch information
benj-dobs authored and ljharb committed Aug 30, 2021
1 parent 9487a17 commit cc95a82
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
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

0 comments on commit cc95a82

Please sign in to comment.