From b95e75e25929ead3e368dd3a342bf202639594e3 Mon Sep 17 00:00:00 2001 From: Abraham Guo Date: Mon, 27 Nov 2023 10:47:37 -0600 Subject: [PATCH] `string-content`: Fix JSX autofix for newlines, etc. (#2222) --- rules/string-content.js | 11 +++++++---- test/string-content.mjs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/rules/string-content.js b/rules/string-content.js index f12607f4b0..646d6f696f 100644 --- a/rules/string-content.js +++ b/rules/string-content.js @@ -105,10 +105,13 @@ const create = context => { const fixed = string.replace(regex, suggest); const fix = type === 'Literal' - ? fixer => fixer.replaceText( - node, - escapeString(fixed, raw[0]), - ) + ? fixer => { + const [quote] = raw; + return fixer.replaceText( + node, + node.parent.type === 'JSXAttribute' ? quote + fixed + quote : escapeString(fixed, quote), + ); + } : fixer => replaceTemplateElement( fixer, node, diff --git a/test/string-content.mjs b/test/string-content.mjs index e8ca335909..c52f33fa0e 100644 --- a/test/string-content.mjs +++ b/test/string-content.mjs @@ -45,6 +45,13 @@ const createSuggestionError = (match, suggest, output) => [ ]; test({ + testerOptions: { + parserOptions: { + ecmaFeatures: { + jsx: true, + }, + }, + }, valid: [ 'const foo = "";', ...[ @@ -279,5 +286,19 @@ test({ errors: createError('no', 'yes'), }, /* eslint-enable no-template-curly-in-string */ + { + code: outdent` + const foo =
+ `, + output: outdent` + const foo =
+ `, + options: [{patterns: noToYesPattern}], + errors: createError('no', 'yes'), + }, ], });