Skip to content

Commit 04fbe74

Browse files
vedadeeptaljharb
authored andcommitted
[Fix] jsx-curly-brace-presence: bail out checks when JSXElements are passed as props
Per #2423
1 parent 20cc016 commit 04fbe74

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

lib/rules/jsx-curly-brace-presence.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,21 @@ module.exports = {
114114
* @param {ASTNode} JSXExpressionNode - The AST node with an unnecessary JSX expression
115115
*/
116116
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-
126117
context.report({
127118
node: JSXExpressionNode,
128119
message: 'Curly braces are unnecessary here.',
129120
fix(fixer) {
121+
const expression = JSXExpressionNode.expression;
122+
const expressionType = expression.type;
123+
const parentType = JSXExpressionNode.parent.type;
124+
130125
let textToReplace;
131126
if (parentType === 'JSXAttribute') {
132127
textToReplace = `"${expressionType === 'TemplateLiteral' ?
133128
expression.quasis[0].value.raw :
134129
expression.raw.substring(1, expression.raw.length - 1)
135130
}"`;
136-
} else if (isJSX) {
131+
} else if (jsxUtil.isJSX(expression)) {
137132
const sourceCode = context.getSourceCode();
138133

139134
textToReplace = sourceCode.getText(expression);
@@ -244,6 +239,20 @@ module.exports = {
244239
}
245240

246241
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+
247256
// If there are adjacent `JsxExpressionContainer` then there is no need,
248257
// to check for unnecessary curly braces.
249258
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {

0 commit comments

Comments
 (0)