]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-var.js
84e14ae19c905f422e55e5bdc7555b2a82ec4215
[pve-eslint.git] / eslint / tests / lib / rules / no-var.js
1 /**
2 * @fileoverview Tests for no-var rule.
3 * @author Jamund Ferguson
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-var"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
20
21 ruleTester.run("no-var", rule, {
22 valid: [
23 "const JOE = 'schmoe';",
24 "let moo = 'car';",
25 {
26 code: "const JOE = 'schmoe';",
27 parserOptions: { ecmaFeatures: { globalReturn: true } }
28 },
29 {
30 code: "let moo = 'car';",
31 parserOptions: { ecmaFeatures: { globalReturn: true } }
32 }
33 ],
34
35 invalid: [
36 {
37 code: "var foo = bar;",
38 output: "let foo = bar;",
39 parserOptions: { ecmaFeatures: { globalReturn: true } },
40 errors: [
41 {
42 messageId: "unexpectedVar",
43 type: "VariableDeclaration"
44 }
45 ]
46 },
47 {
48 code: "var foo = bar, toast = most;",
49 output: "let foo = bar, toast = most;",
50 parserOptions: { ecmaFeatures: { globalReturn: true } },
51 errors: [
52 {
53 messageId: "unexpectedVar",
54 type: "VariableDeclaration"
55 }
56 ]
57 },
58 {
59 code: "var foo = bar; let toast = most;",
60 output: "let foo = bar; let toast = most;",
61 parserOptions: { ecmaFeatures: { globalReturn: true } },
62 errors: [
63 {
64 messageId: "unexpectedVar",
65 type: "VariableDeclaration"
66 }
67 ]
68 },
69 {
70 code: "for (var a of b) { console.log(a); }",
71 output: "for (let a of b) { console.log(a); }",
72 parserOptions: { ecmaFeatures: { globalReturn: true } },
73 errors: [
74 {
75 messageId: "unexpectedVar",
76 type: "VariableDeclaration"
77 }
78 ]
79 },
80 {
81 code: "for (var a in b) { console.log(a); }",
82 output: "for (let a in b) { console.log(a); }",
83 parserOptions: { ecmaFeatures: { globalReturn: true } },
84 errors: [
85 {
86 messageId: "unexpectedVar",
87 type: "VariableDeclaration"
88 }
89 ]
90 },
91 {
92 code: "for (let a of b) { var c = 1; console.log(c); }",
93 output: "for (let a of b) { let c = 1; console.log(c); }",
94 parserOptions: { ecmaFeatures: { globalReturn: true } },
95 errors: [
96 {
97 messageId: "unexpectedVar",
98 type: "VariableDeclaration"
99 }
100 ]
101 },
102 {
103 code: "for (var i = 0; i < list.length; ++i) { foo(i) }",
104 output: "for (let i = 0; i < list.length; ++i) { foo(i) }",
105 parserOptions: { ecmaFeatures: { globalReturn: true } },
106 errors: [
107 { messageId: "unexpectedVar", type: "VariableDeclaration" }
108 ]
109 },
110 {
111 code: "for (var i = 0, i = 0; false;);",
112 output: null,
113 parserOptions: { ecmaFeatures: { globalReturn: true } },
114 errors: [
115 { messageId: "unexpectedVar", type: "VariableDeclaration" }
116 ]
117 },
118 {
119 code: "var i = 0; for (var i = 1; false;); console.log(i);",
120 output: null,
121 parserOptions: { ecmaFeatures: { globalReturn: true } },
122 errors: [
123 { messageId: "unexpectedVar", type: "VariableDeclaration" },
124 { messageId: "unexpectedVar", type: "VariableDeclaration" }
125 ]
126 },
127
128 // Not fix if it's redeclared or it's used from outside of the scope or it's declared on a case chunk.
129 {
130 code: "var a, b, c; var a;",
131 output: null,
132 parserOptions: { ecmaFeatures: { globalReturn: true } },
133 errors: [
134 { messageId: "unexpectedVar" },
135 { messageId: "unexpectedVar" }
136 ]
137 },
138 {
139 code: "var a; if (b) { var a; }",
140 output: null,
141 parserOptions: { ecmaFeatures: { globalReturn: true } },
142 errors: [
143 { messageId: "unexpectedVar" },
144 { messageId: "unexpectedVar" }
145 ]
146 },
147 {
148 code: "if (foo) { var a, b, c; } a;",
149 output: null,
150 parserOptions: { ecmaFeatures: { globalReturn: true } },
151 errors: [
152 { messageId: "unexpectedVar" }
153 ]
154 },
155 {
156 code: "for (var i = 0; i < 10; ++i) {} i;",
157 output: null,
158 parserOptions: { ecmaFeatures: { globalReturn: true } },
159 errors: [
160 { messageId: "unexpectedVar" }
161 ]
162 },
163 {
164 code: "for (var a in obj) {} a;",
165 output: null,
166 parserOptions: { ecmaFeatures: { globalReturn: true } },
167 errors: [
168 { messageId: "unexpectedVar" }
169 ]
170 },
171 {
172 code: "for (var a of list) {} a;",
173 output: null,
174 parserOptions: { ecmaFeatures: { globalReturn: true } },
175 errors: [
176 { messageId: "unexpectedVar" }
177 ]
178 },
179 {
180 code: "switch (a) { case 0: var b = 1 }",
181 output: null,
182 parserOptions: { ecmaFeatures: { globalReturn: true } },
183 errors: [
184 { messageId: "unexpectedVar" }
185 ]
186 },
187
188 // Don't fix if the variable is in a loop and the behavior might change.
189 {
190 code: "for (var a of b) { arr.push(() => a); }",
191 output: null,
192 parserOptions: { ecmaFeatures: { globalReturn: true } },
193 errors: [
194 { messageId: "unexpectedVar" }
195 ]
196 },
197 {
198 code: "for (let a of b) { var c; console.log(c); c = 'hello'; }",
199 output: null,
200 parserOptions: { ecmaFeatures: { globalReturn: true } },
201 errors: [
202 { messageId: "unexpectedVar" }
203 ]
204 },
205
206 // https://github.com/eslint/eslint/issues/7950
207 {
208 code: "var a = a",
209 output: null,
210 parserOptions: { ecmaFeatures: { globalReturn: true } },
211 errors: [
212 { messageId: "unexpectedVar" }
213 ]
214 },
215 {
216 code: "var {a = a} = {}",
217 output: null,
218 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
219 errors: [
220 { messageId: "unexpectedVar" }
221 ]
222 },
223 {
224 code: "var {a = b, b} = {}",
225 output: null,
226 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
227 errors: [
228 { messageId: "unexpectedVar" }
229 ]
230 },
231 {
232 code: "var {a, b = a} = {}",
233 output: "let {a, b = a} = {}",
234 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
235 errors: [
236 { messageId: "unexpectedVar" }
237 ]
238 },
239 {
240 code: "var a = b, b = 1",
241 output: null,
242 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
243 errors: [
244 { messageId: "unexpectedVar" }
245 ]
246 },
247 {
248 code: "var a = b; var b = 1",
249 output: "let a = b; var b = 1",
250 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
251 errors: [
252 { messageId: "unexpectedVar" },
253 { messageId: "unexpectedVar" }
254 ]
255 },
256
257 /*
258 * This case is not in TDZ, but it's very hard to distinguish the reference is in TDZ or not.
259 * So this rule does not fix it for safe.
260 */
261 {
262 code: "function foo() { a } var a = 1; foo()",
263 output: null,
264 parserOptions: { ecmaVersion: 2015, ecmaFeatures: { globalReturn: true } },
265 errors: [
266 { messageId: "unexpectedVar" }
267 ]
268 },
269
270 // https://github.com/eslint/eslint/issues/7961
271 {
272 code: "if (foo) var bar = 1;",
273 output: null,
274 parserOptions: { ecmaFeatures: { globalReturn: true } },
275 errors: [
276 { messageId: "unexpectedVar", type: "VariableDeclaration" }
277 ]
278 },
279
280 // https://github.com/eslint/eslint/issues/9520
281 {
282 code: "var foo = 1",
283 output: null,
284 errors: [{ messageId: "unexpectedVar" }]
285 },
286 {
287 code: "{ var foo = 1 }",
288 output: null,
289 errors: [{ messageId: "unexpectedVar" }]
290 },
291 {
292 code: "if (true) { var foo = 1 }",
293 output: null,
294 errors: [{ messageId: "unexpectedVar" }]
295 },
296 {
297 code: "var foo = 1",
298 output: "let foo = 1",
299 parserOptions: { ecmaVersion: 6, sourceType: "module" },
300 errors: [{ messageId: "unexpectedVar" }]
301 },
302
303 // https://github.com/eslint/eslint/issues/11594
304 {
305 code: "declare var foo = 2;",
306 output: "declare let foo = 2;",
307 parser: require.resolve("../../fixtures/parsers/typescript-parsers/declare-var"),
308 parserOptions: { ecmaVersion: 6, sourceType: "module" },
309 errors: [{ messageId: "unexpectedVar" }]
310 },
311
312 // https://github.com/eslint/eslint/issues/11830
313 {
314 code: "function foo() { var let; }",
315 output: null,
316 errors: [{ messageId: "unexpectedVar" }]
317 },
318 {
319 code: "function foo() { var { let } = {}; }",
320 output: null,
321 errors: [{ messageId: "unexpectedVar" }]
322 }
323 ]
324 });