Skip to content

Commit e0dfed2

Browse files
authored
prefer-number-properties: Don't require by default for Infinity/-Infinity to be written as Number.POSITIVE_INFINITY/Number.NEGATIVE_INFINITY (#2312)
1 parent 598f57b commit e0dfed2

5 files changed

+324
-45
lines changed

docs/rules/prefer-number-properties.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ const foo = isFinite(10);
3939
if (Object.is(foo, NaN)) {}
4040
```
4141

42-
```js
43-
const isPositiveZero = value => value === 0 && 1 / value === Infinity;
44-
```
45-
46-
```js
47-
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;
48-
```
49-
5042
```js
5143
const {parseInt} = Number;
5244
const foo = parseInt('10', 2);
@@ -82,16 +74,24 @@ const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INF
8274
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
8375
```
8476

77+
```js
78+
const isPositiveZero = value => value === 0 && 1 / value === Infinity;
79+
```
80+
81+
```js
82+
const isNegativeZero = value => value === 0 && 1 / value === -Infinity;
83+
```
84+
8585
## Options
8686

8787
Type: `object`
8888

8989
### checkInfinity
9090

9191
Type: `boolean`\
92-
Default: `true`
92+
Default: `false`
9393

94-
Pass `checkInfinity: false` to disable check on `Infinity`.
94+
Pass `checkInfinity: true` to enable check on `Infinity`.
9595

9696
#### Fail
9797

@@ -116,3 +116,13 @@ const foo = Infinity;
116116
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
117117
const foo = -Infinity;
118118
```
119+
120+
```js
121+
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
122+
const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INFINITY;
123+
```
124+
125+
```js
126+
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
127+
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
128+
```

rules/prefer-number-properties.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const create = context => {
7878
const {
7979
checkInfinity,
8080
} = {
81-
checkInfinity: true,
81+
checkInfinity: false,
8282
...context.options[0],
8383
};
8484
const {sourceCode} = context;

test/prefer-number-properties.mjs

+63-34
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,33 @@ const errorNaN = [
182182
},
183183
];
184184

