@@ -114,26 +114,21 @@ module.exports = {
114
114
* @param {ASTNode } JSXExpressionNode - The AST node with an unnecessary JSX expression
115
115
*/
116
116
function reportUnnecessaryCurly ( JSXExpressionNode ) {
117
- const expression = JSXExpressionNode . expression ;
118
- const expressionType = expression . type ;
119
- const parentType = JSXExpressionNode . parent . type ;
120
- const isJSX = jsxUtil . isJSX ( expression ) ;
121
-
122
- if ( parentType === 'JSXAttribute' && isJSX ) {
123
- return ;
124
- }
125
-
126
117
context . report ( {
127
118
node : JSXExpressionNode ,
128
119
message : 'Curly braces are unnecessary here.' ,
129
120
fix ( fixer ) {
121
+ const expression = JSXExpressionNode . expression ;
122
+ const expressionType = expression . type ;
123
+ const parentType = JSXExpressionNode . parent . type ;
124
+
130
125
let textToReplace ;
131
126
if ( parentType === 'JSXAttribute' ) {
132
127
textToReplace = `"${ expressionType === 'TemplateLiteral' ?
133
128
expression . quasis [ 0 ] . value . raw :
134
129
expression . raw . substring ( 1 , expression . raw . length - 1 )
135
130
} "`;
136
- } else if ( isJSX ) {
131
+ } else if ( jsxUtil . isJSX ( expression ) ) {
137
132
const sourceCode = context . getSourceCode ( ) ;
138
133
139
134
textToReplace = sourceCode . getText ( expression ) ;
@@ -244,6 +239,20 @@ module.exports = {
244
239
}
245
240
246
241
function shouldCheckForUnnecessaryCurly ( parent , node , config ) {
242
+ // Bail out if the parent is a JSXAttribute & its contents aren't
243
+ // StringLiteral or TemplateLiteral since e.g
244
+ // <App prop1={<CustomEl />} prop2={<CustomEl>...</CustomEl>} />
245
+
246
+ if (
247
+ parent . type && parent . type === 'JSXAttribute' &&
248
+ ( node . expression && node . expression . type &&
249
+ node . expression . type !== 'Literal' &&
250
+ node . expression . type !== 'StringLiteral' &&
251
+ node . expression . type !== 'TemplateLiteral' )
252
+ ) {
253
+ return false ;
254
+ }
255
+
247
256
// If there are adjacent `JsxExpressionContainer` then there is no need,
248
257
// to check for unnecessary curly braces.
249
258
if ( jsxUtil . isJSX ( parent ) && hasAdjacentJsxExpressionContainers ( node , parent . children ) ) {
0 commit comments