@@ -68,39 +68,42 @@ module.exports = {
68
68
INLINE_ELEMENTS . has ( elementName ( node ) )
69
69
) ;
70
70
71
+ const handleJSX = node => {
72
+ let lastChild = null ;
73
+ let child = null ;
74
+ ( node . children . concat ( [ null ] ) ) . forEach ( nextChild => {
75
+ if (
76
+ ( lastChild || nextChild ) &&
77
+ ( ! lastChild || isInlineElement ( lastChild ) ) &&
78
+ ( child && ( child . type === 'Literal' || child . type === 'JSXText' ) ) &&
79
+ ( ! nextChild || isInlineElement ( nextChild ) ) &&
80
+ true
81
+ ) {
82
+ if ( lastChild && child . value . match ( TEXT_FOLLOWING_ELEMENT_PATTERN ) ) {
83
+ context . report ( {
84
+ node : lastChild ,
85
+ loc : lastChild . loc . end ,
86
+ message : `Ambiguous spacing after previous element ${ elementName ( lastChild ) } `
87
+ } ) ;
88
+ } else if ( nextChild && child . value . match ( TEXT_PRECEDING_ELEMENT_PATTERN ) ) {
89
+ context . report ( {
90
+ node : nextChild ,
91
+ loc : nextChild . loc . start ,
92
+ message : `Ambiguous spacing before next element ${ elementName ( nextChild ) } `
93
+ } ) ;
94
+ }
95
+ }
96
+ lastChild = child ;
97
+ child = nextChild ;
98
+ } ) ;
99
+ } ;
100
+
71
101
const TEXT_FOLLOWING_ELEMENT_PATTERN = / ^ \s * \n \s * \S / ;
72
102
const TEXT_PRECEDING_ELEMENT_PATTERN = / \S \s * \n \s * $ / ;
73
103
74
104
return {
75
- JSXElement : function ( node ) {
76
- let lastChild = null ;
77
- let child = null ;
78
- ( node . children . concat ( [ null ] ) ) . forEach ( nextChild => {
79
- if (
80
- ( lastChild || nextChild ) &&
81
- ( ! lastChild || isInlineElement ( lastChild ) ) &&
82
- ( child && ( child . type === 'Literal' || child . type === 'JSXText' ) ) &&
83
- ( ! nextChild || isInlineElement ( nextChild ) ) &&
84
- true
85
- ) {
86
- if ( lastChild && child . value . match ( TEXT_FOLLOWING_ELEMENT_PATTERN ) ) {
87
- context . report ( {
88
- node : lastChild ,
89
- loc : lastChild . loc . end ,
90
- message : `Ambiguous spacing after previous element ${ elementName ( lastChild ) } `
91
- } ) ;
92
- } else if ( nextChild && child . value . match ( TEXT_PRECEDING_ELEMENT_PATTERN ) ) {
93
- context . report ( {
94
- node : nextChild ,
95
- loc : nextChild . loc . start ,
96
- message : `Ambiguous spacing before next element ${ elementName ( nextChild ) } `
97
- } ) ;
98
- }
99
- }
100
- lastChild = child ;
101
- child = nextChild ;
102
- } ) ;
103
- }
105
+ JSXElement : handleJSX ,
106
+ JSXFragment : handleJSX
104
107
} ;
105
108
}
106
109
} ;
0 commit comments