Skip to content

Commit

Permalink
prefer-number-properties: Don't require by default for Infinity/`…
Browse files Browse the repository at this point in the history
…-Infinity` to be written as `Number.POSITIVE_INFINITY`/`Number.NEGATIVE_INFINITY` (#2312)
  • Loading branch information
Richienb committed Apr 9, 2024
1 parent 598f57b commit e0dfed2
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 45 deletions.
30 changes: 20 additions & 10 deletions docs/rules/prefer-number-properties.md
Expand Up @@ -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);
Expand Down Expand Up @@ -82,16 +74,24 @@ 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`

### checkInfinity

Type: `boolean`\
Default: `true`
Default: `false`

Pass `checkInfinity: false` to disable check on `Infinity`.
Pass `checkInfinity: true` to enable check on `Infinity`.

#### Fail

Expand All @@ -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;
```
2 changes: 1 addition & 1 deletion rules/prefer-number-properties.js
Expand Up @@ -78,7 +78,7 @@ const create = context => {
const {
checkInfinity,
} = {
checkInfinity: true,
checkInfinity: false,
...context.options[0],
};
const {sourceCode} = context;
Expand Down
97 changes: 63 additions & 34 deletions test/prefer-number-properties.mjs
Expand Up @@ -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;',
Expand Down Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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,
},
],
});

Expand Down Expand Up @@ -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] = [];',
Expand All @@ -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);',
Expand All @@ -413,7 +442,7 @@ test.snapshot({
'window.parseFloat(foo);',
'self.parseFloat(foo);',
'globalThis.NaN',
'-globalThis.Infinity',
withCheckInfinity('-globalThis.Infinity'),

// Not a call
outdent`
Expand Down

0 comments on commit e0dfed2

Please sign in to comment.