@@ -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,17 @@ 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
+ if ( tokenBefore . loc . end . line < node . loc . start . line ) {
137
+ // Strip newline after operator if parens newline is specified
138
+ report (
139
+ node ,
140
+ MISSING_PARENS ,
141
+ fixer => fixer . replaceTextRange ( [ tokenBefore . range [ 0 ] , node . range [ 1 ] ] , `${ trimTokenBeforeNewline ( node , tokenBefore ) } (\n${ sourceCode . getText ( node ) } \n)` )
142
+ ) ;
143
+ } else {
144
+ report ( node , MISSING_PARENS , fixer => fixer . replaceText ( node , `(\n${ sourceCode . getText ( node ) } \n)` ) ) ;
145
+ }
129
146
} else if ( needsNewLines ( node ) ) {
130
147
report ( node , PARENS_NEW_LINES , fixer => fixer . replaceText ( node , `\n${ sourceCode . getText ( node ) } \n` ) ) ;
131
148
}
@@ -135,7 +152,6 @@ module.exports = {
135
152
// --------------------------------------------------------------------------
136
153
// Public
137
154
// --------------------------------------------------------------------------
138
-
139
155
return {
140
156
141
157
VariableDeclarator : function ( node ) {
@@ -171,7 +187,7 @@ module.exports = {
171
187
}
172
188
} ,
173
189
174
- 'ArrowFunctionExpression:exit' : function ( node ) {
190
+ 'ArrowFunctionExpression:exit' : function ( node ) {
175
191
const arrowBody = node . body ;
176
192
const type = 'arrow' ;
177
193
@@ -195,7 +211,7 @@ module.exports = {
195
211
}
196
212
} ,
197
213
198
- JSXAttribute : function ( node ) {
214
+ JSXAttribute : function ( node ) {
199
215
const type = 'prop' ;
200
216
if ( isEnabled ( type ) && node . value && node . value . type === 'JSXExpressionContainer' ) {
201
217
check ( node . value . expression , type ) ;
0 commit comments