Skip to content

Commit 26d863d

Browse files
committed
[Fix]self-closing-comp: stop reporting single-line spaces
Closes jsx-eslint#1717 React does not trim JSXText if it is single-line spaces. In case like `<span> </span>`, the spaces are left as is.
1 parent 6bb1604 commit 26d863d

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

lib/rules/self-closing-comp.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,27 @@ module.exports = {
4444
return node.name && node.name.type === 'JSXIdentifier' && !jsxUtil.isDOMComponent(node);
4545
}
4646

47-
function hasChildren(node) {
47+
function childrenIsEmpty(node) {
48+
return node.parent.children.length === 0;
49+
}
50+
51+
function childrenIsMultilineSpaces(node) {
4852
const childrens = node.parent.children;
49-
if (
50-
!childrens.length ||
51-
(childrens.length === 1 && (childrens[0].type === 'Literal' || childrens[0].type === 'JSXText') && !childrens[0].value.replace(/(?!\xA0)\s/g, ''))
52-
) {
53-
return false;
54-
}
55-
return true;
53+
54+
return (
55+
childrens.length === 1 &&
56+
(childrens[0].type === 'Literal' || childrens[0].type === 'JSXText') &&
57+
childrens[0].value.indexOf('\n') !== -1 &&
58+
childrens[0].value.replace(/(?!\xA0)\s/g, '') === ''
59+
);
5660
}
5761

5862
function isShouldBeSelfClosed(node) {
5963
const configuration = Object.assign({}, optionDefaults, context.options[0]);
6064
return (
6165
configuration.component && isComponent(node) ||
6266
configuration.html && jsxUtil.isDOMComponent(node)
63-
) && !node.selfClosing && !hasChildren(node);
67+
) && !node.selfClosing && (childrenIsEmpty(node) || childrenIsMultilineSpaces(node));
6468
}
6569

6670
// --------------------------------------------------------------------------

tests/lib/rules/self-closing-comp.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ ruleTester.run('self-closing-comp', rule, {
3737
<Hello name="John" />
3838
</Hello>
3939
`
40+
}, {
41+
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
42+
}, {
43+
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
4044
}, {
4145
code: 'var HelloJohn = <div>&nbsp;</div>;'
4246
}, {
@@ -56,6 +60,12 @@ ruleTester.run('self-closing-comp', rule, {
5660
</Hello>
5761
`,
5862
options: []
63+
}, {
64+
code: 'var HelloJohn = <div> </div>;',
65+
options: []
66+
}, {
67+
code: 'var HelloJohn = <div> </div>;',
68+
options: []
5969
}, {
6070
code: 'var HelloJohn = <div>&nbsp;</div>;',
6171
options: []
@@ -117,13 +127,6 @@ ruleTester.run('self-closing-comp', rule, {
117127
message: 'Empty components are self-closing'
118128
}]
119129
}, {
120-
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
121-
output: 'var HelloJohn = <Hello name="John" />;',
122-
errors: [{
123-
message: 'Empty components are self-closing'
124-
}]
125-
},
126-
{
127130
code: 'var HelloJohn = <Hello name="John"></Hello>;',
128131
output: 'var HelloJohn = <Hello name="John" />;',
129132
options: [],
@@ -137,13 +140,6 @@ ruleTester.run('self-closing-comp', rule, {
137140
errors: [{
138141
message: 'Empty components are self-closing'
139142
}]
140-
}, {
141-
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
142-
output: 'var HelloJohn = <Hello name="John" />;',
143-
options: [],
144-
errors: [{
145-
message: 'Empty components are self-closing'
146-
}]
147143
}, {
148144
code: 'var contentContainer = <div className="content"></div>;',
149145
output: 'var contentContainer = <div className="content" />;',
@@ -158,13 +154,6 @@ ruleTester.run('self-closing-comp', rule, {
158154
errors: [{
159155
message: 'Empty components are self-closing'
160156
}]
161-
}, {
162-
code: 'var contentContainer = <div className="content"> </div>;',
163-
output: 'var contentContainer = <div className="content" />;',
164-
options: [{html: true}],
165-
errors: [{
166-
message: 'Empty components are self-closing'
167-
}]
168157
}
169158
]
170159
});

0 commit comments

Comments
 (0)