Skip to content

Commit 256e5bc

Browse files
committed
fix sibling reporting
1 parent 7f6784e commit 256e5bc

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ module.exports = {
3838
}
3939

4040
const openingElementEndLine = node.loc.end.line;
41-
const openingElementEndColumn = node.loc.end.column;
4241

4342
// Children
4443
if (node.parent.children.length) {
@@ -67,30 +66,16 @@ module.exports = {
6766

6867
// Siblings
6968
if (node.parent.parent && node.parent.parent.children && node.parent.parent.children.length) {
70-
const siblingsOnLine = [];
71-
node.parent.parent.children.forEach(childNode => {
72-
if (
73-
node.parent === childNode ||
74-
!childNode.openingElement ||
75-
childNode.openingElement.loc.start.line !== openingElementEndLine ||
76-
childNode.openingElement.loc.start.column < openingElementEndColumn
77-
) {
78-
return;
79-
}
69+
const firstSibling = node.parent.parent.children.find(sibling => sibling.type === 'JSXElement');
8070

81-
siblingsOnLine.push(childNode);
82-
});
83-
84-
if (!siblingsOnLine.length) {
71+
if (firstSibling === node.parent) {
8572
return;
8673
}
8774

88-
siblingsOnLine.forEach(elementInLine => {
89-
context.report({
90-
node: elementInLine,
91-
message: `Opening tag for Element \`${elementInLine.openingElement.name.name}\` must be placed on a new line`,
92-
fix: generateFixFunction(elementInLine)
93-
});
75+
context.report({
76+
node: node,
77+
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
78+
fix: generateFixFunction(node)
9479
});
9580
}
9681
},

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,21 @@ ruleTester.run('jsx-max-elements-per-line', rule, {
242242
].join('\n'),
243243
errors: [{message: 'Closing tag for Element `App` must be placed on a new line'}],
244244
parserOptions: parserOptions
245+
}, {
246+
code: [
247+
'<App>',
248+
' <Foo></',
249+
'Foo><Bar />',
250+
'</App>'
251+
].join('\n'),
252+
output: [
253+
'<App>',
254+
' <Foo></',
255+
'Foo>',
256+
'<Bar />',
257+
'</App>'
258+
].join('\n'),
259+
errors: [{message: 'Opening tag for Element `Bar` must be placed on a new line'}],
260+
parserOptions: parserOptions
245261
}]
246262
});

0 commit comments

Comments
 (0)