@@ -104,14 +104,21 @@ module.exports = {
104
104
return node . loc . start . line !== node . loc . end . line ;
105
105
}
106
106
107
- function report ( node , message , fixerFn ) {
107
+ function report ( node , message , fix ) {
108
108
context . report ( {
109
- node : node ,
110
- message : message ,
111
- fix : fixerFn
109
+ node,
110
+ message,
111
+ fix
112
112
} ) ;
113
113
}
114
114
115
+ function trimTokenBeforeNewline ( node , tokenBefore ) {
116
+ // if the token before the jsx is a bracket or curly brace
117
+ // we don't want a space between the opening parentheses and the multiline jsx
118
+ const isBracket = tokenBefore . value === '{' || tokenBefore . value === '[' ;
119
+ return `${ tokenBefore . value . trim ( ) } ${ isBracket ? '' : ' ' } ` ;
120
+ }
121
+
115
122
function check ( node , type ) {
116
123
if ( ! node || node . type !== 'JSXElement' ) {
117
124
return ;
@@ -125,7 +132,21 @@ module.exports = {
125
132
126
133
if ( option === 'parens-new-line' && isMultilines ( node ) ) {
127
134
if ( ! isParenthesised ( node ) ) {
128
- report ( node , MISSING_PARENS , fixer => fixer . replaceText ( node , `(\n${ sourceCode . getText ( node ) } \n)` ) ) ;
135
+ const tokenBefore = sourceCode . getTokenBefore ( node , { includeComments : true } ) ;
136
+ const tokenAfter = sourceCode . getTokenAfter ( node , { includeComments : true } ) ;
137
+ if ( tokenBefore . loc . end . line < node . loc . start . line ) {
138
+ // Strip newline after operator if parens newline is specified
139
+ report (
140
+ node ,
141
+ MISSING_PARENS ,
142
+ fixer => fixer . replaceTextRange (
143
+ [ tokenBefore . range [ 0 ] , tokenAfter . range [ 0 ] ] ,
144
+ `${ trimTokenBeforeNewline ( node , tokenBefore ) } (\n${ sourceCode . getText ( node ) } \n)`
145
+ )
146
+ ) ;
147
+ } else {
148
+ report ( node , MISSING_PARENS , fixer => fixer . replaceText ( node , `(\n${ sourceCode . getText ( node ) } \n)` ) ) ;
149
+ }
129
150
} else if ( needsNewLines ( node ) ) {
130
151
report ( node , PARENS_NEW_LINES , fixer => fixer . replaceText ( node , `\n${ sourceCode . getText ( node ) } \n` ) ) ;
131
152
}
@@ -171,7 +192,7 @@ module.exports = {
171
192
}
172
193
} ,
173
194
174
- 'ArrowFunctionExpression:exit' : function ( node ) {
195
+ 'ArrowFunctionExpression:exit' : function ( node ) {
175
196
const arrowBody = node . body ;
176
197
const type = 'arrow' ;
177
198
@@ -195,7 +216,7 @@ module.exports = {
195
216
}
196
217
} ,
197
218
198
- JSXAttribute : function ( node ) {
219
+ JSXAttribute : function ( node ) {
199
220
const type = 'prop' ;
200
221
if ( isEnabled ( type ) && node . value && node . value . type === 'JSXExpressionContainer' ) {
201
222
check ( node . value . expression , type ) ;
0 commit comments