Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 5754bb5

Browse files
committed
docs(useIsNan): promote Number.isNaN instead of isNaN
1 parent 8d5b2ea commit 5754bb5

File tree

3 files changed

+52
-42
lines changed

3 files changed

+52
-42
lines changed

crates/rome_js_analyze/src/analyzers/nursery/use_is_nan.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ declare_rule! {
1414
/// - `NaN` === `NaN` or `NaN` == `NaN` evaluate to false
1515
/// - `NaN` !== `NaN` or `NaN` != `NaN` evaluate to true
1616
///
17-
/// Therefore, use `Number.isNaN()` or global `isNaN()` functions to test whether a value is `NaN`.
17+
/// Therefore, use `Number.isNaN()` or global `isNaN()` functions to test whether a value is `NaN`.
18+
///
19+
/// Note that `Number.isNaN()` and `isNaN()` [have not the same behavior](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#description).
20+
/// When the argument to `isNaN()` is not a number, the value is first coerced to a number.
21+
/// `Number.isNaN()` does not perform this coercion.
22+
/// Therefore, it is a more reliable way to test whether a value is `NaN`.
1823
///
1924
/// Source: [use-isnan](https://eslint.org/docs/latest/rules/use-isnan).
2025
///
@@ -41,7 +46,7 @@ declare_rule! {
4146
/// ### Valid
4247
///
4348
/// ```js
44-
/// if (isNaN(123) !== true) {}
49+
/// if (Number.isNaN(123) !== true) {}
4550
///
4651
/// foo(Number.NaN / 2)
4752
///
@@ -73,7 +78,7 @@ pub struct RuleState {
7378
impl Message {
7479
fn as_str(&self) -> &str {
7580
match self {
76-
Self::BinaryExpression => "Use the isNaN function to compare with NaN.",
81+
Self::BinaryExpression => "Use the Number.isNaN function to compare with NaN.",
7782
Self::CaseClause => "'case NaN' can never match. Use Number.isNaN before the switch.",
7883
Self::SwitchCase => "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch."
7984
}

crates/rome_js_analyze/tests/specs/nursery/useIsNan/invalid.js.snap

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ switch(Number.NaN) { case Number.NaN: break; }
7070
```
7171
invalid.js:1:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
7272
73-
! Use the isNaN function to compare with NaN.
73+
! Use the Number.isNaN function to compare with NaN.
7474
7575
> 1 │ 123 == NaN;
7676
│ ^^^^^^^^^^
@@ -83,7 +83,7 @@ invalid.js:1:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
8383
```
8484
invalid.js:2:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8585
86-
! Use the isNaN function to compare with NaN.
86+
! Use the Number.isNaN function to compare with NaN.
8787
8888
1 │ 123 == NaN;
8989
> 2 │ 123 === NaN;
@@ -97,7 +97,7 @@ invalid.js:2:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
9797
```
9898
invalid.js:3:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
9999
100-
! Use the isNaN function to compare with NaN.
100+
! Use the Number.isNaN function to compare with NaN.
101101
102102
1 │ 123 == NaN;
103103
2 │ 123 === NaN;
@@ -112,7 +112,7 @@ invalid.js:3:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
112112
```
113113
invalid.js:4:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
114114
115-
! Use the isNaN function to compare with NaN.
115+
! Use the Number.isNaN function to compare with NaN.
116116
117117
2 │ 123 === NaN;
118118
3 │ NaN === "abc";
@@ -127,7 +127,7 @@ invalid.js:4:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
127127
```
128128
invalid.js:5:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
129129
130-
! Use the isNaN function to compare with NaN.
130+
! Use the Number.isNaN function to compare with NaN.
131131
132132
3 │ NaN === "abc";
133133
4 │ NaN == "abc";
@@ -142,7 +142,7 @@ invalid.js:5:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
142142
```
143143
invalid.js:6:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
144144
145-
! Use the isNaN function to compare with NaN.
145+
! Use the Number.isNaN function to compare with NaN.
146146
147147
4 │ NaN == "abc";
148148
5 │ 123 != NaN;
@@ -157,7 +157,7 @@ invalid.js:6:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
157157
```
158158
invalid.js:7:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
159159
160-
! Use the isNaN function to compare with NaN.
160+
! Use the Number.isNaN function to compare with NaN.
161161
162162
5 │ 123 != NaN;
163163
6 │ 123 !== NaN;
@@ -172,7 +172,7 @@ invalid.js:7:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
172172
```
173173
invalid.js:8:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
174174
175-
! Use the isNaN function to compare with NaN.
175+
! Use the Number.isNaN function to compare with NaN.
176176
177177
6 │ 123 !== NaN;
178178
7 │ NaN !== "abc";
@@ -187,7 +187,7 @@ invalid.js:8:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
187187
```
188188
invalid.js:9:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
189189
190-
! Use the isNaN function to compare with NaN.
190+
! Use the Number.isNaN function to compare with NaN.
191191
192192
7 │ NaN !== "abc";
193193
8 │ NaN != "abc";
@@ -202,7 +202,7 @@ invalid.js:9:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
202202
```
203203
invalid.js:10:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
204204
205-
! Use the isNaN function to compare with NaN.
205+
! Use the Number.isNaN function to compare with NaN.
206206
207207
8 │ NaN != "abc";
208208
9 │ NaN < "abc";
@@ -217,7 +217,7 @@ invalid.js:10:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
217217
```
218218
invalid.js:11:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
219219
220-
! Use the isNaN function to compare with NaN.
220+
! Use the Number.isNaN function to compare with NaN.
221221
222222
9 │ NaN < "abc";
223223
10 │ "abc" < NaN;
@@ -232,7 +232,7 @@ invalid.js:11:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
232232
```
233233
invalid.js:12:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
234234
235-
! Use the isNaN function to compare with NaN.
235+
! Use the Number.isNaN function to compare with NaN.
236236
237237
10 │ "abc" < NaN;
238238
11 │ NaN > "abc";
@@ -247,7 +247,7 @@ invalid.js:12:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
247247
```
248248
invalid.js:13:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
249249
250-
! Use the isNaN function to compare with NaN.
250+
! Use the Number.isNaN function to compare with NaN.
251251
252252
11 │ NaN > "abc";
253253
12 │ "abc" > NaN;
@@ -262,7 +262,7 @@ invalid.js:13:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
262262
```
263263
invalid.js:14:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
264264
265-
! Use the isNaN function to compare with NaN.
265+
! Use the Number.isNaN function to compare with NaN.
266266
267267
12 │ "abc" > NaN;
268268
13 │ NaN <= "abc";
@@ -277,7 +277,7 @@ invalid.js:14:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
277277
```
278278
invalid.js:15:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
279279
280-
! Use the isNaN function to compare with NaN.
280+
! Use the Number.isNaN function to compare with NaN.
281281
282282
13 │ NaN <= "abc";
283283
14 │ "abc" <= NaN;
@@ -292,7 +292,7 @@ invalid.js:15:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
292292
```
293293
invalid.js:16:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
294294
295-
! Use the isNaN function to compare with NaN.
295+
! Use the Number.isNaN function to compare with NaN.
296296
297297
14 │ "abc" <= NaN;
298298
15 │ NaN >= "abc";
@@ -307,7 +307,7 @@ invalid.js:16:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
307307
```
308308
invalid.js:17:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
309309
310-
! Use the isNaN function to compare with NaN.
310+
! Use the Number.isNaN function to compare with NaN.
311311
312312
15 │ NaN >= "abc";
313313
16 │ "abc" >= NaN;
@@ -322,7 +322,7 @@ invalid.js:17:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
322322
```
323323
invalid.js:18:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
324324
325-
! Use the isNaN function to compare with NaN.
325+
! Use the Number.isNaN function to compare with NaN.
326326
327327
16 │ "abc" >= NaN;
328328
17 │ 123 == Number.NaN;
@@ -337,7 +337,7 @@ invalid.js:18:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
337337
```
338338
invalid.js:19:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
339339
340-
! Use the isNaN function to compare with NaN.
340+
! Use the Number.isNaN function to compare with NaN.
341341
342342
17 │ 123 == Number.NaN;
343343
18 │ 123 === Number.NaN;
@@ -352,7 +352,7 @@ invalid.js:19:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
352352
```
353353
invalid.js:20:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
354354
355-
! Use the isNaN function to compare with NaN.
355+
! Use the Number.isNaN function to compare with NaN.
356356
357357
18 │ 123 === Number.NaN;
358358
19 │ Number.NaN === "abc";
@@ -367,7 +367,7 @@ invalid.js:20:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
367367
```
368368
invalid.js:21:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
369369
370-
! Use the isNaN function to compare with NaN.
370+
! Use the Number.isNaN function to compare with NaN.
371371
372372
19 │ Number.NaN === "abc";
373373
20 │ Number.NaN == "abc";
@@ -382,7 +382,7 @@ invalid.js:21:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
382382
```
383383
invalid.js:22:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
384384
385-
! Use the isNaN function to compare with NaN.
385+
! Use the Number.isNaN function to compare with NaN.
386386
387387
20 │ Number.NaN == "abc";
388388
21 │ 123 != Number.NaN;
@@ -397,7 +397,7 @@ invalid.js:22:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
397397
```
398398
invalid.js:23:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
399399
400-
! Use the isNaN function to compare with NaN.
400+
! Use the Number.isNaN function to compare with NaN.
401401
402402
21 │ 123 != Number.NaN;
403403
22 │ 123 !== Number.NaN;
@@ -412,7 +412,7 @@ invalid.js:23:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
412412
```
413413
invalid.js:24:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
414414
415-
! Use the isNaN function to compare with NaN.
415+
! Use the Number.isNaN function to compare with NaN.
416416
417417
22 │ 123 !== Number.NaN;
418418
23 │ Number.NaN !== "abc";
@@ -427,7 +427,7 @@ invalid.js:24:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
427427
```
428428
invalid.js:25:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
429429
430-
! Use the isNaN function to compare with NaN.
430+
! Use the Number.isNaN function to compare with NaN.
431431
432432
23 │ Number.NaN !== "abc";
433433
24 │ Number.NaN != "abc";
@@ -442,7 +442,7 @@ invalid.js:25:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
442442
```
443443
invalid.js:26:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
444444
445-
! Use the isNaN function to compare with NaN.
445+
! Use the Number.isNaN function to compare with NaN.
446446
447447
24 │ Number.NaN != "abc";
448448
25 │ Number.NaN < "abc";
@@ -457,7 +457,7 @@ invalid.js:26:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
457457
```
458458
invalid.js:27:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
459459
460-
! Use the isNaN function to compare with NaN.
460+
! Use the Number.isNaN function to compare with NaN.
461461
462462
25 │ Number.NaN < "abc";
463463
26 │ "abc" < Number.NaN;
@@ -472,7 +472,7 @@ invalid.js:27:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
472472
```
473473
invalid.js:28:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
474474
475-
! Use the isNaN function to compare with NaN.
475+
! Use the Number.isNaN function to compare with NaN.
476476
477477
26 │ "abc" < Number.NaN;
478478
27 │ Number.NaN > "abc";
@@ -487,7 +487,7 @@ invalid.js:28:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
487487
```
488488
invalid.js:29:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
489489
490-
! Use the isNaN function to compare with NaN.
490+
! Use the Number.isNaN function to compare with NaN.
491491
492492
27 │ Number.NaN > "abc";
493493
28 │ "abc" > Number.NaN;
@@ -502,7 +502,7 @@ invalid.js:29:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
502502
```
503503
invalid.js:30:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
504504
505-
! Use the isNaN function to compare with NaN.
505+
! Use the Number.isNaN function to compare with NaN.
506506
507507
28 │ "abc" > Number.NaN;
508508
29 │ Number.NaN <= "abc";
@@ -517,7 +517,7 @@ invalid.js:30:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
517517
```
518518
invalid.js:31:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
519519
520-
! Use the isNaN function to compare with NaN.
520+
! Use the Number.isNaN function to compare with NaN.
521521
522522
29 │ Number.NaN <= "abc";
523523
30 │ "abc" <= Number.NaN;
@@ -532,7 +532,7 @@ invalid.js:31:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
532532
```
533533
invalid.js:32:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
534534
535-
! Use the isNaN function to compare with NaN.
535+
! Use the Number.isNaN function to compare with NaN.
536536
537537
30 │ "abc" <= Number.NaN;
538538
31 │ Number.NaN >= "abc";
@@ -547,7 +547,7 @@ invalid.js:32:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
547547
```
548548
invalid.js:33:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
549549
550-
! Use the isNaN function to compare with NaN.
550+
! Use the Number.isNaN function to compare with NaN.
551551
552552
31 │ Number.NaN >= "abc";
553553
32 │ "abc" >= Number.NaN;
@@ -562,7 +562,7 @@ invalid.js:33:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━
562562
```
563563
invalid.js:34:1 lint/nursery/useIsNan ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
564564
565-
! Use the isNaN function to compare with NaN.
565+
! Use the Number.isNaN function to compare with NaN.
566566
567567
32 │ "abc" >= Number.NaN;
568568
33 │ x === Number?.NaN;

0 commit comments

Comments
 (0)