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

Commit

Permalink
use-isnan should only apply to comparison operators (#2317)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbj42 authored and nchen63 committed Mar 8, 2017
1 parent b78df5a commit c7d934b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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

0 comments on commit c7d934b

Please sign in to comment.