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] report Infinity & NaN #8295

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
28 changes: 24 additions & 4 deletions packages/eslint-plugin/src/rules/no-useless-template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ export default createRule<[], MessageId>({
return isString(type);
}

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

function isInfinityIdentifier(expression: TSESTree.Expression): boolean {
return (
expression.type === AST_NODE_TYPES.Identifier &&
expression.name === 'Infinity'
);
}

function isNaNIdentifier(expression: TSESTree.Expression): boolean {
return (
expression.type === AST_NODE_TYPES.Identifier &&
expression.name === 'NaN'
);
}
Copy link
Member

Choose a reason for hiding this comment

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

[Style] Nit: both of these functions check expression.type === AST_NODE_TYPES.Identifier. I think it'd be just a bit cleaner code to avoid the duplicate check and instead have a single function for both. WDYT?

This is a super nitpicky nit and definitely not a blocker 😄.

A good potential followup might be to look into writing a general catch-all function for this. Searching expression.type === AST_NODE_TYPES.Identifier shows a few dozen results. Maybe half or more of them or so are similar calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, so I basically took inspiration from this:

function isIdentifier(
init: TSESTree.Expression,
...names: string[]
): boolean {
return (
init.type === AST_NODE_TYPES.Identifier && names.includes(init.name)
);
}

And thought about extracting it to a util, WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Probably useful to have that as a util yeah. I'm not super picky here to be honest.


return {
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) {
Expand Down Expand Up @@ -91,13 +109,15 @@ export default createRule<[], MessageId>({
return;
}

const literalsOrUndefinedExpressions = node.expressions.filter(
const fixableExpressions = node.expressions.filter(
(expression): expression is TSESTree.Literal | TSESTree.Identifier =>
expression.type === AST_NODE_TYPES.Literal ||
isUndefinedIdentifier(expression),
isLiteral(expression) ||
isUndefinedIdentifier(expression) ||
isInfinityIdentifier(expression) ||
isNaNIdentifier(expression),
);

literalsOrUndefinedExpressions.forEach(expression => {
fixableExpressions.forEach(expression => {
context.report({
node: expression,
messageId: 'noUselessTemplateLiteral',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,32 @@ ruleTester.run('no-useless-template-literals', rule, {
],
},

{
code: '`${Infinity}`;',
output: '`Infinity`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 12,
},
],
},

{
code: '`${NaN}`;',
output: '`NaN`;',
errors: [
{
messageId: 'noUselessTemplateLiteral',
line: 1,
column: 4,
endColumn: 7,
},
],
},

{
code: "`${'a'} ${'b'}`;",
output: '`a b`;',
Expand Down