Skip to content

Commit 8bdddbd

Browse files
flag useless nested template literals
1 parent 6954a4a commit 8bdddbd

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

packages/eslint-plugin/src/rules/no-useless-template-literals.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ export default createRule<[], MessageId>({
110110
}
111111

112112
const fixableExpressions = node.expressions.filter(
113-
(expression): expression is TSESTree.Literal | TSESTree.Identifier =>
113+
expression =>
114114
isLiteral(expression) ||
115115
isUndefinedIdentifier(expression) ||
116116
isInfinityIdentifier(expression) ||
117-
isNaNIdentifier(expression),
117+
isNaNIdentifier(expression) ||
118+
expression.type === AST_NODE_TYPES.TemplateLiteral,
118119
);
119120

120121
fixableExpressions.forEach(expression => {
@@ -145,6 +146,19 @@ export default createRule<[], MessageId>({
145146
const escapedValue = stringValue.replace(/([`$\\])/g, '\\$1');
146147

147148
fixes.push(fixer.replaceText(expression, escapedValue));
149+
} else if (expression.type === AST_NODE_TYPES.TemplateLiteral) {
150+
// Note that some template literals get handled in the previous branch too.
151+
// Remove the beginning and trailing backtick characters.
152+
fixes.push(
153+
fixer.removeRange([
154+
expression.range[0],
155+
expression.range[0] + 1,
156+
]),
157+
fixer.removeRange([
158+
expression.range[1] - 1,
159+
expression.range[1],
160+
]),
161+
);
148162
}
149163

150164
return fixes;

packages/eslint-plugin/tests/rules/no-useless-template-literals.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ ruleTester.run('no-useless-template-literals', rule, {
131131
noFormat`
132132
\`with windows \r new line\`;
133133
`,
134+
135+
`
136+
\`not a useless \${String.raw\`nested interpolation \${a}\`}\`;
137+
`,
134138
],
135139

136140
invalid: [
@@ -555,5 +559,67 @@ ruleTester.run('no-useless-template-literals', rule, {
555559
},
556560
],
557561
},
562+
563+
{
564+
code: "`use${'less'}`;",
565+
output: '`useless`;',
566+
errors: [
567+
{
568+
messageId: 'noUselessTemplateLiteral',
569+
line: 1,
570+
},
571+
],
572+
},
573+
574+
{
575+
code: `
576+
declare const nested: string, interpolation: string;
577+
\`use\${\`less\${nested}\${interpolation}\`}\`;
578+
`,
579+
output: `
580+
declare const nested: string, interpolation: string;
581+
\`useless\${nested}\${interpolation}\`;
582+
`,
583+
errors: [
584+
{
585+
messageId: 'noUselessTemplateLiteral',
586+
},
587+
],
588+
},
589+
590+
{
591+
code: noFormat`
592+
\`u\${
593+
// hopefully this comment is not needed.
594+
'se'
595+
596+
}\${
597+
\`le\${ \`ss\` }\`
598+
}\`;
599+
`,
600+
output: `
601+
\`use\${
602+
\`less\`
603+
}\`;
604+
`,
605+
errors: [
606+
{
607+
messageId: 'noUselessTemplateLiteral',
608+
line: 4,
609+
},
610+
{
611+
messageId: 'noUselessTemplateLiteral',
612+
line: 7,
613+
column: 3,
614+
endLine: 7,
615+
},
616+
{
617+
messageId: 'noUselessTemplateLiteral',
618+
line: 7,
619+
column: 10,
620+
endLine: 7,
621+
},
622+
],
623+
},
558624
],
559625
});

0 commit comments

Comments
 (0)