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

use-isnan fails when use NaN in || operator #2255

Closed
timocov opened this issue Feb 27, 2017 · 5 comments
Closed

use-isnan fails when use NaN in || operator #2255

timocov opened this issue Feb 27, 2017 · 5 comments

Comments

@timocov
Copy link
Contributor

timocov commented Feb 27, 2017

Bug Report

  • TSLint version: 4.3.0
  • TypeScript version: 2.2.1
  • Running TSLint via: CLI

TypeScript code being linted

declare var a: number | undefined;
console.log(a || NaN);

with tslint.json configuration:

{
  "rules": {
    "use-isnan": true
  }
}

Actual behavior

Found an invalid comparison for NaN: a || NaN

Expected behavior

No errors

@ajafff
Copy link
Contributor

ajafff commented Feb 27, 2017

But that's not a comparison.
Your code does basically the following: If a is truthy (a !== 0 && a !== undefined && !isNan(a)) log a, else log NaN.
There's nothing wrong in this piece of code and nothing the use-isnan-Rule could detect.

@sbj42
Copy link
Contributor

sbj42 commented Mar 7, 2017

I believe @timocov would agree with you, that use-isnan should not complain about that code, but it does. I was able to reproduce it by adding the following lines to rules/use-isnan/test.ts.lint:

// no violation for logical operators
x = 0 || NaN;
x = 0 && NaN;

Then ran the tests and got:

// no violation for logical operators
x = 0 || NaN;
    ~~~~~~~~  [Found an invalid comparison for NaN: 0 || NaN]
x = 0 && NaN;
    ~~~~~~~~  [Found an invalid comparison for NaN: 0 && NaN]

The implementation of use-isnan has an exception only for the simple assignment operator, but TypeScript has many other operators which are not "comparison" operators. Many of them would be very silly to use with NaN, but nevertheless the rule does not correctly limit its scope.

@ajafff
Copy link
Contributor

ajafff commented Mar 7, 2017

Ah, sorry. I misread the opening post.

@sbj42 you already analyzed the problem. Fixing this should not be that difficult. Would you like to fix it or should I go for it?

@sbj42
Copy link
Contributor

sbj42 commented Mar 7, 2017

I'd like to fix it. I'll give it a try later today.

@sbj42
Copy link
Contributor

sbj42 commented Mar 9, 2017

I believe this is now resolved.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants