-
Notifications
You must be signed in to change notification settings - Fork 889
Added NaN and (+/-)Infinity as numbers to no-inferrable-types #2885
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ import * as Lint from "../index"; | |
const OPTION_IGNORE_PARMS = "ignore-params"; | ||
const OPTION_IGNORE_PROPERTIES = "ignore-properties"; | ||
|
||
const INFINITY_TEXT = "Infinity"; | ||
|
||
interface Options { | ||
ignoreParameters: boolean; | ||
ignoreProperties: boolean; | ||
|
@@ -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())) { | ||
const fix = Lint.Replacement.deleteFromTo(name.end, type.end); | ||
this.addFailureAtNode(type, Rule.FAILURE_STRING_FACTORY(ts.tokenToString(type.kind)), fix); | ||
} | ||
|
@@ -104,12 +107,12 @@ function shouldCheck(node: ts.Node, { ignoreParameters, ignoreProperties }: Opti | |
} | ||
} | ||
|
||
function typeIsInferrable(type: ts.SyntaxKind, initializer: ts.SyntaxKind): boolean { | ||
function typeIsInferrable(type: ts.SyntaxKind, initializer: ts.SyntaxKind, initializerText: string): boolean { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. A little helper to avoid 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 commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Side note: do we want to include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of consistency we should include There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) |
||
case ts.SyntaxKind.StringKeyword: | ||
switch (initializer) { | ||
case ts.SyntaxKind.StringLiteral: | ||
|
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 callinggetText()