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 1 commit
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
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Expand Up @@ -54,7 +54,10 @@ export default createRule<[], MessageId>({
}

function isLiteral(expression: TSESTree.Expression): boolean {
return expression.type === AST_NODE_TYPES.Literal;
return (
expression.type === AST_NODE_TYPES.Literal ||
expression.type === AST_NODE_TYPES.TemplateLiteral
waynzh marked this conversation as resolved.
Show resolved Hide resolved
);
}

function isInfinityIdentifier(expression: TSESTree.Expression): boolean {
Expand Down Expand Up @@ -110,7 +113,12 @@ export default createRule<[], MessageId>({
}

const fixableExpressions = node.expressions.filter(
(expression): expression is TSESTree.Literal | TSESTree.Identifier =>
(
expression,
): expression is
Copy link
Member

Choose a reason for hiding this comment

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

consider just returning boolean/omitting the return type since the type guard isn't needed for any subsequent code.

It's suspect to me to change the type guards based on a pure implementation change in one of the functions being called.

| TSESTree.Literal
| TSESTree.TemplateLiteral
| TSESTree.Identifier =>
isLiteral(expression) ||
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
Expand Down
Expand Up @@ -370,6 +370,19 @@ ruleTester.run('no-useless-template-literals', rule, {
],
},

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

{
code: "`${'1 + 1 ='} ${2}`;",
output: '`1 + 1 = 2`;',
Expand Down