Skip to content

Commit 652683d

Browse files
committed
refactor rule to simply check parent line instead of all children
1 parent 3893c3d commit 652683d

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

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

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,32 @@ module.exports = {
3737
return;
3838
}
3939

40-
const openingElementEndLine = node.loc.end.line;
40+
const openingStartLine = node.loc.start.line;
4141

42-
// TODO: Just check if my parent opening tag is on the same line as me.
42+
// Parent
43+
if (node.parent.parent && node.parent.parent.openingElement) {
44+
const parentOpeningStartLine = node.parent.parent.openingElement.loc.end.line;
4345

44-
// Children
45-
if (node.parent.children.length) {
46-
const childrenOnLine = [];
47-
48-
node.parent.children.forEach(childNode => {
49-
if (!childNode.openingElement || childNode.openingElement.loc.start.line !== openingElementEndLine) {
50-
return;
51-
}
52-
53-
childrenOnLine.push(childNode);
54-
});
55-
56-
if (!childrenOnLine.length) {
57-
return;
58-
}
59-
60-
childrenOnLine.forEach(childOnLine => {
46+
if (parentOpeningStartLine === openingStartLine) {
6147
context.report({
62-
node: childOnLine,
63-
message: `Opening tag for Element \`${childOnLine.openingElement.name.name}\` must be placed on a new line`,
64-
fix: generateFixFunction(childOnLine)
48+
node: node,
49+
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
50+
fix: generateFixFunction(node)
6551
});
66-
});
52+
}
6753
}
6854

6955
// Siblings
7056
if (node.parent.parent && node.parent.parent.children && node.parent.parent.children.length) {
7157
const firstSibling = node.parent.parent.children.find(sibling => sibling.type === 'JSXElement');
7258

73-
if (firstSibling === node.parent) {
74-
return;
59+
if (firstSibling !== node.parent) {
60+
context.report({
61+
node: node,
62+
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
63+
fix: generateFixFunction(node)
64+
});
7565
}
76-
77-
context.report({
78-
node: node,
79-
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
80-
fix: generateFixFunction(node)
81-
});
8266
}
8367
},
8468

0 commit comments

Comments
 (0)