]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/block-scoped-var.js
596c3ba1cf97791473c38f6e531c78ae8f3e08c5
[pve-eslint.git] / eslint / tests / lib / rules / block-scoped-var.js
1 /**
2 * @fileoverview Tests for block-scoped-var rule
3 * @author Matt DuVall <http://www.mattduvall.com>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/block-scoped-var"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("block-scoped-var", rule, {
22 valid: [
23
24 // See issue https://github.com/eslint/eslint/issues/2242
25 { code: "function f() { } f(); var exports = { f: f };", parserOptions: { ecmaVersion: 6 } },
26 { code: "var f = () => {}; f(); var exports = { f: f };", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
27 "!function f(){ f; }",
28 "function f() { } f(); var exports = { f: f };",
29 "function f() { var a, b; { a = true; } b = a; }",
30 "var a; function f() { var b = a; }",
31 "function f(a) { }",
32 "!function(a) { };",
33 "!function f(a) { };",
34 "function f(a) { var b = a; }",
35 "!function f(a) { var b = a; };",
36 "function f() { var g = f; }",
37 "function f() { } function g() { var f = g; }",
38 "function f() { var hasOwnProperty; { hasOwnProperty; } }",
39 "function f(){ a; b; var a, b; }",
40 "function f(){ g(); function g(){} }",
41 "if (true) { var a = 1; a; }",
42 "var a; if (true) { a; }",
43 "for (var i = 0; i < 10; i++) { i; }",
44 "var i; for(i; i; i) { i; }",
45 { code: "function myFunc(foo) { \"use strict\"; var { bar } = foo; bar.hello();}", parserOptions: { ecmaVersion: 6 } },
46 { code: "function myFunc(foo) { \"use strict\"; var [ bar ] = foo; bar.hello();}", parserOptions: { ecmaVersion: 6 } },
47 { code: "function myFunc(...foo) { return foo;}", parserOptions: { ecmaVersion: 6 } },
48 { code: "var f = () => { var g = f; }", parserOptions: { ecmaVersion: 6 } },
49 { code: "class Foo {}\nexport default Foo;", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
50 { code: "new Date", globals: { Date: false } },
51 { code: "new Date", globals: {} },
52 { code: "var eslint = require('eslint');", globals: { require: false } },
53 { code: "var fun = function({x}) {return x;};", parserOptions: { ecmaVersion: 6 } },
54 { code: "var fun = function([,x]) {return x;};", parserOptions: { ecmaVersion: 6 } },
55 "function f(a) { return a.b; }",
56 "var a = { \"foo\": 3 };",
57 "var a = { foo: 3 };",
58 "var a = { foo: 3, bar: 5 };",
59 "var a = { set foo(a){}, get bar(){} };",
60 "function f(a) { return arguments[0]; }",
61 "function f() { }; var a = f;",
62 "var a = f; function f() { };",
63 "function f(){ for(var i; i; i) i; }",
64 "function f(){ for(var a=0, b=1; a; b) a, b; }",
65 "function f(){ for(var a in {}) a; }",
66 "function f(){ switch(2) { case 1: var b = 2; b; break; default: b; break;} }",
67 "a:;",
68 "foo: while (true) { bar: for (var i = 0; i < 13; ++i) {if (i === 7) break foo; } }",
69 "foo: while (true) { bar: for (var i = 0; i < 13; ++i) {if (i === 7) continue foo; } }",
70 { code: "const React = require(\"react/addons\");const cx = React.addons.classSet;", parserOptions: { ecmaVersion: 6, sourceType: "module" }, globals: { require: false } },
71 { code: "var v = 1; function x() { return v; };", parserOptions: { parserOptions: { ecmaVersion: 6 } } },
72 { code: "import * as y from \"./other.js\"; y();", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
73 { code: "import y from \"./other.js\"; y();", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
74 { code: "import {x as y} from \"./other.js\"; y();", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
75 { code: "var x; export {x};", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
76 { code: "var x; export {x as v};", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
77 { code: "export {x} from \"./other.js\";", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
78 { code: "export {x as v} from \"./other.js\";", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
79 { code: "class Test { myFunction() { return true; }}", parserOptions: { ecmaVersion: 6 } },
80 { code: "class Test { get flag() { return true; }}", parserOptions: { ecmaVersion: 6 } },
81 { code: "var Test = class { myFunction() { return true; }}", parserOptions: { ecmaVersion: 6 } },
82 { code: "var doStuff; let {x: y} = {x: 1}; doStuff(y);", parserOptions: { ecmaVersion: 6 } },
83 { code: "function foo({x: y}) { return y; }", parserOptions: { ecmaVersion: 6 } },
84
85 // those are the same as `no-undef`.
86 "!function f(){}; f",
87 "var f = function foo() { }; foo(); var exports = { f: foo };",
88 { code: "var f = () => { x; }", parserOptions: { ecmaVersion: 6 } },
89 "function f(){ x; }",
90 "var eslint = require('eslint');",
91 "function f(a) { return a[b]; }",
92 "function f() { return b.a; }",
93 "var a = { foo: bar };",
94 "var a = { foo: foo };",
95 "var a = { bar: 7, foo: bar };",
96 "var a = arguments;",
97 "function x(){}; var a = arguments;",
98 "function z(b){}; var a = b;",
99 "function z(){var b;}; var a = b;",
100 "function f(){ try{}catch(e){} e }",
101 "a:b;",
102
103 // https://github.com/eslint/eslint/issues/2253
104 { code: "/*global React*/ let {PropTypes, addons: {PureRenderMixin}} = React; let Test = React.createClass({mixins: [PureRenderMixin]});", parserOptions: { ecmaVersion: 6 } },
105 { code: "/*global prevState*/ const { virtualSize: prevVirtualSize = 0 } = prevState;", parserOptions: { ecmaVersion: 6 } },
106 { code: "const { dummy: { data, isLoading }, auth: { isLoggedIn } } = this.props;", parserOptions: { ecmaVersion: 6 } },
107
108 // https://github.com/eslint/eslint/issues/2747
109 "function a(n) { return n > 0 ? b(n - 1) : \"a\"; } function b(n) { return n > 0 ? a(n - 1) : \"b\"; }",
110
111 // https://github.com/eslint/eslint/issues/2967
112 "(function () { foo(); })(); function foo() {}",
113 { code: "(function () { foo(); })(); function foo() {}", parserOptions: { ecmaVersion: 6, sourceType: "module" } }
114 ],
115 invalid: [
116 { code: "function f(){ x; { var x; } }", errors: [{ messageId: "outOfScope", data: { name: "x" }, type: "Identifier" }] },
117 { code: "function f(){ { var x; } x; }", errors: [{ messageId: "outOfScope", data: { name: "x" }, type: "Identifier" }] },
118 { code: "function f() { var a; { var b = 0; } a = b; }", errors: [{ messageId: "outOfScope", data: { name: "b" }, type: "Identifier" }] },
119 { code: "function f() { try { var a = 0; } catch (e) { var b = a; } }", errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }] },
120 {
121 code: "function a() { for(var b in {}) { var c = b; } c; }",
122 errors: [{ messageId: "outOfScope", data: { name: "c" }, type: "Identifier" }]
123 },
124 {
125 code: "function a() { for(var b of {}) { var c = b; } c; }",
126 parserOptions: { ecmaVersion: 6 },
127 errors: [{ messageId: "outOfScope", data: { name: "c" }, type: "Identifier" }]
128 },
129 {
130 code: "function f(){ switch(2) { case 1: var b = 2; b; break; default: b; break;} b; }",
131 errors: [{ messageId: "outOfScope", data: { name: "b" }, type: "Identifier" }]
132 },
133 {
134 code: "for (var a = 0;;) {} a;",
135 errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }]
136 },
137 {
138 code: "for (var a in []) {} a;",
139 errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }]
140 },
141 {
142 code: "for (var a of []) {} a;",
143 parserOptions: { ecmaVersion: 6 },
144 errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }]
145 },
146 {
147 code: "{ var a = 0; } a;",
148 parserOptions: { ecmaVersion: 6, sourceType: "module" },
149 errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }]
150 },
151 {
152 code: "if (true) { var a; } a;",
153 errors: [{ messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }]
154 },
155 {
156 code: "if (true) { var a = 1; } else { var a = 2; }",
157 errors: [
158 { messageId: "outOfScope", data: { name: "a" }, type: "Identifier" },
159 { messageId: "outOfScope", data: { name: "a" }, type: "Identifier" }
160 ]
161 },
162 {
163 code: "for (var i = 0;;) {} for(var i = 0;;) {}",
164 errors: [
165 { messageId: "outOfScope", data: { name: "i" }, type: "Identifier" },
166 { messageId: "outOfScope", data: { name: "i" }, type: "Identifier" }
167 ]
168 }
169 ]
170 });