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

(backport from cf868f2)
  • Loading branch information
ljharb committed Sep 3, 2024
1 parent e538ee9 commit 45ba6bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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 autofixing attributes with double quotes to a double quoted attribute ([#3814][] @ljharb)

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

## [7.35.1] - 2024.09.02

### Fixed
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 45ba6bc

Please sign in to comment.