@@ -31,6 +31,23 @@ module.exports = {
31
31
} ;
32
32
}
33
33
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
+
34
51
return {
35
52
JSXOpeningElement : function ( node ) {
36
53
if ( ! node . parent ) {
@@ -54,9 +71,11 @@ module.exports = {
54
71
55
72
// Siblings
56
73
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
+ ) ) ;
58
77
59
- if ( firstSibling !== node . parent ) {
78
+ if ( firstSiblingOnLine !== node . parent ) {
60
79
context . report ( {
61
80
node : node ,
62
81
message : `Opening tag for Element \`${ node . name . name } \` must be placed on a new line` ,
@@ -73,20 +92,7 @@ module.exports = {
73
92
74
93
const closingElementStartLine = node . loc . end . line ;
75
94
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 ) ) {
90
96
return ;
91
97
}
92
98
0 commit comments