]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/use-isnan.js
2 * @fileoverview Tests for use-isnan rule.
3 * @author James Allardice, Michael Paulukonis
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/use-isnan"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 const comparisonError
= { messageId
: "comparisonWithNaN", type
: "BinaryExpression" };
23 ruleTester
.run("use-isnan", rule
, {
26 "isNaN(NaN) === true;",
27 "isNaN(123) !== true;",
28 "Number.isNaN(NaN) === true;",
29 "Number.isNaN(123) !== true;",
38 "var x; if (x = NaN) { }",
39 "var x = Number.NaN;",
40 "isNaN(Number.NaN) === true;",
41 "Number.isNaN(Number.NaN) === true;",
42 "foo(Number.NaN + 1);",
43 "foo(1 + Number.NaN);",
44 "foo(Number.NaN - 1)",
45 "foo(1 - Number.NaN)",
46 "foo(Number.NaN * 2)",
47 "foo(2 * Number.NaN)",
48 "foo(Number.NaN / 2)",
49 "foo(2 / Number.NaN)",
50 "var x; if (x = Number.NaN) { }",
53 //------------------------------------------------------------------------------
54 // enforceForSwitchCase
55 //------------------------------------------------------------------------------
58 code
: "switch(NaN) { case foo: break; }",
59 options
: [{ enforceForSwitchCase
: false }]
62 code
: "switch(foo) { case NaN: break; }",
63 options
: [{ enforceForSwitchCase
: false }]
66 code
: "switch(NaN) { case NaN: break; }",
67 options
: [{ enforceForSwitchCase
: false }]
70 code
: "switch(foo) { case bar: break; case NaN: break; default: break; }",
71 options
: [{ enforceForSwitchCase
: false }]
74 code
: "switch(foo) {}",
75 options
: [{ enforceForSwitchCase
: true }]
78 code
: "switch(foo) { case bar: NaN; }",
79 options
: [{ enforceForSwitchCase
: true }]
82 code
: "switch(foo) { default: NaN; }",
83 options
: [{ enforceForSwitchCase
: true }]
86 code
: "switch(Nan) {}",
87 options
: [{ enforceForSwitchCase
: true }]
90 code
: "switch('NaN') { default: break; }",
91 options
: [{ enforceForSwitchCase
: true }]
94 code
: "switch(foo(NaN)) {}",
95 options
: [{ enforceForSwitchCase
: true }]
98 code
: "switch(foo.NaN) {}",
99 options
: [{ enforceForSwitchCase
: true }]
102 code
: "switch(foo) { case Nan: break }",
103 options
: [{ enforceForSwitchCase
: true }]
106 code
: "switch(foo) { case 'NaN': break }",
107 options
: [{ enforceForSwitchCase
: true }]
110 code
: "switch(foo) { case foo(NaN): break }",
111 options
: [{ enforceForSwitchCase
: true }]
114 code
: "switch(foo) { case foo.NaN: break }",
115 options
: [{ enforceForSwitchCase
: true }]
118 code
: "switch(foo) { case bar: break; case 1: break; default: break; }",
119 options
: [{ enforceForSwitchCase
: true }]
122 code
: "switch(Number.NaN) { case foo: break; }",
123 options
: [{ enforceForSwitchCase
: false }]
126 code
: "switch(foo) { case Number.NaN: break; }",
127 options
: [{ enforceForSwitchCase
: false }]
130 code
: "switch(NaN) { case Number.NaN: break; }",
131 options
: [{ enforceForSwitchCase
: false }]
134 code
: "switch(foo) { case bar: break; case Number.NaN: break; default: break; }",
135 options
: [{ enforceForSwitchCase
: false }]
138 code
: "switch(foo) { case bar: Number.NaN; }",
139 options
: [{ enforceForSwitchCase
: true }]
142 code
: "switch(foo) { default: Number.NaN; }",
143 options
: [{ enforceForSwitchCase
: true }]
146 code
: "switch(Number.Nan) {}",
147 options
: [{ enforceForSwitchCase
: true }]
150 code
: "switch('Number.NaN') { default: break; }",
151 options
: [{ enforceForSwitchCase
: true }]
154 code
: "switch(foo(Number.NaN)) {}",
155 options
: [{ enforceForSwitchCase
: true }]
158 code
: "switch(foo.Number.NaN) {}",
159 options
: [{ enforceForSwitchCase
: true }]
162 code
: "switch(foo) { case Number.Nan: break }",
163 options
: [{ enforceForSwitchCase
: true }]
166 code
: "switch(foo) { case 'Number.NaN': break }",
167 options
: [{ enforceForSwitchCase
: true }]
170 code
: "switch(foo) { case foo(Number.NaN): break }",
171 options
: [{ enforceForSwitchCase
: true }]
174 code
: "switch(foo) { case foo.Number.NaN: break }",
175 options
: [{ enforceForSwitchCase
: true }]
178 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
183 "foo.lastIndexOf(NaN)",
184 "foo.indexOf(Number.NaN)",
185 "foo.lastIndexOf(Number.NaN)",
187 code
: "foo.indexOf(NaN)",
191 code
: "foo.lastIndexOf(NaN)",
195 code
: "foo.indexOf(NaN)",
196 options
: [{ enforceForIndexOf
: false }]
199 code
: "foo.lastIndexOf(NaN)",
200 options
: [{ enforceForIndexOf
: false }]
203 code
: "indexOf(NaN)",
204 options
: [{ enforceForIndexOf
: true }]
207 code
: "lastIndexOf(NaN)",
208 options
: [{ enforceForIndexOf
: true }]
211 code
: "new foo.indexOf(NaN)",
212 options
: [{ enforceForIndexOf
: true }]
215 code
: "foo.bar(NaN)",
216 options
: [{ enforceForIndexOf
: true }]
219 code
: "foo.IndexOf(NaN)",
220 options
: [{ enforceForIndexOf
: true }]
223 code
: "foo[indexOf](NaN)",
224 options
: [{ enforceForIndexOf
: true }]
227 code
: "foo[lastIndexOf](NaN)",
228 options
: [{ enforceForIndexOf
: true }]
231 code
: "indexOf.foo(NaN)",
232 options
: [{ enforceForIndexOf
: true }]
235 code
: "foo.indexOf()",
236 options
: [{ enforceForIndexOf
: true }]
239 code
: "foo.lastIndexOf()",
240 options
: [{ enforceForIndexOf
: true }]
243 code
: "foo.indexOf(a)",
244 options
: [{ enforceForIndexOf
: true }]
247 code
: "foo.lastIndexOf(Nan)",
248 options
: [{ enforceForIndexOf
: true }]
251 code
: "foo.indexOf(a, NaN)",
252 options
: [{ enforceForIndexOf
: true }]
255 code
: "foo.lastIndexOf(NaN, b)",
256 options
: [{ enforceForIndexOf
: true }]
259 code
: "foo.indexOf(a, b)",
260 options
: [{ enforceForIndexOf
: true }]
263 code
: "foo.lastIndexOf(NaN, NaN)",
264 options
: [{ enforceForIndexOf
: true }]
267 code
: "foo.indexOf(...NaN)",
268 options
: [{ enforceForIndexOf
: true }],
269 parserOptions
: { ecmaVersion
: 6 }
272 code
: "foo.lastIndexOf(NaN())",
273 options
: [{ enforceForIndexOf
: true }]
276 code
: "foo.indexOf(Number.NaN)",
280 code
: "foo.lastIndexOf(Number.NaN)",
284 code
: "foo.indexOf(Number.NaN)",
285 options
: [{ enforceForIndexOf
: false }]
288 code
: "foo.lastIndexOf(Number.NaN)",
289 options
: [{ enforceForIndexOf
: false }]
292 code
: "indexOf(Number.NaN)",
293 options
: [{ enforceForIndexOf
: true }]
296 code
: "lastIndexOf(Number.NaN)",
297 options
: [{ enforceForIndexOf
: true }]
300 code
: "new foo.indexOf(Number.NaN)",
301 options
: [{ enforceForIndexOf
: true }]
304 code
: "foo.bar(Number.NaN)",
305 options
: [{ enforceForIndexOf
: true }]
308 code
: "foo.IndexOf(Number.NaN)",
309 options
: [{ enforceForIndexOf
: true }]
312 code
: "foo[indexOf](Number.NaN)",
313 options
: [{ enforceForIndexOf
: true }]
316 code
: "foo[lastIndexOf](Number.NaN)",
317 options
: [{ enforceForIndexOf
: true }]
320 code
: "indexOf.foo(Number.NaN)",
321 options
: [{ enforceForIndexOf
: true }]
324 code
: "foo.lastIndexOf(Number.Nan)",
325 options
: [{ enforceForIndexOf
: true }]
328 code
: "foo.indexOf(a, Number.NaN)",
329 options
: [{ enforceForIndexOf
: true }]
332 code
: "foo.lastIndexOf(Number.NaN, b)",
333 options
: [{ enforceForIndexOf
: true }]
336 code
: "foo.lastIndexOf(Number.NaN, NaN)",
337 options
: [{ enforceForIndexOf
: true }]
340 code
: "foo.indexOf(...Number.NaN)",
341 options
: [{ enforceForIndexOf
: true }],
342 parserOptions
: { ecmaVersion
: 6 }
345 code
: "foo.lastIndexOf(Number.NaN())",
346 options
: [{ enforceForIndexOf
: true }]
352 errors
: [comparisonError
]
355 code
: "123 === NaN;",
356 errors
: [comparisonError
]
359 code
: "NaN === \"abc\";",
360 errors
: [comparisonError
]
363 code
: "NaN == \"abc\";",
364 errors
: [comparisonError
]
368 errors
: [comparisonError
]
371 code
: "123 !== NaN;",
372 errors
: [comparisonError
]
375 code
: "NaN !== \"abc\";",
376 errors
: [comparisonError
]
379 code
: "NaN != \"abc\";",
380 errors
: [comparisonError
]
383 code
: "NaN < \"abc\";",
384 errors
: [comparisonError
]
387 code
: "\"abc\" < NaN;",
388 errors
: [comparisonError
]
391 code
: "NaN > \"abc\";",
392 errors
: [comparisonError
]
395 code
: "\"abc\" > NaN;",
396 errors
: [comparisonError
]
399 code
: "NaN <= \"abc\";",
400 errors
: [comparisonError
]
403 code
: "\"abc\" <= NaN;",
404 errors
: [comparisonError
]
407 code
: "NaN >= \"abc\";",
408 errors
: [comparisonError
]
411 code
: "\"abc\" >= NaN;",
412 errors
: [comparisonError
]
415 code
: "123 == Number.NaN;",
416 errors
: [comparisonError
]
419 code
: "123 === Number.NaN;",
420 errors
: [comparisonError
]
423 code
: "Number.NaN === \"abc\";",
424 errors
: [comparisonError
]
427 code
: "Number.NaN == \"abc\";",
428 errors
: [comparisonError
]
431 code
: "123 != Number.NaN;",
432 errors
: [comparisonError
]
435 code
: "123 !== Number.NaN;",
436 errors
: [comparisonError
]
439 code
: "Number.NaN !== \"abc\";",
440 errors
: [comparisonError
]
443 code
: "Number.NaN != \"abc\";",
444 errors
: [comparisonError
]
447 code
: "Number.NaN < \"abc\";",
448 errors
: [comparisonError
]
451 code
: "\"abc\" < Number.NaN;",
452 errors
: [comparisonError
]
455 code
: "Number.NaN > \"abc\";",
456 errors
: [comparisonError
]
459 code
: "\"abc\" > Number.NaN;",
460 errors
: [comparisonError
]
463 code
: "Number.NaN <= \"abc\";",
464 errors
: [comparisonError
]
467 code
: "\"abc\" <= Number.NaN;",
468 errors
: [comparisonError
]
471 code
: "Number.NaN >= \"abc\";",
472 errors
: [comparisonError
]
475 code
: "\"abc\" >= Number.NaN;",
476 errors
: [comparisonError
]
479 code
: "x === Number?.NaN;",
480 parserOptions
: { ecmaVersion
: 2020 },
481 errors
: [comparisonError
]
484 code
: "x === Number['NaN'];",
485 errors
: [comparisonError
]
488 //------------------------------------------------------------------------------
489 // enforceForSwitchCase
490 //------------------------------------------------------------------------------
493 code
: "switch(NaN) { case foo: break; }",
494 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
497 code
: "switch(foo) { case NaN: break; }",
498 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
501 code
: "switch(NaN) { case foo: break; }",
503 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
506 code
: "switch(foo) { case NaN: break; }",
508 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
511 code
: "switch(NaN) {}",
512 options
: [{ enforceForSwitchCase
: true }],
513 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
516 code
: "switch(NaN) { case foo: break; }",
517 options
: [{ enforceForSwitchCase
: true }],
518 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
521 code
: "switch(NaN) { default: break; }",
522 options
: [{ enforceForSwitchCase
: true }],
523 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
526 code
: "switch(NaN) { case foo: break; default: break; }",
527 options
: [{ enforceForSwitchCase
: true }],
528 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
531 code
: "switch(foo) { case NaN: }",
532 options
: [{ enforceForSwitchCase
: true }],
533 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
536 code
: "switch(foo) { case NaN: break; }",
537 options
: [{ enforceForSwitchCase
: true }],
538 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
541 code
: "switch(foo) { case (NaN): break; }",
542 options
: [{ enforceForSwitchCase
: true }],
543 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
546 code
: "switch(foo) { case bar: break; case NaN: break; default: break; }",
547 options
: [{ enforceForSwitchCase
: true }],
548 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 32 }]
551 code
: "switch(foo) { case bar: case NaN: default: break; }",
552 options
: [{ enforceForSwitchCase
: true }],
553 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 25 }]
556 code
: "switch(foo) { case bar: break; case NaN: break; case baz: break; case NaN: break; }",
557 options
: [{ enforceForSwitchCase
: true }],
559 { messageId
: "caseNaN", type
: "SwitchCase", column
: 32 },
560 { messageId
: "caseNaN", type
: "SwitchCase", column
: 66 }
564 code
: "switch(NaN) { case NaN: break; }",
565 options
: [{ enforceForSwitchCase
: true }],
567 { messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 },
568 { messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }
572 code
: "switch(Number.NaN) { case foo: break; }",
573 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
576 code
: "switch(foo) { case Number.NaN: break; }",
577 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
580 code
: "switch(Number.NaN) { case foo: break; }",
582 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
585 code
: "switch(foo) { case Number.NaN: break; }",
587 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
590 code
: "switch(Number.NaN) {}",
591 options
: [{ enforceForSwitchCase
: true }],
592 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
595 code
: "switch(Number.NaN) { case foo: break; }",
596 options
: [{ enforceForSwitchCase
: true }],
597 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
600 code
: "switch(Number.NaN) { default: break; }",
601 options
: [{ enforceForSwitchCase
: true }],
602 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
605 code
: "switch(Number.NaN) { case foo: break; default: break; }",
606 options
: [{ enforceForSwitchCase
: true }],
607 errors
: [{ messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 }]
610 code
: "switch(foo) { case Number.NaN: }",
611 options
: [{ enforceForSwitchCase
: true }],
612 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
615 code
: "switch(foo) { case Number.NaN: break; }",
616 options
: [{ enforceForSwitchCase
: true }],
617 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
620 code
: "switch(foo) { case (Number.NaN): break; }",
621 options
: [{ enforceForSwitchCase
: true }],
622 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 15 }]
625 code
: "switch(foo) { case bar: break; case Number.NaN: break; default: break; }",
626 options
: [{ enforceForSwitchCase
: true }],
627 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 32 }]
630 code
: "switch(foo) { case bar: case Number.NaN: default: break; }",
631 options
: [{ enforceForSwitchCase
: true }],
632 errors
: [{ messageId
: "caseNaN", type
: "SwitchCase", column
: 25 }]
635 code
: "switch(foo) { case bar: break; case NaN: break; case baz: break; case Number.NaN: break; }",
636 options
: [{ enforceForSwitchCase
: true }],
638 { messageId
: "caseNaN", type
: "SwitchCase", column
: 32 },
639 { messageId
: "caseNaN", type
: "SwitchCase", column
: 66 }
643 code
: "switch(Number.NaN) { case Number.NaN: break; }",
644 options
: [{ enforceForSwitchCase
: true }],
646 { messageId
: "switchNaN", type
: "SwitchStatement", column
: 1 },
647 { messageId
: "caseNaN", type
: "SwitchCase", column
: 22 }
651 //------------------------------------------------------------------------------
653 //------------------------------------------------------------------------------
656 code
: "foo.indexOf(NaN)",
657 options
: [{ enforceForIndexOf
: true }],
658 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
661 code
: "foo.lastIndexOf(NaN)",
662 options
: [{ enforceForIndexOf
: true }],
663 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
666 code
: "foo['indexOf'](NaN)",
667 options
: [{ enforceForIndexOf
: true }],
668 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
671 code
: "foo['lastIndexOf'](NaN)",
672 options
: [{ enforceForIndexOf
: true }],
673 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
676 code
: "foo().indexOf(NaN)",
677 options
: [{ enforceForIndexOf
: true }],
678 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
681 code
: "foo.bar.lastIndexOf(NaN)",
682 options
: [{ enforceForIndexOf
: true }],
683 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
686 code
: "foo.indexOf?.(NaN)",
687 options
: [{ enforceForIndexOf
: true }],
688 parserOptions
: { ecmaVersion
: 2020 },
689 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]
692 code
: "foo?.indexOf(NaN)",
693 options
: [{ enforceForIndexOf
: true }],
694 parserOptions
: { ecmaVersion
: 2020 },
695 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]
698 code
: "(foo?.indexOf)(NaN)",
699 options
: [{ enforceForIndexOf
: true }],
700 parserOptions
: { ecmaVersion
: 2020 },
701 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]
704 code
: "foo.indexOf(Number.NaN)",
705 options
: [{ enforceForIndexOf
: true }],
706 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
709 code
: "foo.lastIndexOf(Number.NaN)",
710 options
: [{ enforceForIndexOf
: true }],
711 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
714 code
: "foo['indexOf'](Number.NaN)",
715 options
: [{ enforceForIndexOf
: true }],
716 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
719 code
: "foo['lastIndexOf'](Number.NaN)",
720 options
: [{ enforceForIndexOf
: true }],
721 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
724 code
: "foo().indexOf(Number.NaN)",
725 options
: [{ enforceForIndexOf
: true }],
726 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "indexOf" } }]
729 code
: "foo.bar.lastIndexOf(Number.NaN)",
730 options
: [{ enforceForIndexOf
: true }],
731 errors
: [{ messageId
: "indexOfNaN", type
: "CallExpression", data
: { methodName
: "lastIndexOf" } }]
734 code
: "foo.indexOf?.(Number.NaN)",
735 options
: [{ enforceForIndexOf
: true }],
736 parserOptions
: { ecmaVersion
: 2020 },
737 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]
740 code
: "foo?.indexOf(Number.NaN)",
741 options
: [{ enforceForIndexOf
: true }],
742 parserOptions
: { ecmaVersion
: 2020 },
743 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]
746 code
: "(foo?.indexOf)(Number.NaN)",
747 options
: [{ enforceForIndexOf
: true }],
748 parserOptions
: { ecmaVersion
: 2020 },
749 errors
: [{ messageId
: "indexOfNaN", data
: { methodName
: "indexOf" } }]