Skip to content

Commit

Permalink
[jsx-curly-brace-presence] handle single and only expression template…
Browse files Browse the repository at this point in the history
… literals
  • Loading branch information
taozhou-glean committed Feb 24, 2023
1 parent abb4871 commit f20c9e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<HTML_ENTITY>';
const withCurlyBraces = text.split(HTML_ENTITY_REGEX()).map((word) => (
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
`,
features: ['no-ts'],
options: ['never'],
},
{
code: '<App label={`${label}${suffix}`} />',
options: [{ props: 'never' }],
},
{
code: '<App>{`${label}${suffix}`}</App>',
options: [{ children: 'never' }],
}
)),

Expand Down Expand Up @@ -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: '<App label={`${label}`} />',
output: '<App label={label} />',
errors: [{ messageId: 'unnecessaryCurly' }],
options: [{ props: 'never', children: 'never', propElementValues: 'never' }],
},
{
code: '<App>{`${label}`}</App>',
output: '<App>{label}</App>',
errors: [{ messageId: 'unnecessaryCurly' }],
options: [{ props: 'never', children: 'never', propElementValues: 'never' }],
}
)),
});

0 comments on commit f20c9e5

Please sign in to comment.