From d30de508cd48900cc80afc58f015774203996d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Tue, 7 May 2024 07:21:00 +0200 Subject: [PATCH] `prefer-number-properties`: Add `checkNaN` option (#2315) Co-authored-by: fisker Cheung Co-authored-by: Richie Bendall --- docs/rules/prefer-number-properties.md | 51 ++++++++++++++++++++++++++ rules/prefer-number-properties.js | 21 +++++++++-- test/prefer-number-properties.mjs | 4 ++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/docs/rules/prefer-number-properties.md b/docs/rules/prefer-number-properties.md index 15581b5f24..42c70c0e49 100644 --- a/docs/rules/prefer-number-properties.md +++ b/docs/rules/prefer-number-properties.md @@ -107,6 +107,16 @@ const foo = -Infinity; #### Pass +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}] +const foo = Number.POSITIVE_INFINITY; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}] +const foo = Number.NEGATIVE_INFINITY; +``` + ```js // eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}] const foo = Infinity; @@ -126,3 +136,44 @@ const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INF // eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}] const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY; ``` + +### checkNaN + +Type: `boolean`\ +Default: `true` + +Pass `checkNaN: false` to disable check on `NaN`. + +#### Fail + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}] +const foo = NaN; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}] +const foo = -NaN; +``` + +#### Pass + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}] +const foo = Number.NaN; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}] +const foo = -Number.NaN; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}] +const foo = NaN; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}] +const foo = -NaN; +``` diff --git a/rules/prefer-number-properties.js b/rules/prefer-number-properties.js index 94052250b3..e4acad1854 100644 --- a/rules/prefer-number-properties.js +++ b/rules/prefer-number-properties.js @@ -77,16 +77,25 @@ function checkProperty({node, path: [name]}, sourceCode) { const create = context => { const { checkInfinity, + checkNaN, } = { checkInfinity: false, + checkNaN: true, ...context.options[0], }; const {sourceCode} = context; - let objects = Object.keys(globalObjects); - if (!checkInfinity) { - objects = objects.filter(name => name !== 'Infinity'); - } + const objects = Object.keys(globalObjects).filter(name => { + if (!checkInfinity && name === 'Infinity') { + return false; + } + + if (!checkNaN && name === 'NaN') { + return false; + } + + return true; + }); const tracker = new GlobalReferenceTracker({ objects, @@ -103,6 +112,10 @@ const schema = [ additionalProperties: false, properties: { checkInfinity: { + type: 'boolean', + default: false, + }, + checkNaN: { type: 'boolean', default: true, }, diff --git a/test/prefer-number-properties.mjs b/test/prefer-number-properties.mjs index fcd21b92e0..9c218c3e43 100644 --- a/test/prefer-number-properties.mjs +++ b/test/prefer-number-properties.mjs @@ -281,6 +281,10 @@ test({ 'class Foo { Infinity(){}}', 'const foo = Infinity;', 'const foo = -Infinity;', + { + code: 'const foo = NaN', + options: [{checkNaN: false}], + }, ], invalid: [ {