Skip to content

Commit

Permalink
prefer-number-properties: Add checkNaN option (#2315)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
Co-authored-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
3 people committed May 7, 2024
1 parent bc9d65c commit d30de50
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
51 changes: 51 additions & 0 deletions docs/rules/prefer-number-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
```
21 changes: 17 additions & 4 deletions rules/prefer-number-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -103,6 +112,10 @@ const schema = [
additionalProperties: false,
properties: {
checkInfinity: {
type: 'boolean',
default: false,
},
checkNaN: {
type: 'boolean',
default: true,
},
Expand Down
4 changes: 4 additions & 0 deletions test/prefer-number-properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ test({
'class Foo { Infinity(){}}',
'const foo = Infinity;',
'const foo = -Infinity;',
{
code: 'const foo = NaN',
options: [{checkNaN: false}],
},
],
invalid: [
{
Expand Down

0 comments on commit d30de50

Please sign in to comment.