Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db2df24

Browse filesBrowse files
StyleShitdanvk
authored andcommitted
fix(eslint-plugin): [no-useless-template-literals] report Infinity & NaN (typescript-eslint#8295)
* fix(eslint-plugin): [no-useless-template-literals] report Infinity & NaN Closes typescript-eslint#8294 * remove unnecessary tests
1 parent 2ecc5e7 commit db2df24
Copy full SHA for db2df24

File tree

Expand file treeCollapse file tree

2 files changed

+50
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+50
-4
lines changed

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

Copy file name to clipboardExpand all lines: packages/eslint-plugin/src/rules/no-useless-template-literals.ts
+24-4Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export default createRule<[], MessageId>({
5353
return isString(type);
5454
}
5555

56+
function isLiteral(expression: TSESTree.Expression): boolean {
57+
return expression.type === AST_NODE_TYPES.Literal;
58+
}
59+
60+
function isInfinityIdentifier(expression: TSESTree.Expression): boolean {
61+
return (
62+
expression.type === AST_NODE_TYPES.Identifier &&
63+
expression.name === 'Infinity'
64+
);
65+
}
66+
67+
function isNaNIdentifier(expression: TSESTree.Expression): boolean {
68+
return (
69+
expression.type === AST_NODE_TYPES.Identifier &&
70+
expression.name === 'NaN'
71+
);
72+
}
73+
5674
return {
5775
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
5876
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) {
@@ -91,13 +109,15 @@ export default createRule<[], MessageId>({
91109
return;
92110
}
93111

94-
const literalsOrUndefinedExpressions = node.expressions.filter(
112+
const fixableExpressions = node.expressions.filter(
95113
(expression): expression is TSESTree.Literal | TSESTree.Identifier =>
96-
expression.type === AST_NODE_TYPES.Literal ||
97-
isUndefinedIdentifier(expression),
114+
isLiteral(expression) ||
115+
isUndefinedIdentifier(expression) ||
116+
isInfinityIdentifier(expression) ||
117+
isNaNIdentifier(expression),
98118
);
99119

100-
literalsOrUndefinedExpressions.forEach(expression => {
120+
fixableExpressions.forEach(expression => {
101121
context.report({
102122
node: expression,
103123
messageId: 'noUselessTemplateLiteral',

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

Copy file name to clipboardExpand all lines: packages/eslint-plugin/tests/rules/no-useless-template-literals.test.ts
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,32 @@ ruleTester.run('no-useless-template-literals', rule, {
280280
],
281281
},
282282

283+
{
284+
code: '`${Infinity}`;',
285+
output: '`Infinity`;',
286+
errors: [
287+
{
288+
messageId: 'noUselessTemplateLiteral',
289+
line: 1,
290+
column: 4,
291+
endColumn: 12,
292+
},
293+
],
294+
},
295+
296+
{
297+
code: '`${NaN}`;',
298+
output: '`NaN`;',
299+
errors: [
300+
{
301+
messageId: 'noUselessTemplateLiteral',
302+
line: 1,
303+
column: 4,
304+
endColumn: 7,
305+
},
306+
],
307+
},
308+
283309
{
284310
code: "`${'a'} ${'b'}`;",
285311
output: '`a b`;',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.