Skip to content

Commit d30de50

Browse files
gurgundayfiskerRichienb
authoredMay 7, 2024··
prefer-number-properties: Add checkNaN option (#2315)
Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: Richie Bendall <richiebendall@gmail.com>
1 parent bc9d65c commit d30de50

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed
 

‎docs/rules/prefer-number-properties.md

+51
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ const foo = -Infinity;
107107

108108
#### Pass
109109

110+
```js
111+
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
112+
const foo = Number.POSITIVE_INFINITY;
113+
```
114+
115+
```js
116+
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
117+
const foo = Number.NEGATIVE_INFINITY;
118+
```
119+
110120
```js
111121
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": false}]
112122
const foo = Infinity;
@@ -126,3 +136,44 @@ const isPositiveZero = value => value === 0 && 1 / value === Number.POSITIVE_INF
126136
// eslint unicorn/prefer-number-properties: ["error", {"checkInfinity": true}]
127137
const isNegativeZero = value => value === 0 && 1 / value === Number.NEGATIVE_INFINITY;
128138
```
139+
140+
### checkNaN
141+
142+
Type: `boolean`\
143+
Default: `true`
144+
145+
Pass `checkNaN: false` to disable check on `NaN`.
146+
147+
#### Fail
148+
149+
```js
150+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
151+
const foo = NaN;
152+
```
153+
154+
```js
155+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
156+
const foo = -NaN;
157+
```
158+
159+
#### Pass
160+
161+
```js
162+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
163+
const foo = Number.NaN;
164+
```
165+
166+
```js
167+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": true}]
168+
const foo = -Number.NaN;
169+
```
170+
171+
```js
172+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
173+
const foo = NaN;
174+
```
175+
176+
```js
177+
// eslint unicorn/prefer-number-properties: ["error", {"checkNaN": false}]
178+
const foo = -NaN;
179+
```

‎rules/prefer-number-properties.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,25 @@ function checkProperty({node, path: [name]}, sourceCode) {
7777
const create = context => {
7878
const {
7979
checkInfinity,
80+
checkNaN,
8081
} = {
8182
checkInfinity: false,
83+
checkNaN: true,
8284
...context.options[0],
8385
};
8486
const {sourceCode} = context;
8587

86-
let objects = Object.keys(globalObjects);
87-
if (!checkInfinity) {
88-
objects = objects.filter(name => name !== 'Infinity');
89-
}
88+
const objects = Object.keys(globalObjects).filter(name => {
89+
if (!checkInfinity && name === 'Infinity') {
90+
return false;
91+
}
92+
93+
if (!checkNaN && name === 'NaN') {
94+
return false;
95+
}
96+
97+
return true;
98+
});
9099

91100
const tracker = new GlobalReferenceTracker({
92101
objects,
@@ -103,6 +112,10 @@ const schema = [
103112
additionalProperties: false,
104113
properties: {
105114
checkInfinity: {
115+
type: 'boolean',
116+
default: false,
117+
},
118+
checkNaN: {
106119
type: 'boolean',
107120
default: true,
108121
},

‎test/prefer-number-properties.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ test({
281281
'class Foo { Infinity(){}}',
282282
'const foo = Infinity;',
283283
'const foo = -Infinity;',
284+
{
285+
code: 'const foo = NaN',
286+
options: [{checkNaN: false}],
287+
},
284288
],
285289
invalid: [
286290
{

0 commit comments

Comments
 (0)
Please sign in to comment.