]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-shadow.js
2 * @fileoverview Tests for no-shadow rule.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/no-shadow"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("no-shadow", rule
, {
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 { code
: "class C { foo; foo() { let foo; } }", parserOptions
: { ecmaVersion
: 2022 } },
62 { code
: "class C { static { var x; } static { var x; } }", parserOptions
: { ecmaVersion
: 2022 } },
63 { code
: "class C { static { let x; } static { let x; } }", parserOptions
: { ecmaVersion
: 2022 } },
64 { code
: "class C { static { var x; { var x; /* redeclaration */ } } }", parserOptions
: { ecmaVersion
: 2022 } },
65 { code
: "class C { static { { var x; } { var x; /* redeclaration */ } } }", parserOptions
: { ecmaVersion
: 2022 } },
66 { code
: "class C { static { { let x; } { let x; } } }", parserOptions
: { ecmaVersion
: 2022 } },
67 { code
: "const a = [].find(a => a)", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
68 { code
: "const a = [].find(function(a) { return a; })", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
69 { code
: "const [a = [].find(a => true)] = dummy", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
70 { code
: "const { a = [].find(a => true) } = dummy", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
71 { code
: "function func(a = [].find(a => true)) {}", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
72 { code
: "for (const a in [].find(a => true)) {}", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
73 { code
: "for (const a of [].find(a => true)) {}", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
74 { code
: "const a = [].map(a => true).filter(a => a === 'b')", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
75 { code
: "const a = [].map(a => true).filter(a => a === 'b').find(a => a === 'c')", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
76 { code
: "const { a } = (({ a }) => ({ a }))();", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
77 { code
: "const person = people.find(item => {const person = item.name; return person === 'foo'})", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
78 { code
: "var y = bar || foo(y => y);", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
79 { code
: "var y = bar && foo(y => y);", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
80 { code
: "var z = bar(foo(z => z));", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
81 { code
: "var z = boo(bar(foo(z => z)));", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
82 { code
: "var match = function (person) { return person.name === 'foo'; };\nconst person = [].find(match);", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
83 { code
: "const a = foo(x || (a => {}))", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
84 { code
: "const { a = 1 } = foo(a => {})", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
85 { code
: "const person = {...people.find((person) => person.firstName.startsWith('s'))}", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 2021 } },
86 { code
: "const person = { firstName: people.filter((person) => person.firstName.startsWith('s')).map((person) => person.firstName)[0]}", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 2021 } },
87 { code
: "() => { const y = foo(y => y); }", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
88 { code
: "const x = (x => x)()", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
89 { code
: "var y = bar || (y => y)();", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
90 { code
: "var y = bar && (y => y)();", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
91 { code
: "var x = (x => x)((y => y)());", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
92 { code
: "const { a = 1 } = (a => {})()", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
93 { code
: "() => { const y = (y => y)(); }", options
: [{ ignoreOnInitialization
: true }], parserOptions
: { ecmaVersion
: 6 } },
94 { code
: "const [x = y => y] = [].map(y => y)", parserOptions
: { ecmaVersion
: 6 } }
98 code
: "function a(x) { var b = function c() { var x = 'foo'; }; }",
100 messageId
: "noShadow",
112 code
: "var a = (x) => { var b = () => { var x = 'foo'; }; }",
113 parserOptions
: { ecmaVersion
: 6 },
115 messageId
: "noShadow",
127 code
: "function a(x) { var b = function () { var x = 'foo'; }; }",
129 messageId
: "noShadow",
141 code
: "var x = 1; function a(x) { return ++x; }",
143 messageId
: "noShadow",
155 code
: "var a=3; function b() { var a=10; }",
157 messageId
: "noShadow",
167 code
: "var a=3; function b() { var a=10; }; setTimeout(function() { b(); }, 0);",
169 messageId
: "noShadow",
179 code
: "var a=3; function b() { var a=10; var b=0; }; setTimeout(function() { b(); }, 0);",
182 messageId
: "noShadow",
190 messageId
: "noShadow",
201 code
: "var x = 1; { let x = 2; }",
202 parserOptions
: { ecmaVersion
: 6 },
204 messageId
: "noShadow",
214 code
: "let x = 1; { const x = 2; }",
215 parserOptions
: { ecmaVersion
: 6 },
217 messageId
: "noShadow",
227 code
: "{ let a; } function a() {}",
228 parserOptions
: { ecmaVersion
: 6 },
230 messageId
: "noShadow",
240 code
: "{ const a = 0; } function a() {}",
241 parserOptions
: { ecmaVersion
: 6 },
243 messageId
: "noShadow",
253 code
: "function foo() { let a; } function a() {}",
254 parserOptions
: { ecmaVersion
: 6 },
256 messageId
: "noShadow",
266 code
: "function foo() { var a; } function a() {}",
267 parserOptions
: { ecmaVersion
: 6 },
269 messageId
: "noShadow",
279 code
: "function foo(a) { } function a() {}",
280 parserOptions
: { ecmaVersion
: 6 },
282 messageId
: "noShadow",
292 code
: "{ let a; } let a;",
293 options
: [{ hoist
: "all" }],
294 parserOptions
: { ecmaVersion
: 6 },
296 messageId
: "noShadow",
306 code
: "{ let a; } var a;",
307 options
: [{ hoist
: "all" }],
308 parserOptions
: { ecmaVersion
: 6 },
310 messageId
: "noShadow",
320 code
: "{ let a; } function a() {}",
321 options
: [{ hoist
: "all" }],
322 parserOptions
: { ecmaVersion
: 6 },
324 messageId
: "noShadow",
334 code
: "{ const a = 0; } const a = 1;",
335 options
: [{ hoist
: "all" }],
336 parserOptions
: { ecmaVersion
: 6 },
338 messageId
: "noShadow",
348 code
: "{ const a = 0; } var a;",
349 options
: [{ hoist
: "all" }],
350 parserOptions
: { ecmaVersion
: 6 },
352 messageId
: "noShadow",
362 code
: "{ const a = 0; } function a() {}",
363 options
: [{ hoist
: "all" }],
364 parserOptions
: { ecmaVersion
: 6 },
366 messageId
: "noShadow",
376 code
: "function foo() { let a; } let a;",
377 options
: [{ hoist
: "all" }],
378 parserOptions
: { ecmaVersion
: 6 },
380 messageId
: "noShadow",
390 code
: "function foo() { let a; } var a;",
391 options
: [{ hoist
: "all" }],
392 parserOptions
: { ecmaVersion
: 6 },
394 messageId
: "noShadow",
404 code
: "function foo() { let a; } function a() {}",
405 options
: [{ hoist
: "all" }],
406 parserOptions
: { ecmaVersion
: 6 },
408 messageId
: "noShadow",
418 code
: "function foo() { var a; } let a;",
419 options
: [{ hoist
: "all" }],
420 parserOptions
: { ecmaVersion
: 6 },
422 messageId
: "noShadow",
432 code
: "function foo() { var a; } var a;",
433 options
: [{ hoist
: "all" }],
434 parserOptions
: { ecmaVersion
: 6 },
436 messageId
: "noShadow",
446 code
: "function foo() { var a; } function a() {}",
447 options
: [{ hoist
: "all" }],
448 parserOptions
: { ecmaVersion
: 6 },
450 messageId
: "noShadow",
460 code
: "function foo(a) { } let a;",
461 options
: [{ hoist
: "all" }],
462 parserOptions
: { ecmaVersion
: 6 },
464 messageId
: "noShadow",
474 code
: "function foo(a) { } var a;",
475 options
: [{ hoist
: "all" }],
476 parserOptions
: { ecmaVersion
: 6 },
478 messageId
: "noShadow",
488 code
: "function foo(a) { } function a() {}",
489 options
: [{ hoist
: "all" }],
490 parserOptions
: { ecmaVersion
: 6 },
492 messageId
: "noShadow",
502 code
: "(function a() { function a(){} })()",
504 messageId
: "noShadow",
514 code
: "(function a() { class a{} })()",
515 parserOptions
: { ecmaVersion
: 6 },
517 messageId
: "noShadow",
527 code
: "(function a() { (function a(){}); })()",
529 messageId
: "noShadow",
539 code
: "(function a() { (class a{}); })()",
540 parserOptions
: { ecmaVersion
: 6 },
542 messageId
: "noShadow",
552 code
: "(function() { var a = function(a) {}; })()",
554 messageId
: "noShadow",
564 code
: "(function() { var a = function() { function a() {} }; })()",
566 messageId
: "noShadow",
576 code
: "(function() { var a = function() { class a{} }; })()",
577 parserOptions
: { ecmaVersion
: 6 },
579 messageId
: "noShadow",
589 code
: "(function() { var a = function() { (function a() {}); }; })()",
591 messageId
: "noShadow",
601 code
: "(function() { var a = function() { (class a{}); }; })()",
602 parserOptions
: { ecmaVersion
: 6 },
604 messageId
: "noShadow",
614 code
: "(function() { var a = class { constructor() { class a {} } }; })()",
615 parserOptions
: { ecmaVersion
: 6 },
617 messageId
: "noShadow",
627 code
: "class A { constructor() { var A; } }",
628 parserOptions
: { ecmaVersion
: 6 },
630 messageId
: "noShadow",
640 code
: "(function a() { function a(){ function a(){} } })()",
643 messageId
: "noShadow",
654 messageId
: "noShadow",
667 code
: "function foo() { var Object = 0; }",
668 options
: [{ builtinGlobals
: true }],
670 messageId
: "noShadowGlobal",
678 code
: "function foo() { var top = 0; }",
679 options
: [{ builtinGlobals
: true }],
680 env
: { browser
: true },
682 messageId
: "noShadowGlobal",
690 code
: "var Object = 0;",
691 options
: [{ builtinGlobals
: true }],
692 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
694 messageId
: "noShadowGlobal",
702 code
: "var top = 0;",
703 options
: [{ builtinGlobals
: true }],
704 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
705 env
: { browser
: true },
707 messageId
: "noShadowGlobal",
715 code
: "var Object = 0;",
716 options
: [{ builtinGlobals
: true }],
717 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
719 messageId
: "noShadowGlobal",
727 code
: "var top = 0;",
728 options
: [{ builtinGlobals
: true }],
729 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
730 env
: { browser
: true },
732 messageId
: "noShadowGlobal",
740 code
: "function foo(cb) { (function (cb) { cb(42); })(cb); }",
742 messageId
: "noShadow",
754 code
: "class C { static { let a; { let a; } } }",
755 parserOptions
: { ecmaVersion
: 2022 },
757 messageId
: "noShadow",
769 code
: "class C { static { var C; } }",
770 parserOptions
: { ecmaVersion
: 2022 },
772 messageId
: "noShadow",
784 code
: "class C { static { let C; } }",
785 parserOptions
: { ecmaVersion
: 2022 },
787 messageId
: "noShadow",
799 code
: "var a; class C { static { var a; } }",
800 parserOptions
: { ecmaVersion
: 2022 },
802 messageId
: "noShadow",
814 code
: "class C { static { var a; } } var a;",
815 options
: [{ hoist
: "all" }],
816 parserOptions
: { ecmaVersion
: 2022 },
818 messageId
: "noShadow",
830 code
: "class C { static { let a; } } let a;",
831 options
: [{ hoist
: "all" }],
832 parserOptions
: { ecmaVersion
: 2022 },
834 messageId
: "noShadow",
846 code
: "class C { static { var a; } } let a;",
847 options
: [{ hoist
: "all" }],
848 parserOptions
: { ecmaVersion
: 2022 },
850 messageId
: "noShadow",
862 code
: "class C { static { var a; class D { static { var a; } } } }",
863 parserOptions
: { ecmaVersion
: 2022 },
865 messageId
: "noShadow",
877 code
: "class C { static { let a; class D { static { let a; } } } }",
878 parserOptions
: { ecmaVersion
: 2022 },
880 messageId
: "noShadow",
892 code
: "let x = foo((x,y) => {});\nlet y;",
893 options
: [{ hoist
: "all" }],
894 parserOptions
: { ecmaVersion
: 6 },
897 messageId
: "noShadow",
906 messageId
: "noShadow",
917 code
: "const a = fn(()=>{ class C { fn () { const a = 42; return a } } return new C() })",
918 options
: [{ ignoreOnInitialization
: true }],
919 parserOptions
: { ecmaVersion
: 6 },
921 messageId
: "noShadow",
933 code
: "function a() {}\nfoo(a => {});",
934 options
: [{ ignoreOnInitialization
: true }],
935 parserOptions
: { ecmaVersion
: 6 },
937 messageId
: "noShadow",
949 code
: "const a = fn(()=>{ function C() { this.fn=function() { const a = 42; return a } } return new C() });",
950 options
: [{ ignoreOnInitialization
: true }],
951 parserOptions
: { ecmaVersion
: 6 },
953 messageId
: "noShadow",
965 code
: "const x = foo(() => { const bar = () => { return x => {}; }; return bar; });",
966 options
: [{ ignoreOnInitialization
: true }],
967 parserOptions
: { ecmaVersion
: 6 },
969 messageId
: "noShadow",
981 code
: "const x = foo(() => { return { bar(x) {} }; });",
982 options
: [{ ignoreOnInitialization
: true }],
983 parserOptions
: { ecmaVersion
: 6 },
985 messageId
: "noShadow",
997 code
: "const x = () => { foo(x => x); }",
998 options
: [{ ignoreOnInitialization
: true }],
999 parserOptions
: { ecmaVersion
: 6 },
1001 messageId
: "noShadow",
1013 code
: "const foo = () => { let x; bar(x => x); }",
1014 options
: [{ ignoreOnInitialization
: true }],
1015 parserOptions
: { ecmaVersion
: 6 },
1017 messageId
: "noShadow",
1029 code
: "foo(() => { const x = x => x; });",
1030 options
: [{ ignoreOnInitialization
: true }],
1031 parserOptions
: { ecmaVersion
: 6 },
1033 messageId
: "noShadow",
1045 code
: "const foo = (x) => { bar(x => {}) }",
1046 options
: [{ ignoreOnInitialization
: true }],
1047 parserOptions
: { ecmaVersion
: 6 },
1049 messageId
: "noShadow",
1061 code
: "let x = ((x,y) => {})();\nlet y;",
1062 options
: [{ hoist
: "all" }],
1063 parserOptions
: { ecmaVersion
: 6 },
1066 messageId
: "noShadow",
1075 messageId
: "noShadow",
1086 code
: "const a = (()=>{ class C { fn () { const a = 42; return a } } return new C() })()",
1087 options
: [{ ignoreOnInitialization
: true }],
1088 parserOptions
: { ecmaVersion
: 6 },
1090 messageId
: "noShadow",
1102 code
: "const x = () => { (x => x)(); }",
1103 options
: [{ ignoreOnInitialization
: true }],
1104 parserOptions
: { ecmaVersion
: 6 },
1106 messageId
: "noShadow",