From 45ba6bcb5658052b7fcd989871b620e5301f6c63 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 3 Sep 2024 09:25:27 -0700 Subject: [PATCH] [Fix] `jsx-curly-brace-presence`: avoid autofixing attributes with double quotes to a double quoted attribute Fixes #3814 (backport from cf868f28) --- CHANGELOG.md | 5 +++++ lib/rules/jsx-curly-brace-presence.js | 12 ++++++++---- tests/lib/rules/jsx-curly-brace-presence.js | 10 ++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4387a1bae..eebad21f9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/jsx-curly-brace-presence.js b/lib/rules/jsx-curly-brace-presence.js index cfbe3bda8d..0f17143ffa 100755 --- a/lib/rules/jsx-curly-brace-presence.js +++ b/lib/rules/jsx-curly-brace-presence.js @@ -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 { diff --git a/tests/lib/rules/jsx-curly-brace-presence.js b/tests/lib/rules/jsx-curly-brace-presence.js index 70fd884030..59d756737b 100755 --- a/tests/lib/rules/jsx-curly-brace-presence.js +++ b/tests/lib/rules/jsx-curly-brace-presence.js @@ -945,5 +945,15 @@ ruleTester.run('jsx-curly-brace-presence', rule, { output: ``, errors: [{ messageId: 'unnecessaryCurly' }], }, + { + code: ` + + `, + options: ['never'], + output: ` + + `, + errors: [{ messageId: 'unnecessaryCurly' }], + } )), });