From e0dfed2659993b8fc844bc19e485bde3af1407c4 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Tue, 9 Apr 2024 23:06:41 +1200 Subject: [PATCH] `prefer-number-properties`: Don't require by default for `Infinity`/`-Infinity` to be written as `Number.POSITIVE_INFINITY`/`Number.NEGATIVE_INFINITY` (#2312) --- docs/rules/prefer-number-properties.md | 30 ++- rules/prefer-number-properties.js | 2 +- test/prefer-number-properties.mjs | 97 ++++--- .../snapshots/prefer-number-properties.mjs.md | 240 ++++++++++++++++++ .../prefer-number-properties.mjs.snap | Bin 2294 -> 2361 bytes 5 files changed, 324 insertions(+), 45 deletions(-) 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 abd5cc529894131901fabd567478e31f56190009..ad2f7b62337b034b48e08561ee8e54085ff821a7 100644 GIT binary patch literal 2361 zcmV-93C8w8RzVK}^;00000000B!Tw8D4L=;Xz2)b_|fq=GjnwE68i8t}?=C&n)0%=*0Hl+zw zOGt>5coWmD9j(3TCA*dAh5HK<5`CqQ`~$>4KtkdNAn^-$;1P)j8@P61l6Hy0U3*7?yeO(b}3~DOZM`?t8Y+{v3Ly@A*Tono63sXRBQTX z+j8v7!2WgIzI}Z~Z50`q#%$a2aMl3M<=nK$4?+m(5cpYIS|S%)YC~<2WpBV`Vr=dF zMX4_roq@HPR?BD+d02LtJLNK0LYd1K$aWXut_XZ4T<&)5G4@6Q!s#&f01)J_0g$e6 z`EwxBX7Cl4!E&K$kGC@xnIiJpAZ;gst1mi110#&9Gr;Vq-^^pkZw(3h5SYK?5Cli{ zSV_XgahHpVolQ-}G|4&2KAK1c;M);54T7c^FC)OXKWOaFA71(_0+3lZeGm`W%t^x3 zQJ1OF-K+_zRRh4um%R)Ktx^)OJkDB1s83e_!uwwOh$>SOZcez|j4?H-rZ+TQvu^oj z)_upxLEz*uq?}OWDAi~oN67Y0Nd2oD>gqMuxW7uQR$I*x(#uSy$?L7ocF5+n`Vt5YB)@j~FFm#oh%_V?395%=uk}(tQblZ#mM1#EPv? z83{S$3OV5jIlJl~Mc>b1# zOTp|c2eMQ0Ihr&(ajb)rodFUTW0$1wz-V_+={YorvNHv}z5M(UVImQHtRoB*nv5ide}S2%lF8>5!VAAraYLe|E}B`DQ|q|0N0Dm+RR-iGan zuI(_UZ3&Qg3Xq6nWUT7j0IGsH#4ReoK`7|w0M-P0a^ggk%(R0ekFn}Wq4J5&Jvbl- zM!(=(6zF#!z;0ps1p*ZanC=9WsD$I3E@{y+=B$>cujdPhig@!T3};`OH&J@0h?CL9 z6vP|UGuxfy1S6&tP0jF~Bnq_etEJSlo?ig$n~0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