Skip to content

Commit

Permalink
refactor: modify code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh committed Mar 6, 2024
1 parent a259377 commit 67c097e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
32 changes: 20 additions & 12 deletions packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Expand Up @@ -54,12 +54,11 @@ export default createRule<[], MessageId>({
}

function isLiteral(expression: TSESTree.Expression): boolean {
return (
expression.type === AST_NODE_TYPES.Literal ||
(expression.type === AST_NODE_TYPES.TemplateLiteral &&
expression.expressions.length === 0 &&
expression.quasis.length === 1)
);
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 {
Expand Down Expand Up @@ -115,13 +114,9 @@ export default createRule<[], MessageId>({
}

const fixableExpressions = node.expressions.filter(
(
expression,
): expression is
| TSESTree.Literal
| TSESTree.TemplateLiteral
| TSESTree.Identifier =>
expression =>
isLiteral(expression) ||
isTemplateLiteral(expression) ||
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
isNaNIdentifier(expression),
Expand Down Expand Up @@ -155,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 @@ -61,11 +61,6 @@ ruleTester.run('no-useless-template-literals', rule, {
\`\${left}\${center}\${right}\`;
`,

`
declare const num: 1;
\`a\${\`b\${num}\`}c\`;
`,

'`1 + 1 = ${1 + 1}`;',

'`true && false = ${true && false}`;',
Expand Down Expand Up @@ -136,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 @@ -363,39 +362,91 @@ 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: '`a${`${`b`}c`}`;',
output: '`a${`bc`}`;',
code: '`use${`less`}`;',
output: '`useless`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 8,
endColumn: 11,
},
],
},

{
code: '`a${`bc`}`;',
output: '`abc`;',
code: `
declare const nested: string, interpolation: string;
\`use\${\`less\${nested}\${interpolation}\`}\`;
`,
output: `
declare const nested: string, interpolation: string;
\`useless\${nested}\${interpolation}\`;
`,
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 5,
},
],
},

{
code: noFormat`
\`u\${
// hopefully this comment is not needed.
'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

0 comments on commit 67c097e

Please sign in to comment.