Skip to content

Commit cc95a82

Browse files
benj-dobsljharb
authored andcommitted
[Fix] jsx-no-useless-fragment: Allow insignificant whitespace when allowExpressions: true
1 parent 9487a17 commit cc95a82

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Fixed
9+
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)
10+
11+
[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061
12+
813
## [7.25.1] - 2021.08.29
914

1015
### Fixed

docs/rules/jsx-no-useless-fragment.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ Examples of **correct** code for the rule, when `"allowExpressions"` is `true`:
6464

6565
```jsx
6666
<>{foo}</>
67+
68+
<>
69+
{foo}
70+
</>
6771
```

lib/rules/jsx-no-useless-fragment.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ function containsCallExpression(node) {
7777
&& node.expression.type === 'CallExpression';
7878
}
7979

80-
function isFragmentWithSingleExpression(node) {
81-
return node && node.children.length === 1 && node.children[0].type === 'JSXExpressionContainer';
82-
}
83-
8480
module.exports = {
8581
meta: {
8682
type: 'suggestion',
@@ -115,6 +111,15 @@ module.exports = {
115111
&& arrayIncludes(node.raw, '\n');
116112
}
117113

114+
function isFragmentWithSingleExpression(node) {
115+
const children = node && node.children.filter((child) => !isPaddingSpaces(child));
116+
return (
117+
children
118+
&& children.length === 1
119+
&& children[0].type === 'JSXExpressionContainer'
120+
);
121+
}
122+
118123
/**
119124
* Test whether a JSXElement has less than two children, excluding paddings spaces.
120125
* @param {JSXElement|JSXFragment} node

tests/lib/rules/jsx-no-useless-fragment.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ ruleTester.run('jsx-no-useless-fragment', rule, {
7272
code: '<>{moo}</>',
7373
parser: parsers.BABEL_ESLINT,
7474
options: [{allowExpressions: true}]
75+
},
76+
{
77+
code: `
78+
<>
79+
{moo}
80+
</>
81+
`,
82+
parser: parsers.BABEL_ESLINT,
83+
options: [{allowExpressions: true}]
7584
}
7685
],
7786
invalid: [

0 commit comments

Comments
 (0)