Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions lib/rules/self-closing-comp.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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>;'
}, {
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>;',
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'
}]
}
]
});