diff --git a/docs/rules/prefer-number-properties.md b/docs/rules/prefer-number-properties.md index ace82475e1..603a8e5fd6 100644 --- a/docs/rules/prefer-number-properties.md +++ b/docs/rules/prefer-number-properties.md @@ -39,14 +39,6 @@ const foo = isFinite(10); if (Object.is(foo, NaN)) {} ``` -```js -const isPositiveZero = value => value === 0 && 1 / value === Infinity; -``` - -```js -const isNegativeZero = value => value === 0 && 1 / value === -Infinity; -``` - ```js const {parseInt} = Number; const foo = parseInt('10', 2); @@ -82,6 +74,14 @@ const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INF const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY; ``` +```js +const isPositiveZero = value => value === 0 && 1 / value === Infinity; +``` + +```js +const isNegativeZero = value => value === 0 && 1 / value === -Infinity; +``` + ## Options Type: `object` @@ -89,9 +89,9 @@ Type: `object` ### checkInfinity Type: `boolean`\ -Default: `true` +Default: `false` -Pass `checkInfinity: false` to disable check on `Infinity`. +Pass `checkInfinity: true` to enable check on `Infinity`. #### Fail @@ -116,3 +116,13 @@ const foo = Infinity; // eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}] const foo = -Infinity; ``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}] +const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY; +``` + +```js +// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}] +const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY; +``` diff --git a/rules/prefer-number-properties.js b/rules/prefer-number-properties.js index 294804487d..94052250b3 100644 --- a/rules/prefer-number-properties.js +++ b/rules/prefer-number-properties.js @@ -78,7 +78,7 @@ const create = context => { const { checkInfinity, } = { - checkInfinity: true, + checkInfinity: false, ...context.options[0], }; const {sourceCode} = context; diff --git a/test/prefer-number-properties.mjs b/test/prefer-number-properties.mjs index f6970fa3a2..fcd21b92e0 100644 --- a/test/prefer-number-properties.mjs +++ b/test/prefer-number-properties.mjs @@ -182,6 +182,33 @@ const errorNaN = [ }, ]; +const errorPositiveInfinity = [ + { + messageId: MESSAGE_ID_ERROR, + data: { + description: 'Infinity', + property: 'POSITIVE_INFINITY', + }, + }, +]; + +const errorNegativeInfinity = [ + { + messageId: MESSAGE_ID_ERROR, + data: { + description: '-Infinity', + property: 'NEGATIVE_INFINITY', + }, + }, +]; + +function withCheckInfinity(code) { + return { + code, + options: [{checkInfinity: true}], + }; +} + test({ valid: [ 'const foo = Number.NaN;', @@ -252,14 +279,8 @@ test({ 'function Infinity() {}', 'class Infinity {}', 'class Foo { Infinity(){}}', - { - code: 'const foo = Infinity;', - options: [{checkInfinity: false}], - }, - { - code: 'const foo = -Infinity;', - options: [{checkInfinity: false}], - }, + 'const foo = Infinity;', + 'const foo = -Infinity;', ], invalid: [ { @@ -307,6 +328,16 @@ test({ output: 'class Foo3 {[Number.NaN] = 1}', errors: errorNaN, }, + { + ...withCheckInfinity('const foo = Infinity;'), + output: 'const foo = Number.POSITIVE_INFINITY;', + errors: errorPositiveInfinity, + }, + { + ...withCheckInfinity('const foo = -Infinity;'), + output: 'const foo = Number.NEGATIVE_INFINITY;', + errors: errorNegativeInfinity, + }, ], }); @@ -370,30 +401,28 @@ test.snapshot({ 'foo[NaN] = 1;', 'class A {[NaN](){}}', 'foo = {[NaN]: 1}', - - 'const foo = Infinity;', - 'if (Number.isNaN(Infinity)) {}', - 'if (Object.is(foo, Infinity)) {}', - 'const foo = bar[Infinity];', - 'const foo = {Infinity};', - 'const foo = {Infinity: Infinity};', - 'const foo = {[Infinity]: -Infinity};', - 'const foo = {[-Infinity]: Infinity};', - 'const foo = {Infinity: -Infinity};', - 'const {foo = Infinity} = {};', - 'const {foo = -Infinity} = {};', - 'const foo = Infinity.toString();', - 'const foo = -Infinity.toString();', - 'const foo = (-Infinity).toString();', - 'const foo = +Infinity;', - 'const foo = +-Infinity;', - 'const foo = -Infinity;', - 'const foo = -(-Infinity);', - 'const foo = 1 - Infinity;', - 'const foo = 1 - -Infinity;', - 'const isPositiveZero = value => value === 0 && 1 / value === Infinity;', - 'const isNegativeZero = value => value === 0 && 1 / value === -Infinity;', - + withCheckInfinity('const foo = Infinity;'), + withCheckInfinity('if (Number.isNaN(Infinity)) {}'), + withCheckInfinity('if (Object.is(foo, Infinity)) {}'), + withCheckInfinity('const foo = bar[Infinity];'), + withCheckInfinity('const foo = {Infinity};'), + withCheckInfinity('const foo = {Infinity: Infinity};'), + withCheckInfinity('const foo = {[Infinity]: -Infinity};'), + withCheckInfinity('const foo = {[-Infinity]: Infinity};'), + withCheckInfinity('const foo = {Infinity: -Infinity};'), + withCheckInfinity('const {foo = Infinity} = {};'), + withCheckInfinity('const {foo = -Infinity} = {};'), + withCheckInfinity('const foo = Infinity.toString();'), + withCheckInfinity('const foo = -Infinity.toString();'), + withCheckInfinity('const foo = (-Infinity).toString();'), + withCheckInfinity('const foo = +Infinity;'), + withCheckInfinity('const foo = +-Infinity;'), + withCheckInfinity('const foo = -Infinity;'), + withCheckInfinity('const foo = -(-Infinity);'), + withCheckInfinity('const foo = 1 - Infinity;'), + withCheckInfinity('const foo = 1 - -Infinity;'), + withCheckInfinity('const isPositiveZero = value => value === 0 && 1 / value === Infinity;'), + withCheckInfinity('const isNegativeZero = value => value === 0 && 1 / value === -Infinity;'), 'const {a = NaN} = {};', 'const {[NaN]: a = NaN} = {};', 'const [a = NaN] = [];', @@ -402,7 +431,7 @@ test.snapshot({ 'function foo([a = NaN]) {}', // Space after keywords - 'function foo() {return-Infinity}', + withCheckInfinity('function foo() {return-Infinity}'), 'globalThis.isNaN(foo);', 'global.isNaN(foo);', @@ -413,7 +442,7 @@ test.snapshot({ 'window.parseFloat(foo);', 'self.parseFloat(foo);', 'globalThis.NaN', - '-globalThis.Infinity', + withCheckInfinity('-globalThis.Infinity'), // Not a call outdent` diff --git a/test/snapshots/prefer-number-properties.mjs.md b/test/snapshots/prefer-number-properties.mjs.md index 9f5072b1e6..659a392d88 100644 --- a/test/snapshots/prefer-number-properties.mjs.md +++ b/test/snapshots/prefer-number-properties.mjs.md @@ -117,6 +117,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -138,6 +148,16 @@ Generated by [AVA](https://avajs.dev). 1 | if (Number.isNaN(Infinity)) {}␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -159,6 +179,16 @@ Generated by [AVA](https://avajs.dev). 1 | if (Object.is(foo, Infinity)) {}␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -180,6 +210,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = bar[Infinity];␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -201,6 +241,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = {Infinity};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -222,6 +272,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = {Infinity: Infinity};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -243,6 +303,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = {[Infinity]: -Infinity};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -271,6 +341,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = {[-Infinity]: Infinity};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -299,6 +379,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = {Infinity: -Infinity};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -320,6 +410,16 @@ Generated by [AVA](https://avajs.dev). 1 | const {foo = Infinity} = {};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -341,6 +441,16 @@ Generated by [AVA](https://avajs.dev). 1 | const {foo = -Infinity} = {};␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -362,6 +472,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = Infinity.toString();␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -383,6 +503,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = -Infinity.toString();␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -404,6 +534,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = (-Infinity).toString();␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -425,6 +565,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = +Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -446,6 +596,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = +-Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -467,6 +627,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = -Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -488,6 +658,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = -(-Infinity);␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -509,6 +689,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = 1 - Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -530,6 +720,16 @@ Generated by [AVA](https://avajs.dev). 1 | const foo = 1 - -Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -551,6 +751,16 @@ Generated by [AVA](https://avajs.dev). 1 | const isPositiveZero = value => value === 0 && 1 / value === Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -572,6 +782,16 @@ Generated by [AVA](https://avajs.dev). 1 | const isNegativeZero = value => value === 0 && 1 / value === -Infinity;␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -733,6 +953,16 @@ Generated by [AVA](https://avajs.dev). 1 | function foo() {return-Infinity}␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ @@ -935,6 +1165,16 @@ Generated by [AVA](https://avajs.dev). 1 | -globalThis.Infinity␊ ` +> Options + + `␊ + [␊ + {␊ + "checkInfinity": true␊ + }␊ + ]␊ + ` + > Output `␊ diff --git a/test/snapshots/prefer-number-properties.mjs.snap b/test/snapshots/prefer-number-properties.mjs.snap index abd5cc5298..ad2f7b6233 100644 Binary files a/test/snapshots/prefer-number-properties.mjs.snap and b/test/snapshots/prefer-number-properties.mjs.snap differ