From f20c9e51672aa0bf54007bc1d26915a821fb76a6 Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Fri, 24 Feb 2023 15:51:20 -0800 Subject: [PATCH] [jsx-curly-brace-presence] handle single and only expression template literals --- lib/rules/jsx-curly-brace-presence.js | 10 ++++++++++ tests/lib/rules/jsx-curly-brace-presence.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/rules/jsx-curly-brace-presence.js b/lib/rules/jsx-curly-brace-presence.js index c7b3e55d2e..28b72e86eb 100755 --- a/lib/rules/jsx-curly-brace-presence.js +++ b/lib/rules/jsx-curly-brace-presence.js @@ -131,6 +131,10 @@ module.exports = { return containsLineTerminators(text) && text.trim() === ''; } + function isSingleExpressionTemplateLiteral(child) { + return child.type === 'TemplateLiteral' && child.expressions.length === 1 && child.quasis.map((quasis) => quasis.value.raw).join('') === ''; + } + function wrapNonHTMLEntities(text) { const HTML_ENTITY = ''; const withCurlyBraces = text.split(HTML_ENTITY_REGEX()).map((word) => ( @@ -177,6 +181,9 @@ module.exports = { if (jsxUtil.isJSX(expression)) { const sourceCode = context.getSourceCode(); textToReplace = sourceCode.getText(expression); + } else if (isSingleExpressionTemplateLiteral(expression)) { + const sourceCode = context.getSourceCode(); + textToReplace = `{${sourceCode.getText(expression.expressions[0])}}`; } else { const expressionType = expression && expression.type; const parentType = JSXExpressionNode.parent.type; @@ -279,6 +286,9 @@ module.exports = { && !containsQuoteCharacters(expression.quasis[0].value.cooked) ) { reportUnnecessaryCurly(JSXExpressionNode); + } else if ( + isSingleExpressionTemplateLiteral(expression)) { + reportUnnecessaryCurly(JSXExpressionNode); } else if (jsxUtil.isJSX(expression)) { reportUnnecessaryCurly(JSXExpressionNode); } diff --git a/tests/lib/rules/jsx-curly-brace-presence.js b/tests/lib/rules/jsx-curly-brace-presence.js index 8c66929233..284d008bfd 100755 --- a/tests/lib/rules/jsx-curly-brace-presence.js +++ b/tests/lib/rules/jsx-curly-brace-presence.js @@ -468,6 +468,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, { `, features: ['no-ts'], options: ['never'], + }, + { + code: '', + options: [{ props: 'never' }], + }, + { + code: '{`${label}${suffix}`}', + options: [{ children: 'never' }], } )), @@ -931,6 +939,18 @@ ruleTester.run('jsx-curly-brace-presence', rule, { errors: [{ messageId: 'unnecessaryCurly' }], options: [{ props: 'never', children: 'never', propElementValues: 'never' }], features: ['no-ts'], + }, + { + code: '', + output: '', + errors: [{ messageId: 'unnecessaryCurly' }], + options: [{ props: 'never', children: 'never', propElementValues: 'never' }], + }, + { + code: '{`${label}`}', + output: '{label}', + errors: [{ messageId: 'unnecessaryCurly' }], + options: [{ props: 'never', children: 'never', propElementValues: 'never' }], } )), });