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

use-isnan should only apply to comparison operators #2317

Merged
merged 4 commits into from
Mar 8, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/rules/useIsnanRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Rule extends Lint.Rules.AbstractRule {
class UseIsnanRuleWalker extends Lint.RuleWalker {
protected visitBinaryExpression(node: ts.BinaryExpression): void {
if ((this.isExpressionNaN(node.left) || this.isExpressionNaN(node.right))
&& node.operatorToken.kind !== ts.SyntaxKind.EqualsToken) {
&& this.isComparisonOperator(node.operatorToken.kind)) {
this.addFailureAtNode(node, Rule.FAILURE_STRING + node.getText());
}
super.visitBinaryExpression(node);
Expand All @@ -54,4 +54,15 @@ class UseIsnanRuleWalker extends Lint.RuleWalker {
private isExpressionNaN(node: ts.Node) {
return node.kind === ts.SyntaxKind.Identifier && node.getText() === "NaN";
}

private isComparisonOperator(operator: ts.BinaryOperator) {
return (operator === ts.SyntaxKind.LessThanToken
|| operator === ts.SyntaxKind.GreaterThanToken
|| operator === ts.SyntaxKind.LessThanEqualsToken
|| operator === ts.SyntaxKind.GreaterThanEqualsToken
|| operator === ts.SyntaxKind.EqualsEqualsToken
|| operator === ts.SyntaxKind.ExclamationEqualsToken
|| operator === ts.SyntaxKind.EqualsEqualsEqualsToken
|| operator === ts.SyntaxKind.ExclamationEqualsEqualsToken);
}
}
33 changes: 33 additions & 0 deletions test/rules/use-isnan/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ if (isNaN(something)) { }
// no violation for assignments
let x = 0;
x = NaN;
x += NaN;
x -= NaN;
x /= NaN;
x *= NaN;
x %= NaN;
x **= NaN;
x <<= NaN;
x >>= NaN;
x >>>= NaN;
x &= NaN;
x ^= NaN;
x |= NaN;

// no violation for arithmetic operators
x = x + NaN;
x = NaN - x;
x = x / NaN;
x = NaN * x;
x = x % NaN;
x = NaN ** x;

// no violation for bitwise operators
x = x & NaN;
x = NaN | x;
x = x ^ NaN;
x = NaN << x;
x = x >> NaN;
x = NaN >>> x;
x = x & NaN;

// no violation for other operators
if (x instanceof NaN) { }
if (NaN in x) { }

// do not use equality operators to compare for NaN
if (foo == NaN) { }
Expand Down