Skip to content

Commit 8e81d90

Browse files
committed
fix always reporting new line required for all siblings
1 parent a45e9c4 commit 8e81d90

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

lib/rules/jsx-one-element-per-line.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ module.exports = {
3131
};
3232
}
3333

34+
function elementDoesOpenOrCloseOnLine (element, line) {
35+
const reportableLines = [element.openingElement, element.closingElement].reduce((lines, el) => {
36+
if (!el) {
37+
return lines;
38+
}
39+
40+
return lines.concat([el.loc.start.line, el.loc.end.line]);
41+
}, []);
42+
43+
44+
return reportableLines.indexOf(line) !== -1;
45+
}
46+
47+
function anyElementDoesOpenOrCloseOnLine (elements, line) {
48+
return elements.some(element => elementDoesOpenOrCloseOnLine(element, line));
49+
}
50+
3451
return {
3552
JSXOpeningElement: function (node) {
3653
if (!node.parent) {
@@ -54,9 +71,11 @@ module.exports = {
5471

5572
// Siblings
5673
if (node.parent.parent && node.parent.parent.children && node.parent.parent.children.length) {
57-
const firstSibling = node.parent.parent.children.find(sibling => sibling.type === 'JSXElement');
74+
const firstSiblingOnLine = node.parent.parent.children.find(sibling => (
75+
sibling.type === 'JSXElement' && elementDoesOpenOrCloseOnLine(sibling, openingStartLine)
76+
));
5877

59-
if (firstSibling !== node.parent) {
78+
if (firstSiblingOnLine !== node.parent) {
6079
context.report({
6180
node: node,
6281
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
@@ -73,20 +92,7 @@ module.exports = {
7392

7493
const closingElementStartLine = node.loc.end.line;
7594

76-
const anyChildrenOnLine = node.parent.children.some(child => {
77-
const reportableLines = [child.openingElement, child.closingElement].reduce((lines, el) => {
78-
if (!el) {
79-
return lines;
80-
}
81-
82-
return lines.concat([el.loc.start.line, el.loc.end.line]);
83-
}, []);
84-
85-
86-
return reportableLines.indexOf(closingElementStartLine) !== -1;
87-
});
88-
89-
if (!anyChildrenOnLine) {
95+
if (!anyElementDoesOpenOrCloseOnLine(node.parent.children, closingElementStartLine)) {
9096
return;
9197
}
9298

tests/lib/rules/jsx-one-element-per-line.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ ruleTester.run('jsx-max-elements-per-line', rule, {
4343
' <Foo />',
4444
'</App>'
4545
].join('\n')
46+
}, {
47+
code: [
48+
'<App>',
49+
' <Foo />',
50+
' <Bar />',
51+
'</App>'
52+
].join('\n')
4653
}, {
4754
code: [
4855
'<App>',

0 commit comments

Comments
 (0)