Skip to content

Commit 3893c3d

Browse files
committed
short-circuit childrenOnLine finder when a child is found
1 parent 256e5bc commit 3893c3d

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = {
3939

4040
const openingElementEndLine = node.loc.end.line;
4141

42+
// TODO: Just check if my parent opening tag is on the same line as me.
43+
4244
// Children
4345
if (node.parent.children.length) {
4446
const childrenOnLine = [];
@@ -55,11 +57,11 @@ module.exports = {
5557
return;
5658
}
5759

58-
childrenOnLine.forEach(elementInLine => {
60+
childrenOnLine.forEach(childOnLine => {
5961
context.report({
60-
node: elementInLine,
61-
message: `Opening tag for Element \`${elementInLine.openingElement.name.name}\` must be placed on a new line`,
62-
fix: generateFixFunction(elementInLine)
62+
node: childOnLine,
63+
message: `Opening tag for Element \`${childOnLine.openingElement.name.name}\` must be placed on a new line`,
64+
fix: generateFixFunction(childOnLine)
6365
});
6466
});
6567
}
@@ -85,26 +87,22 @@ module.exports = {
8587
return;
8688
}
8789

88-
const childrenOnLine = [];
8990
const closingElementStartLine = node.loc.end.line;
9091

91-
node.parent.children.forEach(childNode => {
92-
const reportableLines = [childNode.openingElement, childNode.closingElement].reduce((lines, el) => {
92+
const anyChildrenOnLine = node.parent.children.some(child => {
93+
const reportableLines = [child.openingElement, child.closingElement].reduce((lines, el) => {
9394
if (!el) {
9495
return lines;
9596
}
9697

9798
return lines.concat([el.loc.start.line, el.loc.end.line]);
9899
}, []);
99100

100-
if (reportableLines.indexOf(closingElementStartLine) === -1) {
101-
return;
102-
}
103101

104-
childrenOnLine.push(childNode);
102+
return reportableLines.indexOf(closingElementStartLine) !== -1;
105103
});
106104

107-
if (!childrenOnLine.length) {
105+
if (!anyChildrenOnLine) {
108106
return;
109107
}
110108

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,21 @@ ruleTester.run('jsx-max-elements-per-line', rule, {
258258
].join('\n'),
259259
errors: [{message: 'Opening tag for Element `Bar` must be placed on a new line'}],
260260
parserOptions: parserOptions
261+
}, {
262+
code: [
263+
'<App>',
264+
' <Foo>',
265+
' <Bar /></Foo>',
266+
'</App>'
267+
].join('\n'),
268+
output: [
269+
'<App>',
270+
' <Foo>',
271+
' <Bar />',
272+
'</Foo>',
273+
'</App>'
274+
].join('\n'),
275+
errors: [{message: 'Closing tag for Element `Foo` must be placed on a new line'}],
276+
parserOptions: parserOptions
261277
}]
262278
});

0 commit comments

Comments
 (0)