Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow case NaN, switch(NaN), indexOf(NaN), and lastIndexOf(NaN) (use-isnan) #1429

Closed
feross opened this issue Sep 30, 2019 · 3 comments
Closed

Comments

@feross
Copy link
Member

feross commented Sep 30, 2019

https://eslint.org/docs/rules/use-isnan

This is a proposal to modify the use-isnan rule.

Current rule setting:

use-isnan: "error"

Proposed rule setting:

["error", {"enforceForSwitchCase": true}]

The switch statement internally uses the === comparison to match the expression's value to a case clause.
Therefore, it can never match case NaN. Also, switch(NaN) can never match a case clause.

Set "enforceForSwitchCase" to true if you want this rule to report case NaN and switch(NaN) in switch statements.

Examples of incorrect code for this rule with "enforceForSwitchCase" option set to true:

/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/

switch (foo) {
    case NaN:
        bar();
        break;
    case 1:
        baz();
        break;
    // ...
}

switch (NaN) {
    case a:
        bar();
        break;
    case b:
        baz();
        break;
    // ...
}

Examples of correct code for this rule with "enforceForSwitchCase" option set to true:

/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/

if (Number.isNaN(foo)) {
    bar();
} else {
    switch (foo) {
        case 1:
            baz();
            break;
        // ...
    }
}

if (Number.isNaN(a)) {
    bar();
} else if (Number.isNaN(b)) {
    baz();
} // ...
@mightyiam
Copy link
Member

Makes sense to me. If anyone is breaking by this that might be a bug rather than good code.

@feross feross modified the milestones: standard 15, standard 16 Oct 22, 2020
@feross
Copy link
Member Author

feross commented Oct 29, 2020

No ecosystem impact. This is shipping in standard 16!

@feross feross changed the title Disallow case NaN and switch(NaN) (use-isnan) Disallow case NaN, switch(NaN), indexOf(NaN), and lastIndexOf(NaN) (use-isnan) Oct 29, 2020
@feross
Copy link
Member Author

feross commented Oct 29, 2020

There's a new option: "enforceForIndexOf": true which disallows the use of indexOf and lastIndexOf methods with NaN. Let's enable it as well.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

2 participants