Skip to content

Commit

Permalink
Breaking: use-isnan enforceForSwitchCase default true (fixes #12810) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Feb 14, 2020
1 parent cf38d0d commit 4293229
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
36 changes: 30 additions & 6 deletions docs/rules/use-isnan.md
Expand Up @@ -45,17 +45,15 @@ if (!isNaN(foo)) {

This rule has an object option, with two options:

* `"enforceForSwitchCase"` when set to `true` disallows `case NaN` and `switch(NaN)` in `switch` statements. Default is `false`, meaning that this rule by default does not warn about `case NaN` or `switch(NaN)`.
* `"enforceForIndexOf"` when set to `true` disallows the use of `indexOf` and `lastIndexOf` methods with `NaN`. Default is `false`, meaning that this rule by default does not warn about `indexOf(NaN)` or `lastIndexOf(NaN)` method calls.
* `"enforceForSwitchCase": true` (default) additionally disallows `case NaN` and `switch(NaN)` in `switch` statements.
* `"enforceForIndexOf": true` additionally disallows the use of `indexOf` and `lastIndexOf` methods with `NaN`. Default is `false`, meaning that this rule by default does not warn about `indexOf(NaN)` or `lastIndexOf(NaN)` method calls.

### enforceForSwitchCase

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`:
Examples of **incorrect** code for this rule with `"enforceForSwitchCase"` option set to `true` (default):

```js
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
Expand All @@ -81,7 +79,7 @@ switch (NaN) {
}
```

Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `true`:
Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `true` (default):

```js
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
Expand All @@ -104,6 +102,32 @@ if (Number.isNaN(a)) {
} // ...
```

Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `false`:

```js
/*eslint use-isnan: ["error", {"enforceForSwitchCase": false}]*/

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

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

### enforceForIndexOf

The following methods internally use the `===` comparison to match the given value with an array element:
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/use-isnan.js
Expand Up @@ -45,7 +45,7 @@ module.exports = {
properties: {
enforceForSwitchCase: {
type: "boolean",
default: false
default: true
},
enforceForIndexOf: {
type: "boolean",
Expand All @@ -66,7 +66,7 @@ module.exports = {

create(context) {

const enforceForSwitchCase = context.options[0] && context.options[0].enforceForSwitchCase;
const enforceForSwitchCase = !context.options[0] || context.options[0].enforceForSwitchCase;
const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf;

/**
Expand Down
28 changes: 18 additions & 10 deletions tests/lib/rules/use-isnan.js
Expand Up @@ -41,16 +41,6 @@ ruleTester.run("use-isnan", rule, {
// enforceForSwitchCase
//------------------------------------------------------------------------------

"switch(NaN) { case foo: break; }",
"switch(foo) { case NaN: break; }",
{
code: "switch(NaN) { case foo: break; }",
options: [{}]
},
{
code: "switch(foo) { case NaN: break; }",
options: [{}]
},
{
code: "switch(NaN) { case foo: break; }",
options: [{ enforceForSwitchCase: false }]
Expand Down Expand Up @@ -282,6 +272,24 @@ ruleTester.run("use-isnan", rule, {
// enforceForSwitchCase
//------------------------------------------------------------------------------

{
code: "switch(NaN) { case foo: break; }",
errors: [{ messageId: "switchNaN", type: "SwitchStatement", column: 1 }]
},
{
code: "switch(foo) { case NaN: break; }",
errors: [{ messageId: "caseNaN", type: "SwitchCase", column: 15 }]
},
{
code: "switch(NaN) { case foo: break; }",
options: [{}],
errors: [{ messageId: "switchNaN", type: "SwitchStatement", column: 1 }]
},
{
code: "switch(foo) { case NaN: break; }",
options: [{}],
errors: [{ messageId: "caseNaN", type: "SwitchCase", column: 15 }]
},
{
code: "switch(NaN) {}",
options: [{ enforceForSwitchCase: true }],
Expand Down

0 comments on commit 4293229

Please sign in to comment.