]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-shadow.js
dc2cc63c4dea035a593ee84b80ce38e846f01088
[pve-eslint.git] / eslint / tests / lib / rules / no-shadow.js
1 /**
2 * @fileoverview Tests for no-shadow rule.
3 * @author Ilya Volodin
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-shadow"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-shadow", rule, {
22 valid: [
23 "var a=3; function b(x) { a++; return x + a; }; setTimeout(function() { b(a); }, 0);",
24 "(function() { var doSomething = function doSomething() {}; doSomething() }())",
25 "var arguments;\nfunction bar() { }",
26 { code: "var a=3; var b = (x) => { a++; return x + a; }; setTimeout(() => { b(a); }, 0);", parserOptions: { ecmaVersion: 6 } },
27 { code: "class A {}", parserOptions: { ecmaVersion: 6 } },
28 { code: "class A { constructor() { var a; } }", parserOptions: { ecmaVersion: 6 } },
29 { code: "(function() { var A = class A {}; })()", parserOptions: { ecmaVersion: 6 } },
30 { code: "{ var a; } var a;", parserOptions: { ecmaVersion: 6 } }, // this case reports `no-redeclare`, not shadowing.
31 { code: "{ let a; } let a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
32 { code: "{ let a; } var a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
33 { code: "{ let a; } function a() {}", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
34 { code: "{ const a = 0; } const a = 1;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
35 { code: "{ const a = 0; } var a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
36 { code: "{ const a = 0; } function a() {}", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
37 { code: "function foo() { let a; } let a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
38 { code: "function foo() { let a; } var a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
39 { code: "function foo() { let a; } function a() {}", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
40 { code: "function foo() { var a; } let a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
41 { code: "function foo() { var a; } var a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
42 { code: "function foo() { var a; } function a() {}", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
43 { code: "function foo(a) { } let a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
44 { code: "function foo(a) { } var a;", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
45 { code: "function foo(a) { } function a() {}", options: [{ hoist: "never" }], parserOptions: { ecmaVersion: 6 } },
46 { code: "{ let a; } let a;", parserOptions: { ecmaVersion: 6 } },
47 { code: "{ let a; } var a;", parserOptions: { ecmaVersion: 6 } },
48 { code: "{ const a = 0; } const a = 1;", parserOptions: { ecmaVersion: 6 } },
49 { code: "{ const a = 0; } var a;", parserOptions: { ecmaVersion: 6 } },
50 { code: "function foo() { let a; } let a;", parserOptions: { ecmaVersion: 6 } },
51 { code: "function foo() { let a; } var a;", parserOptions: { ecmaVersion: 6 } },
52 { code: "function foo() { var a; } let a;", parserOptions: { ecmaVersion: 6 } },
53 { code: "function foo() { var a; } var a;", parserOptions: { ecmaVersion: 6 } },
54 { code: "function foo(a) { } let a;", parserOptions: { ecmaVersion: 6 } },
55 { code: "function foo(a) { } var a;", parserOptions: { ecmaVersion: 6 } },
56 "function foo() { var Object = 0; }",
57 { code: "function foo() { var top = 0; }", env: { browser: true } },
58 { code: "var Object = 0;", options: [{ builtinGlobals: true }] },
59 { code: "var top = 0;", options: [{ builtinGlobals: true }], env: { browser: true } },
60 { code: "function foo(cb) { (function (cb) { cb(42); })(cb); }", options: [{ allow: ["cb"] }] }
61 ],
62 invalid: [
63 {
64 code: "function a(x) { var b = function c() { var x = 'foo'; }; }",
65 errors: [{
66 messageId: "noShadow",
67 data: {
68 name: "x",
69 shadowedLine: 1,
70 shadowedColumn: 12
71 },
72 type: "Identifier",
73 line: 1,
74 column: 44
75 }]
76 },
77 {
78 code: "var a = (x) => { var b = () => { var x = 'foo'; }; }",
79 parserOptions: { ecmaVersion: 6 },
80 errors: [{
81 messageId: "noShadow",
82 data: {
83 name: "x",
84 shadowedLine: 1,
85 shadowedColumn: 10
86 },
87 type: "Identifier",
88 line: 1,
89 column: 38
90 }]
91 },
92 {
93 code: "function a(x) { var b = function () { var x = 'foo'; }; }",
94 errors: [{
95 messageId: "noShadow",
96 data: {
97 name: "x",
98 shadowedLine: 1,
99 shadowedColumn: 12
100 },
101 type: "Identifier",
102 line: 1,
103 column: 43
104 }]
105 },
106 {
107 code: "var x = 1; function a(x) { return ++x; }",
108 errors: [{
109 messageId: "noShadow",
110 data: {
111 name: "x",
112 shadowedLine: 1,
113 shadowedColumn: 5
114 },
115 type: "Identifier",
116 line: 1,
117 column: 23
118 }]
119 },
120 {
121 code: "var a=3; function b() { var a=10; }",
122 errors: [{
123 messageId: "noShadow",
124 data: {
125 name: "a",
126 shadowedLine: 1,
127 shadowedColumn: 5
128 },
129 type: "Identifier"
130 }]
131 },
132 {
133 code: "var a=3; function b() { var a=10; }; setTimeout(function() { b(); }, 0);",
134 errors: [{
135 messageId: "noShadow",
136 data: {
137 name: "a",
138 shadowedLine: 1,
139 shadowedColumn: 5
140 },
141 type: "Identifier"
142 }]
143 },
144 {
145 code: "var a=3; function b() { var a=10; var b=0; }; setTimeout(function() { b(); }, 0);",
146 errors: [
147 {
148 messageId: "noShadow",
149 data: {
150 name: "a",
151 shadowedLine: 1,
152 shadowedColumn: 5
153 },
154 type: "Identifier"
155 }, {
156 messageId: "noShadow",
157 data: {
158 name: "b",
159 shadowedLine: 1,
160 shadowedColumn: 19
161 },
162 type: "Identifier"
163 }
164 ]
165 },
166 {
167 code: "var x = 1; { let x = 2; }",
168 parserOptions: { ecmaVersion: 6 },
169 errors: [{
170 messageId: "noShadow",
171 data: {
172 name: "x",
173 shadowedLine: 1,
174 shadowedColumn: 5
175 },
176 type: "Identifier"
177 }]
178 },
179 {
180 code: "let x = 1; { const x = 2; }",
181 parserOptions: { ecmaVersion: 6 },
182 errors: [{
183 messageId: "noShadow",
184 data: {
185 name: "x",
186 shadowedLine: 1,
187 shadowedColumn: 5
188 },
189 type: "Identifier"
190 }]
191 },
192 {
193 code: "{ let a; } function a() {}",
194 parserOptions: { ecmaVersion: 6 },
195 errors: [{
196 messageId: "noShadow",
197 data: {
198 name: "a",
199 shadowedLine: 1,
200 shadowedColumn: 21
201 },
202 type: "Identifier"
203 }]
204 },
205 {
206 code: "{ const a = 0; } function a() {}",
207 parserOptions: { ecmaVersion: 6 },
208 errors: [{
209 messageId: "noShadow",
210 data: {
211 name: "a",
212 shadowedLine: 1,
213 shadowedColumn: 27
214 },
215 type: "Identifier"
216 }]
217 },
218 {
219 code: "function foo() { let a; } function a() {}",
220 parserOptions: { ecmaVersion: 6 },
221 errors: [{
222 messageId: "noShadow",
223 data: {
224 name: "a",
225 shadowedLine: 1,
226 shadowedColumn: 36
227 },
228 type: "Identifier"
229 }]
230 },
231 {
232 code: "function foo() { var a; } function a() {}",
233 parserOptions: { ecmaVersion: 6 },
234 errors: [{
235 messageId: "noShadow",
236 data: {
237 name: "a",
238 shadowedLine: 1,
239 shadowedColumn: 36
240 },
241 type: "Identifier"
242 }]
243 },
244 {
245 code: "function foo(a) { } function a() {}",
246 parserOptions: { ecmaVersion: 6 },
247 errors: [{
248 messageId: "noShadow",
249 data: {
250 name: "a",
251 shadowedLine: 1,
252 shadowedColumn: 30
253 },
254 type: "Identifier"
255 }]
256 },
257 {
258 code: "{ let a; } let a;",
259 options: [{ hoist: "all" }],
260 parserOptions: { ecmaVersion: 6 },
261 errors: [{
262 messageId: "noShadow",
263 data: {
264 name: "a",
265 shadowedLine: 1,
266 shadowedColumn: 16
267 },
268 type: "Identifier"
269 }]
270 },
271 {
272 code: "{ let a; } var a;",
273 options: [{ hoist: "all" }],
274 parserOptions: { ecmaVersion: 6 },
275 errors: [{
276 messageId: "noShadow",
277 data: {
278 name: "a",
279 shadowedLine: 1,
280 shadowedColumn: 16
281 },
282 type: "Identifier"
283 }]
284 },
285 {
286 code: "{ let a; } function a() {}",
287 options: [{ hoist: "all" }],
288 parserOptions: { ecmaVersion: 6 },
289 errors: [{
290 messageId: "noShadow",
291 data: {
292 name: "a",
293 shadowedLine: 1,
294 shadowedColumn: 21
295 },
296 type: "Identifier"
297 }]
298 },
299 {
300 code: "{ const a = 0; } const a = 1;",
301 options: [{ hoist: "all" }],
302 parserOptions: { ecmaVersion: 6 },
303 errors: [{
304 messageId: "noShadow",
305 data: {
306 name: "a",
307 shadowedLine: 1,
308 shadowedColumn: 24
309 },
310 type: "Identifier"
311 }]
312 },
313 {
314 code: "{ const a = 0; } var a;",
315 options: [{ hoist: "all" }],
316 parserOptions: { ecmaVersion: 6 },
317 errors: [{
318 messageId: "noShadow",
319 data: {
320 name: "a",
321 shadowedLine: 1,
322 shadowedColumn: 22
323 },
324 type: "Identifier"
325 }]
326 },
327 {
328 code: "{ const a = 0; } function a() {}",
329 options: [{ hoist: "all" }],
330 parserOptions: { ecmaVersion: 6 },
331 errors: [{
332 messageId: "noShadow",
333 data: {
334 name: "a",
335 shadowedLine: 1,
336 shadowedColumn: 27
337 },
338 type: "Identifier"
339 }]
340 },
341 {
342 code: "function foo() { let a; } let a;",
343 options: [{ hoist: "all" }],
344 parserOptions: { ecmaVersion: 6 },
345 errors: [{
346 messageId: "noShadow",
347 data: {
348 name: "a",
349 shadowedLine: 1,
350 shadowedColumn: 31
351 },
352 type: "Identifier"
353 }]
354 },
355 {
356 code: "function foo() { let a; } var a;",
357 options: [{ hoist: "all" }],
358 parserOptions: { ecmaVersion: 6 },
359 errors: [{
360 messageId: "noShadow",
361 data: {
362 name: "a",
363 shadowedLine: 1,
364 shadowedColumn: 31
365 },
366 type: "Identifier"
367 }]
368 },
369 {
370 code: "function foo() { let a; } function a() {}",
371 options: [{ hoist: "all" }],
372 parserOptions: { ecmaVersion: 6 },
373 errors: [{
374 messageId: "noShadow",
375 data: {
376 name: "a",
377 shadowedLine: 1,
378 shadowedColumn: 36
379 },
380 type: "Identifier"
381 }]
382 },
383 {
384 code: "function foo() { var a; } let a;",
385 options: [{ hoist: "all" }],
386 parserOptions: { ecmaVersion: 6 },
387 errors: [{
388 messageId: "noShadow",
389 data: {
390 name: "a",
391 shadowedLine: 1,
392 shadowedColumn: 31
393 },
394 type: "Identifier"
395 }]
396 },
397 {
398 code: "function foo() { var a; } var a;",
399 options: [{ hoist: "all" }],
400 parserOptions: { ecmaVersion: 6 },
401 errors: [{
402 messageId: "noShadow",
403 data: {
404 name: "a",
405 shadowedLine: 1,
406 shadowedColumn: 31
407 },
408 type: "Identifier"
409 }]
410 },
411 {
412 code: "function foo() { var a; } function a() {}",
413 options: [{ hoist: "all" }],
414 parserOptions: { ecmaVersion: 6 },
415 errors: [{
416 messageId: "noShadow",
417 data: {
418 name: "a",
419 shadowedLine: 1,
420 shadowedColumn: 36
421 },
422 type: "Identifier"
423 }]
424 },
425 {
426 code: "function foo(a) { } let a;",
427 options: [{ hoist: "all" }],
428 parserOptions: { ecmaVersion: 6 },
429 errors: [{
430 messageId: "noShadow",
431 data: {
432 name: "a",
433 shadowedLine: 1,
434 shadowedColumn: 25
435 },
436 type: "Identifier"
437 }]
438 },
439 {
440 code: "function foo(a) { } var a;",
441 options: [{ hoist: "all" }],
442 parserOptions: { ecmaVersion: 6 },
443 errors: [{
444 messageId: "noShadow",
445 data: {
446 name: "a",
447 shadowedLine: 1,
448 shadowedColumn: 25
449 },
450 type: "Identifier"
451 }]
452 },
453 {
454 code: "function foo(a) { } function a() {}",
455 options: [{ hoist: "all" }],
456 parserOptions: { ecmaVersion: 6 },
457 errors: [{
458 messageId: "noShadow",
459 data: {
460 name: "a",
461 shadowedLine: 1,
462 shadowedColumn: 30
463 },
464 type: "Identifier"
465 }]
466 },
467 {
468 code: "(function a() { function a(){} })()",
469 errors: [{
470 messageId: "noShadow",
471 data: {
472 name: "a",
473 shadowedLine: 1,
474 shadowedColumn: 11
475 },
476 type: "Identifier"
477 }]
478 },
479 {
480 code: "(function a() { class a{} })()",
481 parserOptions: { ecmaVersion: 6 },
482 errors: [{
483 messageId: "noShadow",
484 data: {
485 name: "a",
486 shadowedLine: 1,
487 shadowedColumn: 11
488 },
489 type: "Identifier"
490 }]
491 },
492 {
493 code: "(function a() { (function a(){}); })()",
494 errors: [{
495 messageId: "noShadow",
496 data: {
497 name: "a",
498 shadowedLine: 1,
499 shadowedColumn: 11
500 },
501 type: "Identifier"
502 }]
503 },
504 {
505 code: "(function a() { (class a{}); })()",
506 parserOptions: { ecmaVersion: 6 },
507 errors: [{
508 messageId: "noShadow",
509 data: {
510 name: "a",
511 shadowedLine: 1,
512 shadowedColumn: 11
513 },
514 type: "Identifier"
515 }]
516 },
517 {
518 code: "(function() { var a = function(a) {}; })()",
519 errors: [{
520 messageId: "noShadow",
521 data: {
522 name: "a",
523 shadowedLine: 1,
524 shadowedColumn: 19
525 },
526 type: "Identifier"
527 }]
528 },
529 {
530 code: "(function() { var a = function() { function a() {} }; })()",
531 errors: [{
532 messageId: "noShadow",
533 data: {
534 name: "a",
535 shadowedLine: 1,
536 shadowedColumn: 19
537 },
538 type: "Identifier"
539 }]
540 },
541 {
542 code: "(function() { var a = function() { class a{} }; })()",
543 parserOptions: { ecmaVersion: 6 },
544 errors: [{
545 messageId: "noShadow",
546 data: {
547 name: "a",
548 shadowedLine: 1,
549 shadowedColumn: 19
550 },
551 type: "Identifier"
552 }]
553 },
554 {
555 code: "(function() { var a = function() { (function a() {}); }; })()",
556 errors: [{
557 messageId: "noShadow",
558 data: {
559 name: "a",
560 shadowedLine: 1,
561 shadowedColumn: 19
562 },
563 type: "Identifier"
564 }]
565 },
566 {
567 code: "(function() { var a = function() { (class a{}); }; })()",
568 parserOptions: { ecmaVersion: 6 },
569 errors: [{
570 messageId: "noShadow",
571 data: {
572 name: "a",
573 shadowedLine: 1,
574 shadowedColumn: 19
575 },
576 type: "Identifier"
577 }]
578 },
579 {
580 code: "(function() { var a = class { constructor() { class a {} } }; })()",
581 parserOptions: { ecmaVersion: 6 },
582 errors: [{
583 messageId: "noShadow",
584 data: {
585 name: "a",
586 shadowedLine: 1,
587 shadowedColumn: 19
588 },
589 type: "Identifier"
590 }]
591 },
592 {
593 code: "class A { constructor() { var A; } }",
594 parserOptions: { ecmaVersion: 6 },
595 errors: [{
596 messageId: "noShadow",
597 data: {
598 name: "A",
599 shadowedLine: 1,
600 shadowedColumn: 7
601 },
602 type: "Identifier"
603 }]
604 },
605 {
606 code: "(function a() { function a(){ function a(){} } })()",
607 errors: [
608 {
609 messageId: "noShadow",
610 data: {
611 name: "a",
612 shadowedLine: 1,
613 shadowedColumn: 11
614 },
615 type: "Identifier",
616 line: 1,
617 column: 26
618 },
619 {
620 messageId: "noShadow",
621 data: {
622 name: "a",
623 shadowedLine: 1,
624 shadowedColumn: 26
625 },
626 type: "Identifier",
627 line: 1,
628 column: 40
629 }
630 ]
631 },
632 {
633 code: "function foo() { var Object = 0; }",
634 options: [{ builtinGlobals: true }],
635 errors: [{
636 messageId: "noShadowGlobal",
637 data: {
638 name: "Object"
639 },
640 type: "Identifier"
641 }]
642 },
643 {
644 code: "function foo() { var top = 0; }",
645 options: [{ builtinGlobals: true }],
646 env: { browser: true },
647 errors: [{
648 messageId: "noShadowGlobal",
649 data: {
650 name: "top"
651 },
652 type: "Identifier"
653 }]
654 },
655 {
656 code: "var Object = 0;",
657 options: [{ builtinGlobals: true }],
658 parserOptions: { ecmaVersion: 6, sourceType: "module" },
659 errors: [{
660 messageId: "noShadowGlobal",
661 data: {
662 name: "Object"
663 },
664 type: "Identifier"
665 }]
666 },
667 {
668 code: "var top = 0;",
669 options: [{ builtinGlobals: true }],
670 parserOptions: { ecmaVersion: 6, sourceType: "module" },
671 env: { browser: true },
672 errors: [{
673 messageId: "noShadowGlobal",
674 data: {
675 name: "top"
676 },
677 type: "Identifier"
678 }]
679 },
680 {
681 code: "var Object = 0;",
682 options: [{ builtinGlobals: true }],
683 parserOptions: { ecmaFeatures: { globalReturn: true } },
684 errors: [{
685 messageId: "noShadowGlobal",
686 data: {
687 name: "Object"
688 },
689 type: "Identifier"
690 }]
691 },
692 {
693 code: "var top = 0;",
694 options: [{ builtinGlobals: true }],
695 parserOptions: { ecmaFeatures: { globalReturn: true } },
696 env: { browser: true },
697 errors: [{
698 messageId: "noShadowGlobal",
699 data: {
700 name: "top"
701 },
702 type: "Identifier"
703 }]
704 },
705 {
706 code: "function foo(cb) { (function (cb) { cb(42); })(cb); }",
707 errors: [{
708 messageId: "noShadow",
709 data: {
710 name: "cb",
711 shadowedLine: 1,
712 shadowedColumn: 14
713 },
714 type: "Identifier",
715 line: 1,
716 column: 31
717 }]
718 }
719 ]
720 });