Skip to content

Commit

Permalink
[Fix] jsx-curly-brace-presence: avoid warning on curlies containing…
Browse files Browse the repository at this point in the history
… quote characters

Fixes #3214
  • Loading branch information
ljharb committed Feb 25, 2022
1 parent 7f44ecc commit fe708e1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`jsx-curly-brace-presence`]: avoid warning on curlies containing quote characters ([#3214][] @ljharb)

[#3214]: https://github.com/yannickcr/eslint-plugin-react/issues/3214

## [7.29.1] - 2022.02.25

### Fixed
Expand Down
16 changes: 6 additions & 10 deletions lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -39,9 +39,7 @@ const messages = {
module.exports = {
meta: {
docs: {
description:
'Disallow unnecessary JSX expressions when literals alone are sufficient '
+ 'or enfore JSX expressions on literals in JSX children or attributes',
description: 'Disallow unnecessary JSX expressions when literals alone are sufficient or enfore JSX expressions on literals in JSX children or attributes',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-curly-brace-presence'),
Expand Down Expand Up @@ -274,13 +272,11 @@ module.exports = {
reportUnnecessaryCurly(JSXExpressionNode);
} else if (
expressionType === 'TemplateLiteral'
&& expression.expressions.length === 0
&& expression.quasis[0].value.raw.indexOf('\n') === -1
&& !isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw)
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw, JSXExpressionNode) && (
jsxUtil.isJSX(JSXExpressionNode.parent)
|| !containsQuoteCharacters(expression.quasis[0].value.cooked)
)
&& expression.expressions.length === 0
&& expression.quasis[0].value.raw.indexOf('\n') === -1
&& !isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw)
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw, JSXExpressionNode)
&& !containsQuoteCharacters(expression.quasis[0].value.cooked)
) {
reportUnnecessaryCurly(JSXExpressionNode);
} else if (jsxUtil.isJSX(expression)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -454,6 +454,11 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
{
code: `<App horror={<div />} />`,
options: [{ propElementValues: 'ignore' }],
},
{
code: `
<script>{\`window.foo = "bar"\`}</script>
`,
}
)),

Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/no-unescaped-entities.js
Expand Up @@ -315,6 +315,25 @@ ruleTester.run('no-unescaped-entities', rule, {
],
},
],
},
{
code: `
<script>window.foo = "bar"</script>
`,
errors: [
{
messageId: 'unescapedEntityAlts',
data: { entity: '"', alts: '`&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`' },
line: 2,
column: 30,
},
{
messageId: 'unescapedEntityAlts',
data: { entity: '"', alts: '`&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`' },
line: 2,
column: 34,
},
],
}
)),
});

0 comments on commit fe708e1

Please sign in to comment.