Skip to content

Commit

Permalink
[Fix] jsx-curly-brace-presence: avoid autofixing attributes with do…
Browse files Browse the repository at this point in the history
…uble quotes to a double quoted attribute

Fixes #3814
  • Loading branch information
ljharb committed Sep 3, 2024
1 parent 25ae093 commit cf868f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Added
* [`no-string-refs`]: allow this.refs in > 18.3.0 ([#3807][] @henryqdineen)

### Fixed
* [`jsx-curly-brace-presence`]: avoid autofixing attributes with double quotes to a double quoted attribute ([#3814][] @ljharb)

[#3814]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3814
[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807

## [7.35.1] - 2024.09.02
Expand Down
12 changes: 8 additions & 4 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,14 @@ module.exports = {
const parentType = JSXExpressionNode.parent.type;

if (parentType === 'JSXAttribute') {
textToReplace = `"${expressionType === 'TemplateLiteral'
? expression.quasis[0].value.raw
: expression.raw.slice(1, -1)
}"`;
if (expressionType !== 'TemplateLiteral' && /["]/.test(expression.raw.slice(1, -1))) {
textToReplace = expression.raw;
} else {
textToReplace = `"${expressionType === 'TemplateLiteral'
? expression.quasis[0].value.raw
: expression.raw.slice(1, -1)
}"`;
}
} else if (jsxUtil.isJSX(expression)) {
textToReplace = getText(context, expression);
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,5 +945,15 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
output: `<Foo bar="'" />`,
errors: [{ messageId: 'unnecessaryCurly' }],
},
{
code: `
<Foo help={'The maximum time range for searches. (i.e. "P30D" for 30 days, "PT24H" for 24 hours)'} />
`,
options: ['never'],
output: `
<Foo help='The maximum time range for searches. (i.e. "P30D" for 30 days, "PT24H" for 24 hours)' />
`,
errors: [{ messageId: 'unnecessaryCurly' }],
}
)),
});

0 comments on commit cf868f2

Please sign in to comment.