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-literals] detect TemplateLiteral #8575

Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Expand Up @@ -57,6 +57,10 @@ export default createRule<[], MessageId>({
return expression.type === AST_NODE_TYPES.Literal;
}

function isTemplateLiteral(expression: TSESTree.Expression): boolean {
return expression.type === AST_NODE_TYPES.TemplateLiteral;
}

function isInfinityIdentifier(expression: TSESTree.Expression): boolean {
return (
expression.type === AST_NODE_TYPES.Identifier &&
Expand Down Expand Up @@ -110,8 +114,9 @@ export default createRule<[], MessageId>({
}

const fixableExpressions = node.expressions.filter(
(expression): expression is TSESTree.Literal | TSESTree.Identifier =>
expression =>
isLiteral(expression) ||
isTemplateLiteral(expression) ||
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
isNaNIdentifier(expression),
Expand Down Expand Up @@ -145,6 +150,19 @@ export default createRule<[], MessageId>({
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1');

fixes.push(fixer.replaceText(expression, escapedValue));
} else if (isTemplateLiteral(expression)) {
// Note that some template literals get handled in the previous branch too.
// Remove the beginning and trailing backtick characters.
fixes.push(
fixer.removeRange([
expression.range[0],
expression.range[0] + 1,
]),
fixer.removeRange([
expression.range[1] - 1,
expression.range[1],
]),
);
}

return fixes;
Expand Down
Expand Up @@ -131,6 +131,10 @@ ruleTester.run('no-useless-template-literals', rule, {
noFormat`
\`with windows \r new line\`;
`,

`
\`not a useless \${String.raw\`nested interpolation \${a}\`}\`;
`,
],

invalid: [
Expand Down Expand Up @@ -358,14 +362,92 @@ ruleTester.run('no-useless-template-literals', rule, {
},

{
code: "`a${'b'}`;",
output: '`ab`;',
code: "`use${'less'}`;",
output: '`useless`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 5,
endColumn: 8,
},
],
},

{
code: '`use${`less`}`;',
output: '`useless`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
},
],
},

{
code: `
declare const nested: string, interpolation: string;
\`use\${\`less\${nested}\${interpolation}\`}\`;
`,
output: `
declare const nested: string, interpolation: string;
\`useless\${nested}\${interpolation}\`;
`,
errors: [
{
messageId: 'noUselessTemplateLiteral',
},
],
},

{
code: noFormat`
\`u\${
// hopefully this comment is not needed.
Copy link
Member

Choose a reason for hiding this comment

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

[Non-Actionable] Oh, right, this reminds me I'd wanted to file a low-priority followup bug about the comments being removed. Thanks 😄 #8609

'se'

}\${
\`le\${ \`ss\` }\`
}\`;
`,
output: `
\`use\${
\`less\`
}\`;
`,
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 4,
},
{
messageId: 'noUselessTemplateLiteral',
line: 7,
column: 3,
endLine: 7,
},
{
messageId: 'noUselessTemplateLiteral',
line: 7,
column: 10,
endLine: 7,
},
],
},
{
code: noFormat`
\`use\${
\`less\`
}\`;
`,
output: `
\`useless\`;
`,
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 3,
column: 3,
endColumn: 9,
},
],
},
Expand Down