forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx-one-element-per-line.js
107 lines (86 loc) · 3.07 KB
/
jsx-one-element-per-line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* @fileoverview Limit to one element tag per line in JSX
* @author Mark Ivan Allen <Vydia.com>
*/
'use strict';
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: 'Limit to one element tag per line in JSX',
category: 'Stylistic Issues',
recommended: false
},
fixable: 'whitespace',
schema: []
},
create: function (context) {
const sourceCode = context.getSourceCode();
function generateFixFunction(elementInLine) {
const nodeText = sourceCode.getText(elementInLine);
return function(fixer) {
return fixer.replaceText(elementInLine, `\n${nodeText}`);
};
}
function elementDoesOpenOrCloseOnLine (element, line) {
const reportableLines = [element.openingElement, element.closingElement].reduce((lines, el) => {
if (!el) {
return lines;
}
return lines.concat([el.loc.start.line, el.loc.end.line]);
}, []);
return reportableLines.indexOf(line) !== -1;
}
function anyElementDoesOpenOrCloseOnLine (elements, line) {
return elements.some(element => elementDoesOpenOrCloseOnLine(element, line));
}
return {
JSXOpeningElement: function (node) {
if (!node.parent) {
return;
}
const openingStartLine = node.loc.start.line;
// Parent
if (node.parent.parent && node.parent.parent.openingElement) {
const parentOpeningStartLine = node.parent.parent.openingElement.loc.end.line;
if (parentOpeningStartLine === openingStartLine) {
context.report({
node: node,
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
fix: generateFixFunction(node)
});
}
}
// Siblings
if (node.parent.parent && node.parent.parent.children && node.parent.parent.children.length) {
const firstSiblingOnLine = node.parent.parent.children.find(sibling => (
sibling.type === 'JSXElement' && elementDoesOpenOrCloseOnLine(sibling, openingStartLine)
));
if (firstSiblingOnLine !== node.parent) {
context.report({
node: node,
message: `Opening tag for Element \`${node.name.name}\` must be placed on a new line`,
fix: generateFixFunction(node)
});
}
}
},
JSXClosingElement: function (node) {
if (!node.parent || !node.parent.children.length) {
return;
}
const closingElementStartLine = node.loc.end.line;
if (!anyElementDoesOpenOrCloseOnLine(node.parent.children, closingElementStartLine)) {
return;
}
context.report({
node: node,
message: `Closing tag for Element \`${node.name.name}\` must be placed on a new line`,
fix: generateFixFunction(node)
});
}
};
}
};