From 4293229709dde105692347241513766e953664dd Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 14 Feb 2020 16:52:28 +0100 Subject: [PATCH] Breaking: use-isnan enforceForSwitchCase default `true` (fixes #12810) (#12913) --- docs/rules/use-isnan.md | 36 ++++++++++++++++++++++++++++++------ lib/rules/use-isnan.js | 4 ++-- tests/lib/rules/use-isnan.js | 28 ++++++++++++++++++---------- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/docs/rules/use-isnan.md b/docs/rules/use-isnan.md index 0d75251e440..ae9fefe36af 100644 --- a/docs/rules/use-isnan.md +++ b/docs/rules/use-isnan.md @@ -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}]*/ @@ -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}]*/ @@ -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: diff --git a/lib/rules/use-isnan.js b/lib/rules/use-isnan.js index cd9ccdbaf89..7b466be75f2 100644 --- a/lib/rules/use-isnan.js +++ b/lib/rules/use-isnan.js @@ -45,7 +45,7 @@ module.exports = { properties: { enforceForSwitchCase: { type: "boolean", - default: false + default: true }, enforceForIndexOf: { type: "boolean", @@ -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; /** diff --git a/tests/lib/rules/use-isnan.js b/tests/lib/rules/use-isnan.js index e494fc014ad..aa1b1746fcd 100644 --- a/tests/lib/rules/use-isnan.js +++ b/tests/lib/rules/use-isnan.js @@ -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 }] @@ -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 }],