Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-useless-template-literal] do not render escaped strings in autofixes #8688

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the comment, one thing though that I want to flag is that in the issue it was also intended that

`${'a'}`; 
// should autofix to 
`a`;
// rather than 
'a';

@kirkwaiblinger, Ohh, I missed that in the original issue!

I'm 50/50 on this!

though it's possible that that flew under the radar on the issue itself as well when it was marked accepting PRs.

Maybe..

Fixing to string literal vs template literal sounds a bit stylistic to me, I think it would be great to hear more opinions on this!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. I will say, at the time, I didn't realize it was just removing the wrapping template syntax, as opposed to overriding whatever the user had typed with one particular quote style. So my motivation for that change has softened a lot as well. It still feels a little surprising to me, but it does have a very justifiable internal logic as-is.

So unless someone comes in and has a strong opinion, I'd say, just leave it as-is :)

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createRule,
getConstrainedTypeAtLocation,
getParserServices,
getStaticStringValue,
isTypeFlagSet,
isUndefinedIdentifier,
} from '../util';
Expand Down Expand Up @@ -53,7 +52,9 @@ export default createRule<[], MessageId>({
return isString(type);
}

function isLiteral(expression: TSESTree.Expression): boolean {
function isLiteral(
expression: TSESTree.Expression,
): expression is TSESTree.Literal {
return expression.type === AST_NODE_TYPES.Literal;
}

Expand Down Expand Up @@ -144,10 +145,11 @@ export default createRule<[], MessageId>({
]),
];

const stringValue = getStaticStringValue(expression);

if (stringValue != null) {
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1');
if (isLiteral(expression)) {
const escapedValue =
auvred marked this conversation as resolved.
Show resolved Hide resolved
typeof expression.value === 'string'
? expression.raw.slice(1, -1).replace(/([`$])/g, '\\$1')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit, optional) - why are the escaping patterns different for each branch? Maybe worth a comment?

Copy link
Member Author

@auvred auvred Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does seem a bit confusing

Does e07e14607+ae589b436 clear it up?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oof, i think i get it, but i still struggle tbh 😆 It's just really tricky to me to figure out which things are in escaped-land and which things are non-escaped in the code comment there (it's even hard for me to type out the special characters in this comment 😆 ). Is the idea
raw string ⇒

  1. Removing enclosing quotation characters
  2. just add escapes to characters that need escape in template string, but didn't as a string literal (` and $)

cooked string ⇒

  1. escape everything that needs escapes (`, $, and \)

I really think a few English words would be helpful in the code comments (to go along with the examples), since my brain is not big enough to make meaning of all the special characters at a glance 😄

: String(expression.value).replace(/([`$\\])/g, '\\$1');

fixes.push(fixer.replaceText(expression, escapedValue));
} else if (isTemplateLiteral(expression)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,48 @@ ruleTester.run('no-useless-template-literals', rule, {
},
],
},
{
code: '`${0o25}`;',
output: '`21`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 8,
},
],
},
{
code: '`${0b1010} ${0b1111}`;',
output: '`10 15`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 10,
},
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 14,
endColumn: 20,
},
],
},
{
code: '`${0x25}`;',
output: '`37`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 8,
},
],
},
{
code: '`${/a/}`;',
output: '`/a/`;',
Expand All @@ -174,6 +216,18 @@ ruleTester.run('no-useless-template-literals', rule, {
},
],
},
{
code: '`${/a/gim}`;',
output: '`/a/gim`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 10,
},
],
},

{
code: noFormat`\`\${ 1 }\`;`,
Expand Down Expand Up @@ -637,5 +691,79 @@ declare const nested: string, interpolation: string;
},
],
},

{
code: '`some ${/`/} string`;',
output: '`some /\\`/ string`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
},
],
},
{
code: '`some ${/${}/} string`;',
output: '`some /\\${}/ string`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
},
],
},
{
code: '`some ${/\\\\/} string`;',
output: '`some /\\\\\\\\/ string`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
},
],
},

{
code: "`${'\\u00E5'}`;",
output: "'\\u00E5';",
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: "`${'\\n'}`;",
output: "'\\n';",
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: "` ${'\\u00E5'} `;",
output: '` \\u00E5 `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: "` ${'\\n'} `;",
output: '` \\n `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: noFormat`\` \${"\\n"} \`;`,
output: '` \\n `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: '` ${`\\n`} `;',
output: '` \\n `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: noFormat`\` \${ 'A\\u0307\\u0323' } \`;`,
output: '` A\\u0307\\u0323 `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: "` ${'👨‍👩‍👧‍👦'} `;",
output: '` 👨‍👩‍👧‍👦 `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
{
code: "` ${'\\ud83d\\udc68'} `;",
output: '` \\ud83d\\udc68 `;',
errors: [{ messageId: 'noUselessTemplateLiteral' }],
},
auvred marked this conversation as resolved.
Show resolved Hide resolved
],
});