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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply use-isnan only to comparison operators
The purpose of the rule is only  to prevent things like `x == NaN`.  It should not warn about the use of other operators, such as `x || NaN`.
  • Loading branch information
sbj42 committed Mar 8, 2017
commit 5cec2aac7f66b6480e738ef8fa895ed914c7db42
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);
}
}