]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/vars-on-top.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / vars-on-top.js
1 /**
2 * @fileoverview Tests for vars-on-top rule.
3 * @author Danny Fritz
4 * @author Gyandeep Singh
5 */
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/vars-on-top"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20 const error = { messageId: "top", type: "VariableDeclaration" };
21
22 ruleTester.run("vars-on-top", rule, {
23
24 valid: [
25 [
26 "var first = 0;",
27 "function foo() {",
28 " first = 2;",
29 "}"
30 ].join("\n"),
31 [
32 "function foo() {",
33 "}"
34 ].join("\n"),
35 [
36 "function foo() {",
37 " var first;",
38 " if (true) {",
39 " first = true;",
40 " } else {",
41 " first = 1;",
42 " }",
43 "}"
44 ].join("\n"),
45 [
46 "function foo() {",
47 " var first;",
48 " var second = 1;",
49 " var third;",
50 " var fourth = 1, fifth, sixth = third;",
51 " var seventh;",
52 " if (true) {",
53 " third = true;",
54 " }",
55 " first = second;",
56 "}"
57 ].join("\n"),
58 [
59 "function foo() {",
60 " var i;",
61 " for (i = 0; i < 10; i++) {",
62 " alert(i);",
63 " }",
64 "}"
65 ].join("\n"),
66 [
67 "function foo() {",
68 " var outer;",
69 " function inner() {",
70 " var inner = 1;",
71 " var outer = inner;",
72 " }",
73 " outer = 1;",
74 "}"
75 ].join("\n"),
76 [
77 "function foo() {",
78 " var first;",
79 " //Hello",
80 " var second = 1;",
81 " first = second;",
82 "}"
83 ].join("\n"),
84 [
85 "function foo() {",
86 " var first;",
87 " /*",
88 " Hello Clarice",
89 " */",
90 " var second = 1;",
91 " first = second;",
92 "}"
93 ].join("\n"),
94 [
95 "function foo() {",
96 " var first;",
97 " var second = 1;",
98 " function bar(){",
99 " var first;",
100 " first = 5;",
101 " }",
102 " first = second;",
103 "}"
104 ].join("\n"),
105 [
106 "function foo() {",
107 " var first;",
108 " var second = 1;",
109 " function bar(){",
110 " var third;",
111 " third = 5;",
112 " }",
113 " first = second;",
114 "}"
115 ].join("\n"),
116 [
117 "function foo() {",
118 " var first;",
119 " var bar = function(){",
120 " var third;",
121 " third = 5;",
122 " }",
123 " first = 5;",
124 "}"
125 ].join("\n"),
126 [
127 "function foo() {",
128 " var first;",
129 " first.onclick(function(){",
130 " var third;",
131 " third = 5;",
132 " });",
133 " first = 5;",
134 "}"
135 ].join("\n"),
136 {
137 code: [
138 "function foo() {",
139 " var i = 0;",
140 " for (let j = 0; j < 10; j++) {",
141 " alert(j);",
142 " }",
143 " i = i + 1;",
144 "}"
145 ].join("\n"),
146 parserOptions: {
147 ecmaVersion: 6
148 }
149 },
150 "'use strict'; var x; f();",
151 "'use strict'; 'directive'; var x; var y; f();",
152 "function f() { 'use strict'; var x; f(); }",
153 "function f() { 'use strict'; 'directive'; var x; var y; f(); }",
154 { code: "import React from 'react'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
155 { code: "'use strict'; import React from 'react'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
156 { code: "import React from 'react'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
157 { code: "import * as foo from 'mod.js'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
158 { code: "import { square, diag } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
159 { code: "import { default as foo } from 'lib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
160 { code: "import 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
161 { code: "import theDefault, { named1, named2 } from 'src/mylib'; 'use strict'; var y; function f() { 'use strict'; var x; var y; f(); }", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
162 {
163 code: [
164 "export var x;",
165 "var y;",
166 "var z;"
167 ].join("\n"),
168 parserOptions: {
169 ecmaVersion: 6,
170 sourceType: "module"
171 }
172 },
173 {
174 code: [
175 "var x;",
176 "export var y;",
177 "var z;"
178 ].join("\n"),
179 parserOptions: {
180 ecmaVersion: 6,
181 sourceType: "module"
182 }
183 },
184 {
185 code: [
186 "var x;",
187 "var y;",
188 "export var z;"
189 ].join("\n"),
190 parserOptions: {
191 ecmaVersion: 6,
192 sourceType: "module"
193 }
194 },
195 {
196 code: [
197 "class C {",
198 " static {",
199 " var x;",
200 " }",
201 "}"
202 ].join("\n"),
203 parserOptions: {
204 ecmaVersion: 2022
205 }
206 },
207 {
208 code: [
209 "class C {",
210 " static {",
211 " var x;",
212 " foo();",
213 " }",
214 "}"
215 ].join("\n"),
216 parserOptions: {
217 ecmaVersion: 2022
218 }
219 },
220 {
221 code: [
222 "class C {",
223 " static {",
224 " var x;",
225 " var y;",
226 " }",
227 "}"
228 ].join("\n"),
229 parserOptions: {
230 ecmaVersion: 2022
231 }
232 },
233 {
234 code: [
235 "class C {",
236 " static {",
237 " var x;",
238 " var y;",
239 " foo();",
240 " }",
241 "}"
242 ].join("\n"),
243 parserOptions: {
244 ecmaVersion: 2022
245 }
246 },
247 {
248 code: [
249 "class C {",
250 " static {",
251 " let x;",
252 " var y;",
253 " }",
254 "}"
255 ].join("\n"),
256 parserOptions: {
257 ecmaVersion: 2022
258 }
259 },
260 {
261 code: [
262 "class C {",
263 " static {",
264 " foo();",
265 " let x;",
266 " }",
267 "}"
268 ].join("\n"),
269 parserOptions: {
270 ecmaVersion: 2022
271 }
272 }
273 ],
274
275 invalid: [
276 {
277 code: [
278 "var first = 0;",
279 "function foo() {",
280 " first = 2;",
281 " second = 2;",
282 "}",
283 "var second = 0;"
284 ].join("\n"),
285 errors: [error]
286 },
287 {
288 code: [
289 "function foo() {",
290 " var first;",
291 " first = 1;",
292 " first = 2;",
293 " first = 3;",
294 " first = 4;",
295 " var second = 1;",
296 " second = 2;",
297 " first = second;",
298 "}"
299 ].join("\n"),
300 errors: [error]
301 },
302 {
303 code: [
304 "function foo() {",
305 " var first;",
306 " if (true) {",
307 " var second = true;",
308 " }",
309 " first = second;",
310 "}"
311 ].join("\n"),
312 errors: [error]
313 },
314 {
315 code: [
316 "function foo() {",
317 " for (var i = 0; i < 10; i++) {",
318 " alert(i);",
319 " }",
320 "}"
321 ].join("\n"),
322 errors: [error]
323 },
324 {
325 code: [
326 "function foo() {",
327 " var first = 10;",
328 " var i;",
329 " for (i = 0; i < first; i ++) {",
330 " var second = i;",
331 " }",
332 "}"
333 ].join("\n"),
334 errors: [error]
335 },
336 {
337 code: [
338 "function foo() {",
339 " var first = 10;",
340 " var i;",
341 " switch (first) {",
342 " case 10:",
343 " var hello = 1;",
344 " break;",
345 " }",
346 "}"
347 ].join("\n"),
348 errors: [error]
349 },
350 {
351 code: [
352 "function foo() {",
353 " var first = 10;",
354 " var i;",
355 " try {",
356 " var hello = 1;",
357 " } catch (e) {",
358 " alert('error');",
359 " }",
360 "}"
361 ].join("\n"),
362 errors: [error]
363 },
364 {
365 code: [
366 "function foo() {",
367 " var first = 10;",
368 " var i;",
369 " try {",
370 " asdf;",
371 " } catch (e) {",
372 " var hello = 1;",
373 " }",
374 "}"
375 ].join("\n"),
376 errors: [error]
377 },
378 {
379 code: [
380 "function foo() {",
381 " var first = 10;",
382 " while (first) {",
383 " var hello = 1;",
384 " }",
385 "}"
386 ].join("\n"),
387 errors: [error]
388 },
389 {
390 code: [
391 "function foo() {",
392 " var first = 10;",
393 " do {",
394 " var hello = 1;",
395 " } while (first == 10);",
396 "}"
397 ].join("\n"),
398 errors: [error]
399 },
400 {
401 code: [
402 "function foo() {",
403 " var first = [1,2,3];",
404 " for (var item in first) {",
405 " item++;",
406 " }",
407 "}"
408 ].join("\n"),
409 errors: [error]
410 },
411 {
412 code: [
413 "function foo() {",
414 " var first = [1,2,3];",
415 " var item;",
416 " for (item in first) {",
417 " var hello = item;",
418 " }",
419 "}"
420 ].join("\n"),
421 errors: [error]
422 },
423 {
424 code: [
425 "var foo = () => {",
426 " var first = [1,2,3];",
427 " var item;",
428 " for (item in first) {",
429 " var hello = item;",
430 " }",
431 "}"
432 ].join("\n"),
433 parserOptions: { ecmaVersion: 6 },
434 errors: [error]
435 },
436 {
437 code: "'use strict'; 0; var x; f();",
438 errors: [error]
439 },
440 {
441 code: "'use strict'; var x; 'directive'; var y; f();",
442 errors: [error]
443 },
444 {
445 code: "function f() { 'use strict'; 0; var x; f(); }",
446 errors: [error]
447 },
448 {
449 code: "function f() { 'use strict'; var x; 'directive'; var y; f(); }",
450 errors: [error]
451 },
452 {
453 code: [
454 "export function f() {}",
455 "var x;"
456 ].join("\n"),
457 parserOptions: {
458 ecmaVersion: 6,
459 sourceType: "module"
460 },
461 errors: [error]
462 },
463 {
464 code: [
465 "var x;",
466 "export function f() {}",
467 "var y;"
468 ].join("\n"),
469 parserOptions: {
470 ecmaVersion: 6,
471 sourceType: "module"
472 },
473 errors: [error]
474 },
475 {
476 code: [
477 "import {foo} from 'foo';",
478 "export {foo};",
479 "var test = 1;"
480 ].join("\n"),
481 parserOptions: {
482 ecmaVersion: 6,
483 sourceType: "module"
484 },
485 errors: [error]
486 },
487 {
488 code: [
489 "export {foo} from 'foo';",
490 "var test = 1;"
491 ].join("\n"),
492 parserOptions: {
493 ecmaVersion: 6,
494 sourceType: "module"
495 },
496 errors: [error]
497 },
498 {
499 code: [
500 "export * from 'foo';",
501 "var test = 1;"
502 ].join("\n"),
503 parserOptions: {
504 ecmaVersion: 6,
505 sourceType: "module"
506 },
507 errors: [error]
508 },
509 {
510 code: [
511 "class C {",
512 " static {",
513 " foo();",
514 " var x;",
515 " }",
516 "}"
517 ].join("\n"),
518 parserOptions: {
519 ecmaVersion: 2022
520 },
521 errors: [error]
522 },
523 {
524 code: [
525 "class C {",
526 " static {",
527 " 'use strict';", // static blocks do not have directives
528 " var x;",
529 " }",
530 "}"
531 ].join("\n"),
532 parserOptions: {
533 ecmaVersion: 2022
534 },
535 errors: [error]
536 },
537 {
538 code: [
539 "class C {",
540 " static {",
541 " var x;",
542 " foo();",
543 " var y;",
544 " }",
545 "}"
546 ].join("\n"),
547 parserOptions: {
548 ecmaVersion: 2022
549 },
550 errors: [{ ...error, line: 5 }]
551 },
552 {
553 code: [
554 "class C {",
555 " static {",
556 " if (foo) {",
557 " var x;",
558 " }",
559 " }",
560 "}"
561 ].join("\n"),
562 parserOptions: {
563 ecmaVersion: 2022
564 },
565 errors: [error]
566 },
567 {
568 code: [
569 "class C {",
570 " static {",
571 " if (foo)",
572 " var x;",
573 " }",
574 "}"
575 ].join("\n"),
576 parserOptions: {
577 ecmaVersion: 2022
578 },
579 errors: [error]
580 }
581 ]
582 });