-
Notifications
You must be signed in to change notification settings - Fork 889
Added NaN and (+/-)Infinity as numbers to no-inferrable-types #2885
Added NaN and (+/-)Infinity as numbers to no-inferrable-types #2885
Conversation
Adds the initializer text to logic in `typeIsInferrable`, and allows it to contain `Infinity`. Fixes #2777.
src/rules/noInferrableTypesRule.ts
Outdated
@@ -77,7 +79,8 @@ class NoInferrableTypesWalker extends Lint.AbstractWalker<Options> { | |||
const cb = (node: ts.Node): void => { | |||
if (shouldCheck(node, this.options)) { | |||
const { name, type, initializer } = node; | |||
if (type !== undefined && initializer !== undefined && typeIsInferrable(type.kind, initializer.kind)) { | |||
if (type !== undefined && initializer !== undefined | |||
&& typeIsInferrable(type.kind, initializer.kind, initializer.getText())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider passing initializer
as parameter instead of calling getText()
src/rules/noInferrableTypesRule.ts
Outdated
switch (type) { | ||
case ts.SyntaxKind.BooleanKeyword: | ||
return initializer === ts.SyntaxKind.TrueKeyword || initializer === ts.SyntaxKind.FalseKeyword; | ||
case ts.SyntaxKind.NumberKeyword: | ||
return initializer === ts.SyntaxKind.NumericLiteral; | ||
return initializer === ts.SyntaxKind.NumericLiteral || initializerText.indexOf(INFINITY_TEXT) !== -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little helper to avoid getText()
, handle negative numbers, Infinity
, -Infinity
, and NaN
function isNumeric(node: ts.Expression) {
while (isPrefixUnaryExpression(node) &&
(node.operator === ts.SyntaxKind.PlusToken || node.operator === ts.SyntaxKind.MinusToken)) {
node = node.operand;
}
return node.kind === ts.SyntaxKind.NumericLiteral ||
isIdentifier(node) && (node.text === "NaN" || node.text === "Infinity");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Side note: do we want to include "NaN"
for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of consistency we should include NaN
. Otherwise Infinity
shouldn't be included either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Also just noticed I missed an 'f' in the commit for your username. Whoops :)
@JoshuaKGoldberg Don't forget to update the PR description and changelog entry to state that the rule now also handles negative number and |
Thanks :) |
PR checklist
no-inferrable-types
rule doesn't detectInfinity
as an inferrable numeric literal #2777Overview of change:
Adds the initializer text to logic in
typeIsInferrable
, and allows it to containInfinity
.Fixes #2777.
CHANGELOG.md entry:
[enhancement] Added NaN and (+/-)Infinity as numbers to
no-inferrable-types