185+
const errorPositiveInfinity = [
186+
{
187+
messageId: MESSAGE_ID_ERROR,
188+
data: {
189+
description: 'Infinity',
190+
property: 'POSITIVE_INFINITY',
191+
},
192+
},
193+
];
194+
195+
const errorNegativeInfinity = [
196+
{
197+
messageId: MESSAGE_ID_ERROR,
198+
data: {
199+
description: '-Infinity',
200+
property: 'NEGATIVE_INFINITY',
201+
},
202+
},
203+
];
204+
205+
function withCheckInfinity(code) {
206+
return {
207+
code,
208+
options: [{checkInfinity: true}],
209+
};
210+
}
211+
185212
test({
186213
valid: [
187214
'const foo = Number.NaN;',
@@ -252,14 +279,8 @@ test({
252279
'function Infinity() {}',
253280
'class Infinity {}',
254281
'class Foo { Infinity(){}}',
255-
{
256-
code: 'const foo = Infinity;',
257-
options: [{checkInfinity: false}],
258-
},
259-
{
260-
code: 'const foo = -Infinity;',
261-
options: [{checkInfinity: false}],
262-
},
282+
'const foo = Infinity;',
283+
'const foo = -Infinity;',
263284
],
264285
invalid: [
265286
{
@@ -307,6 +328,16 @@ test({
307328
output: 'class Foo3 {[Number.NaN] = 1}',
308329
errors: errorNaN,
309330
},
331+
{
332+
...withCheckInfinity('const foo = Infinity;'),
333+
output: 'const foo = Number.POSITIVE_INFINITY;',
334+
errors: errorPositiveInfinity,
335+
},
336+
{
337+
...withCheckInfinity('const foo = -Infinity;'),
338+
output: 'const foo = Number.NEGATIVE_INFINITY;',
339+
errors: errorNegativeInfinity,
340+
},
310341
],
311342
});
312343

@@ -370,30 +401,28 @@ test.snapshot({
370401
'foo[NaN] = 1;',
371402
'class A {[NaN](){}}',
372403
'foo = {[NaN]: 1}',
373-
374-
'const foo = Infinity;',
375-
'if (Number.isNaN(Infinity)) {}',
376-
'if (Object.is(foo, Infinity)) {}',
377-
'const foo = bar[Infinity];',
378-
'const foo = {Infinity};',
379-
'const foo = {Infinity: Infinity};',
380-
'const foo = {[Infinity]: -Infinity};',
381-
'const foo = {[-Infinity]: Infinity};',
382-
'const foo = {Infinity: -Infinity};',
383-
'const {foo = Infinity} = {};',
384-
'const {foo = -Infinity} = {};',
385-
'const foo = Infinity.toString();',
386-
'const foo = -Infinity.toString();',
387-
'const foo = (-Infinity).toString();',
388-
'const foo = +Infinity;',
389-
'const foo = +-Infinity;',
390-
'const foo = -Infinity;',
391-
'const foo = -(-Infinity);',
392-
'const foo = 1 - Infinity;',
393-
'const foo = 1 - -Infinity;',
394-
'const isPositiveZero = value => value === 0 && 1 / value === Infinity;',
395-
'const isNegativeZero = value => value === 0 && 1 / value === -Infinity;',
396-
404+
withCheckInfinity('const foo = Infinity;'),
405+
withCheckInfinity('if (Number.isNaN(Infinity)) {}'),
406+
withCheckInfinity('if (Object.is(foo, Infinity)) {}'),
407+
withCheckInfinity('const foo = bar[Infinity];'),
408+
withCheckInfinity('const foo = {Infinity};'),
409+
withCheckInfinity('const foo = {Infinity: Infinity};'),
410+
withCheckInfinity('const foo = {[Infinity]: -Infinity};'),
411+
withCheckInfinity('const foo = {[-Infinity]: Infinity};'),
412+
withCheckInfinity('const foo = {Infinity: -Infinity};'),
413+
withCheckInfinity('const {foo = Infinity} = {};'),
414+
withCheckInfinity('const {foo = -Infinity} = {};'),
415+
withCheckInfinity('const foo = Infinity.toString();'),
416+
withCheckInfinity('const foo = -Infinity.toString();'),
417+
withCheckInfinity('const foo = (-Infinity).toString();'),
418+
withCheckInfinity('const foo = +Infinity;'),
419+
withCheckInfinity('const foo = +-Infinity;'),
420+
withCheckInfinity('const foo = -Infinity;'),
421+
withCheckInfinity('const foo = -(-Infinity);'),
422+
withCheckInfinity('const foo = 1 - Infinity;'),
423+
withCheckInfinity('const foo = 1 - -Infinity;'),
424+
withCheckInfinity('const isPositiveZero = value => value === 0 && 1 / value === Infinity;'),
425+
withCheckInfinity('const isNegativeZero = value => value === 0 && 1 / value === -Infinity;'),
397426
'const {a = NaN} = {};',
398427
'const {[NaN]: a = NaN} = {};',
399428
'const [a = NaN] = [];',
@@ -402,7 +431,7 @@ test.snapshot({
402431
'function foo([a = NaN]) {}',
403432

404433
// Space after keywords
405-
'function foo() {return-Infinity}',
434+
withCheckInfinity('function foo() {return-Infinity}'),
406435

407436
'globalThis.isNaN(foo);',
408437
'global.isNaN(foo);',
@@ -413,7 +442,7 @@ test.snapshot({
413442
'window.parseFloat(foo);',
414443
'self.parseFloat(foo);',
415444
'globalThis.NaN',
416-
'-globalThis.Infinity',
445+
withCheckInfinity('-globalThis.Infinity'),
417446

418447
// Not a call
419448
outdent`

0 commit comments

Comments
 (0)