Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Added NaN and (+/-)Infinity as numbers to no-inferrable-types #2885

Merged
merged 2 commits into from Jul 7, 2017
Merged

Added NaN and (+/-)Infinity as numbers to no-inferrable-types #2885

merged 2 commits into from Jul 7, 2017

Conversation

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Jun 4, 2017

PR checklist

Overview of change:

Adds the initializer text to logic in typeIsInferrable, and allows it to contain Infinity.
Fixes #2777.

CHANGELOG.md entry:

[enhancement] Added NaN and (+/-)Infinity as numbers to no-inferrable-types

Adds the initializer text to logic in `typeIsInferrable`, and allows it to contain `Infinity`.
Fixes #2777.
@@ -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())) {
Copy link
Contributor

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()

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;
Copy link
Contributor

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");
}

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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 :)

@ajafff
Copy link
Contributor

ajafff commented Jun 5, 2017

@JoshuaKGoldberg Don't forget to update the PR description and changelog entry to state that the rule now also handles negative number and NaN.

@JoshuaKGoldberg JoshuaKGoldberg changed the title Added Infinity and -Infinity as numbers to no-inferrable-types Added NaN and (+/-)Infinity as numbers to no-inferrable-types Jun 5, 2017
@JoshuaKGoldberg
Copy link
Contributor Author

Thanks :)

@adidahiya adidahiya merged commit ff3904a into palantir:master Jul 7, 2017
@JoshuaKGoldberg JoshuaKGoldberg deleted the infinity-inferrable-type branch July 7, 2017 04:01
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no-inferrable-types rule doesn't detect Infinity as an inferrable numeric literal
3 participants