]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-shadow.js
dc2cc63c4dea035a593ee84b80ce38e846f01088
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"] }] }
64 code
: "function a(x) { var b = function c() { var x = 'foo'; }; }",
66 messageId
: "noShadow",
78 code
: "var a = (x) => { var b = () => { var x = 'foo'; }; }",
79 parserOptions
: { ecmaVersion
: 6 },
81 messageId
: "noShadow",
93 code
: "function a(x) { var b = function () { var x = 'foo'; }; }",
95 messageId
: "noShadow",
107 code
: "var x = 1; function a(x) { return ++x; }",
109 messageId
: "noShadow",
121 code
: "var a=3; function b() { var a=10; }",
123 messageId
: "noShadow",
133 code
: "var a=3; function b() { var a=10; }; setTimeout(function() { b(); }, 0);",
135 messageId
: "noShadow",
145 code
: "var a=3; function b() { var a=10; var b=0; }; setTimeout(function() { b(); }, 0);",
148 messageId
: "noShadow",
156 messageId
: "noShadow",
167 code
: "var x = 1; { let x = 2; }",
168 parserOptions
: { ecmaVersion
: 6 },
170 messageId
: "noShadow",
180 code
: "let x = 1; { const x = 2; }",
181 parserOptions
: { ecmaVersion
: 6 },
183 messageId
: "noShadow",
193 code
: "{ let a; } function a() {}",
194 parserOptions
: { ecmaVersion
: 6 },
196 messageId
: "noShadow",
206 code
: "{ const a = 0; } function a() {}",
207 parserOptions
: { ecmaVersion
: 6 },
209 messageId
: "noShadow",
219 code
: "function foo() { let a; } function a() {}",
220 parserOptions
: { ecmaVersion
: 6 },
222 messageId
: "noShadow",
232 code
: "function foo() { var a; } function a() {}",
233 parserOptions
: { ecmaVersion
: 6 },
235 messageId
: "noShadow",
245 code
: "function foo(a) { } function a() {}",
246 parserOptions
: { ecmaVersion
: 6 },
248 messageId
: "noShadow",
258 code
: "{ let a; } let a;",
259 options
: [{ hoist
: "all" }],
260 parserOptions
: { ecmaVersion
: 6 },
262 messageId
: "noShadow",
272 code
: "{ let a; } var a;",
273 options
: [{ hoist
: "all" }],
274 parserOptions
: { ecmaVersion
: 6 },
276 messageId
: "noShadow",
286 code
: "{ let a; } function a() {}",
287 options
: [{ hoist
: "all" }],
288 parserOptions
: { ecmaVersion
: 6 },
290 messageId
: "noShadow",
300 code
: "{ const a = 0; } const a = 1;",
301 options
: [{ hoist
: "all" }],
302 parserOptions
: { ecmaVersion
: 6 },
304 messageId
: "noShadow",
314 code
: "{ const a = 0; } var a;",
315 options
: [{ hoist
: "all" }],
316 parserOptions
: { ecmaVersion
: 6 },
318 messageId
: "noShadow",
328 code
: "{ const a = 0; } function a() {}",
329 options
: [{ hoist
: "all" }],
330 parserOptions
: { ecmaVersion
: 6 },
332 messageId
: "noShadow",
342 code
: "function foo() { let a; } let a;",
343 options
: [{ hoist
: "all" }],
344 parserOptions
: { ecmaVersion
: 6 },
346 messageId
: "noShadow",
356 code
: "function foo() { let a; } var a;",
357 options
: [{ hoist
: "all" }],
358 parserOptions
: { ecmaVersion
: 6 },
360 messageId
: "noShadow",
370 code
: "function foo() { let a; } function a() {}",
371 options
: [{ hoist
: "all" }],
372 parserOptions
: { ecmaVersion
: 6 },
374 messageId
: "noShadow",
384 code
: "function foo() { var a; } let a;",
385 options
: [{ hoist
: "all" }],
386 parserOptions
: { ecmaVersion
: 6 },
388 messageId
: "noShadow",
398 code
: "function foo() { var a; } var a;",
399 options
: [{ hoist
: "all" }],
400 parserOptions
: { ecmaVersion
: 6 },
402 messageId
: "noShadow",
412 code
: "function foo() { var a; } function a() {}",
413 options
: [{ hoist
: "all" }],
414 parserOptions
: { ecmaVersion
: 6 },
416 messageId
: "noShadow",
426 code
: "function foo(a) { } let a;",
427 options
: [{ hoist
: "all" }],
428 parserOptions
: { ecmaVersion
: 6 },
430 messageId
: "noShadow",
440 code
: "function foo(a) { } var a;",
441 options
: [{ hoist
: "all" }],
442 parserOptions
: { ecmaVersion
: 6 },
444 messageId
: "noShadow",
454 code
: "function foo(a) { } function a() {}",
455 options
: [{ hoist
: "all" }],
456 parserOptions
: { ecmaVersion
: 6 },
458 messageId
: "noShadow",
468 code
: "(function a() { function a(){} })()",
470 messageId
: "noShadow",
480 code
: "(function a() { class a{} })()",
481 parserOptions
: { ecmaVersion
: 6 },
483 messageId
: "noShadow",
493 code
: "(function a() { (function a(){}); })()",
495 messageId
: "noShadow",
505 code
: "(function a() { (class a{}); })()",
506 parserOptions
: { ecmaVersion
: 6 },
508 messageId
: "noShadow",
518 code
: "(function() { var a = function(a) {}; })()",
520 messageId
: "noShadow",
530 code
: "(function() { var a = function() { function a() {} }; })()",
532 messageId
: "noShadow",
542 code
: "(function() { var a = function() { class a{} }; })()",
543 parserOptions
: { ecmaVersion
: 6 },
545 messageId
: "noShadow",
555 code
: "(function() { var a = function() { (function a() {}); }; })()",
557 messageId
: "noShadow",
567 code
: "(function() { var a = function() { (class a{}); }; })()",
568 parserOptions
: { ecmaVersion
: 6 },
570 messageId
: "noShadow",
580 code
: "(function() { var a = class { constructor() { class a {} } }; })()",
581 parserOptions
: { ecmaVersion
: 6 },
583 messageId
: "noShadow",
593 code
: "class A { constructor() { var A; } }",
594 parserOptions
: { ecmaVersion
: 6 },
596 messageId
: "noShadow",
606 code
: "(function a() { function a(){ function a(){} } })()",
609 messageId
: "noShadow",
620 messageId
: "noShadow",
633 code
: "function foo() { var Object = 0; }",
634 options
: [{ builtinGlobals
: true }],
636 messageId
: "noShadowGlobal",
644 code
: "function foo() { var top = 0; }",
645 options
: [{ builtinGlobals
: true }],
646 env
: { browser
: true },
648 messageId
: "noShadowGlobal",
656 code
: "var Object = 0;",
657 options
: [{ builtinGlobals
: true }],
658 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
660 messageId
: "noShadowGlobal",
668 code
: "var top = 0;",
669 options
: [{ builtinGlobals
: true }],
670 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
671 env
: { browser
: true },
673 messageId
: "noShadowGlobal",
681 code
: "var Object = 0;",
682 options
: [{ builtinGlobals
: true }],
683 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
685 messageId
: "noShadowGlobal",
693 code
: "var top = 0;",
694 options
: [{ builtinGlobals
: true }],
695 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
696 env
: { browser
: true },
698 messageId
: "noShadowGlobal",
706 code
: "function foo(cb) { (function (cb) { cb(42); })(cb); }",
708 messageId
: "noShadow",