Skip to content

Commit

Permalink
[Fix] jsx-curly-brace-presence: bail out checks when JSXElements ar…
Browse files Browse the repository at this point in the history
…e passed as props

Per #2423
  • Loading branch information
vedadeepta authored and ljharb committed Oct 1, 2019
1 parent 20cc016 commit 04fbe74
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,21 @@ module.exports = {
* @param {ASTNode} JSXExpressionNode - The AST node with an unnecessary JSX expression
*/
function reportUnnecessaryCurly(JSXExpressionNode) {
const expression = JSXExpressionNode.expression;
const expressionType = expression.type;
const parentType = JSXExpressionNode.parent.type;
const isJSX = jsxUtil.isJSX(expression);

if (parentType === 'JSXAttribute' && isJSX) {
return;
}

context.report({
node: JSXExpressionNode,
message: 'Curly braces are unnecessary here.',
fix(fixer) {
const expression = JSXExpressionNode.expression;
const expressionType = expression.type;
const parentType = JSXExpressionNode.parent.type;

let textToReplace;
if (parentType === 'JSXAttribute') {
textToReplace = `"${expressionType === 'TemplateLiteral' ?
expression.quasis[0].value.raw :
expression.raw.substring(1, expression.raw.length - 1)
}"`;
} else if (isJSX) {
} else if (jsxUtil.isJSX(expression)) {
const sourceCode = context.getSourceCode();

textToReplace = sourceCode.getText(expression);
Expand Down Expand Up @@ -244,6 +239,20 @@ module.exports = {
}

function shouldCheckForUnnecessaryCurly(parent, node, config) {
// Bail out if the parent is a JSXAttribute & its contents aren't
// StringLiteral or TemplateLiteral since e.g
// <App prop1={<CustomEl />} prop2={<CustomEl>...</CustomEl>} />

if (
parent.type && parent.type === 'JSXAttribute' &&
(node.expression && node.expression.type &&
node.expression.type !== 'Literal' &&
node.expression.type !== 'StringLiteral' &&
node.expression.type !== 'TemplateLiteral')
) {
return false;
}

// If there are adjacent `JsxExpressionContainer` then there is no need,
// to check for unnecessary curly braces.
if (jsxUtil.isJSX(parent) && hasAdjacentJsxExpressionContainers(node, parent.children)) {
Expand Down

0 comments on commit 04fbe74

Please sign in to comment.