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]self-closing-comp: stop reporting single-line spaces #2210

Merged
merged 1 commit into from Apr 12, 2019
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
22 changes: 13 additions & 9 deletions lib/rules/self-closing-comp.js
Expand Up @@ -44,23 +44,27 @@ module.exports = {
return node.name && node.name.type === 'JSXIdentifier' && !jsxUtil.isDOMComponent(node);
}

function hasChildren(node) {
function childrenIsEmpty(node) {
return node.parent.children.length === 0;
}

function childrenIsMultilineSpaces(node) {
const childrens = node.parent.children;
if (
!childrens.length ||
(childrens.length === 1 && (childrens[0].type === 'Literal' || childrens[0].type === 'JSXText') && !childrens[0].value.replace(/(?!\xA0)\s/g, ''))
) {
return false;
}
return true;

return (
childrens.length === 1 &&
(childrens[0].type === 'Literal' || childrens[0].type === 'JSXText') &&
childrens[0].value.indexOf('\n') !== -1 &&
childrens[0].value.replace(/(?!\xA0)\s/g, '') === ''
);
}

function isShouldBeSelfClosed(node) {
const configuration = Object.assign({}, optionDefaults, context.options[0]);
return (
configuration.component && isComponent(node) ||
configuration.html && jsxUtil.isDOMComponent(node)
) && !node.selfClosing && !hasChildren(node);
) && !node.selfClosing && (childrenIsEmpty(node) || childrenIsMultilineSpaces(node));
}

// --------------------------------------------------------------------------
Expand Down
31 changes: 10 additions & 21 deletions tests/lib/rules/self-closing-comp.js
Expand Up @@ -37,6 +37,10 @@ ruleTester.run('self-closing-comp', rule, {
<Hello name="John" />
</Hello>
`
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;'
ljharb marked this conversation as resolved.
Show resolved Hide resolved
}, {
code: 'var HelloJohn = <div>&nbsp;</div>;'
}, {
Expand All @@ -56,6 +60,12 @@ ruleTester.run('self-closing-comp', rule, {
</Hello>
`,
options: []
}, {
code: 'var HelloJohn = <div> </div>;',
options: []
}, {
code: 'var HelloJohn = <div> </div>;',
ljharb marked this conversation as resolved.
Show resolved Hide resolved
options: []
}, {
code: 'var HelloJohn = <div>&nbsp;</div>;',
options: []
Expand Down Expand Up @@ -117,13 +127,6 @@ ruleTester.run('self-closing-comp', rule, {
message: 'Empty components are self-closing'
}]
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
output: 'var HelloJohn = <Hello name="John" />;',
errors: [{
message: 'Empty components are self-closing'
}]
},
{
code: 'var HelloJohn = <Hello name="John"></Hello>;',
output: 'var HelloJohn = <Hello name="John" />;',
options: [],
Expand All @@ -137,13 +140,6 @@ ruleTester.run('self-closing-comp', rule, {
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var HelloJohn = <Hello name="John"> </Hello>;',
output: 'var HelloJohn = <Hello name="John" />;',
options: [],
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"></div>;',
output: 'var contentContainer = <div className="content" />;',
Expand All @@ -158,13 +154,6 @@ ruleTester.run('self-closing-comp', rule, {
errors: [{
message: 'Empty components are self-closing'
}]
}, {
code: 'var contentContainer = <div className="content"> </div>;',
output: 'var contentContainer = <div className="content" />;',
options: [{html: true}],
errors: [{
message: 'Empty components are self-closing'
}]
}
]
});