Skip to content

Commit cf868f2

Browse files
committed
[Fix] jsx-curly-brace-presence: avoid autofixing attributes with double quotes to a double quoted attribute
Fixes #3814
1 parent 25ae093 commit cf868f2

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88
### Added
99
* [`no-string-refs`]: allow this.refs in > 18.3.0 ([#3807][] @henryqdineen)
1010

11+
### Fixed
12+
* [`jsx-curly-brace-presence`]: avoid autofixing attributes with double quotes to a double quoted attribute ([#3814][] @ljharb)
13+
14+
[#3814]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3814
1115
[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807
1216

1317
## [7.35.1] - 2024.09.02

lib/rules/jsx-curly-brace-presence.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,14 @@ module.exports = {
199199
const parentType = JSXExpressionNode.parent.type;
200200

201201
if (parentType === 'JSXAttribute') {
202-
textToReplace = `"${expressionType === 'TemplateLiteral'
203-
? expression.quasis[0].value.raw
204-
: expression.raw.slice(1, -1)
205-
}"`;
202+
if (expressionType !== 'TemplateLiteral' && /["]/.test(expression.raw.slice(1, -1))) {
203+
textToReplace = expression.raw;
204+
} else {
205+
textToReplace = `"${expressionType === 'TemplateLiteral'
206+
? expression.quasis[0].value.raw
207+
: expression.raw.slice(1, -1)
208+
}"`;
209+
}
206210
} else if (jsxUtil.isJSX(expression)) {
207211
textToReplace = getText(context, expression);
208212
} else {

tests/lib/rules/jsx-curly-brace-presence.js

+10
Original file line numberDiff line numberDiff line change
@@ -945,5 +945,15 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
945945
output: `<Foo bar="'" />`,
946946
errors: [{ messageId: 'unnecessaryCurly' }],
947947
},
948+
{
949+
code: `
950+
<Foo help={'The maximum time range for searches. (i.e. "P30D" for 30 days, "PT24H" for 24 hours)'} />
951+
`,
952+
options: ['never'],
953+
output: `
954+
<Foo help='The maximum time range for searches. (i.e. "P30D" for 30 days, "PT24H" for 24 hours)' />
955+
`,
956+
errors: [{ messageId: 'unnecessaryCurly' }],
957+
}
948958
)),
949959
});

0 commit comments

Comments
 (0)