Skip to content

Commit

Permalink
string-content: Fix JSX autofix for newlines, etc. (#2222)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahamguo authored Nov 27, 2023
1 parent f475168 commit b95e75e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rules/string-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions test/string-content.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const createSuggestionError = (match, suggest, output) => [
];

test({
testerOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
valid: [
'const foo = "";',
...[
Expand Down Expand Up @@ -279,5 +286,19 @@ test({
errors: createError('no', 'yes'),
},
/* eslint-enable no-template-curly-in-string */
{
code: outdent`
const foo = <div className='
no
' />
`,
output: outdent`
const foo = <div className='
yes
' />
`,
options: [{patterns: noToYesPattern}],
errors: createError('no', 'yes'),
},
],
});

0 comments on commit b95e75e

Please sign in to comment.