]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-use-before-define.js
import 8.23.1 source
[pve-eslint.git] / eslint / tests / lib / rules / no-use-before-define.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-use-before-define rule.
3 * @author Ilya Volodin
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-use-before-define"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("no-use-before-define", rule, {
22 valid: [
609c276f
TL
23 "unresolved",
24 "Array",
25 "function foo () { arguments; }",
eb39fafa
DC
26 "var a=10; alert(a);",
27 "function b(a) { alert(a); }",
28 "Object.hasOwnProperty.call(a);",
29 "function a() { alert(arguments);}",
30 { code: "a(); function a() { alert(arguments); }", options: ["nofunc"] },
31 { code: "(() => { var a = 42; alert(a); })();", parserOptions: { ecmaVersion: 6 } },
32 "a(); try { throw new Error() } catch (a) {}",
33 { code: "class A {} new A();", parserOptions: { ecmaVersion: 6 } },
34 "var a = 0, b = a;",
35 { code: "var {a = 0, b = a} = {};", parserOptions: { ecmaVersion: 6 } },
36 { code: "var [a = 0, b = a] = {};", parserOptions: { ecmaVersion: 6 } },
37 "function foo() { foo(); }",
38 "var foo = function() { foo(); };",
39 "var a; for (a in a) {}",
40 { code: "var a; for (a of a) {}", parserOptions: { ecmaVersion: 6 } },
609c276f
TL
41 { code: "let a; class C { static { a; } }", parserOptions: { ecmaVersion: 2022 } },
42 { code: "class C { static { let a; a; } }", parserOptions: { ecmaVersion: 2022 } },
eb39fafa
DC
43
44 // Block-level bindings
45 { code: "\"use strict\"; a(); { function a() {} }", parserOptions: { ecmaVersion: 6 } },
46 { code: "\"use strict\"; { a(); function a() {} }", options: ["nofunc"], parserOptions: { ecmaVersion: 6 } },
47 { code: "switch (foo) { case 1: { a(); } default: { let a; }}", parserOptions: { ecmaVersion: 6 } },
48 { code: "a(); { let a = function () {}; }", parserOptions: { ecmaVersion: 6 } },
49
50 // object style options
51 { code: "a(); function a() { alert(arguments); }", options: [{ functions: false }] },
52 { code: "\"use strict\"; { a(); function a() {} }", options: [{ functions: false }], parserOptions: { ecmaVersion: 6 } },
53 { code: "function foo() { new A(); } class A {};", options: [{ classes: false }], parserOptions: { ecmaVersion: 6 } },
54
55 // "variables" option
56 {
57 code: "function foo() { bar; } var bar;",
58 options: [{ variables: false }]
59 },
60 {
61 code: "var foo = () => bar; var bar;",
62 options: [{ variables: false }],
63 parserOptions: { ecmaVersion: 6 }
609c276f
TL
64 },
65 {
66 code: "class C { static { () => foo; let foo; } }",
67 options: [{ variables: false }],
68 parserOptions: { ecmaVersion: 2022 }
69 },
70
71 // Tests related to class definition evaluation. These are not TDZ errors.
72 { code: "class C extends (class { method() { C; } }) {}", parserOptions: { ecmaVersion: 6 } },
73 { code: "(class extends (class { method() { C; } }) {});", parserOptions: { ecmaVersion: 6 } },
74 { code: "const C = (class extends (class { method() { C; } }) {});", parserOptions: { ecmaVersion: 6 } },
75 { code: "class C extends (class { field = C; }) {}", parserOptions: { ecmaVersion: 2022 } },
76 { code: "(class extends (class { field = C; }) {});", parserOptions: { ecmaVersion: 2022 } },
77 { code: "const C = (class extends (class { field = C; }) {});", parserOptions: { ecmaVersion: 2022 } },
78 { code: "class C { [() => C](){} }", parserOptions: { ecmaVersion: 6 } },
79 { code: "(class C { [() => C](){} });", parserOptions: { ecmaVersion: 6 } },
80 { code: "const C = class { [() => C](){} };", parserOptions: { ecmaVersion: 6 } },
81 { code: "class C { static [() => C](){} }", parserOptions: { ecmaVersion: 6 } },
82 { code: "(class C { static [() => C](){} });", parserOptions: { ecmaVersion: 6 } },
83 { code: "const C = class { static [() => C](){} };", parserOptions: { ecmaVersion: 6 } },
84 { code: "class C { [() => C]; }", parserOptions: { ecmaVersion: 2022 } },
85 { code: "(class C { [() => C]; });", parserOptions: { ecmaVersion: 2022 } },
86 { code: "const C = class { [() => C]; };", parserOptions: { ecmaVersion: 2022 } },
87 { code: "class C { static [() => C]; }", parserOptions: { ecmaVersion: 2022 } },
88 { code: "(class C { static [() => C]; });", parserOptions: { ecmaVersion: 2022 } },
89 { code: "const C = class { static [() => C]; };", parserOptions: { ecmaVersion: 2022 } },
90 { code: "class C { method() { C; } }", parserOptions: { ecmaVersion: 6 } },
91 { code: "(class C { method() { C; } });", parserOptions: { ecmaVersion: 6 } },
92 { code: "const C = class { method() { C; } };", parserOptions: { ecmaVersion: 6 } },
93 { code: "class C { static method() { C; } }", parserOptions: { ecmaVersion: 6 } },
94 { code: "(class C { static method() { C; } });", parserOptions: { ecmaVersion: 6 } },
95 { code: "const C = class { static method() { C; } };", parserOptions: { ecmaVersion: 6 } },
96 { code: "class C { field = C; }", parserOptions: { ecmaVersion: 2022 } },
97 { code: "(class C { field = C; });", parserOptions: { ecmaVersion: 2022 } },
98 { code: "const C = class { field = C; };", parserOptions: { ecmaVersion: 2022 } },
99 { code: "class C { static field = C; }", parserOptions: { ecmaVersion: 2022 } },
100 { code: "(class C { static field = C; });", parserOptions: { ecmaVersion: 2022 } }, // `const C = class { static field = C; };` is TDZ error
101 { code: "class C { static field = class { static field = C; }; }", parserOptions: { ecmaVersion: 2022 } },
102 { code: "(class C { static field = class { static field = C; }; });", parserOptions: { ecmaVersion: 2022 } },
103 { code: "class C { field = () => C; }", parserOptions: { ecmaVersion: 2022 } },
104 { code: "(class C { field = () => C; });", parserOptions: { ecmaVersion: 2022 } },
105 { code: "const C = class { field = () => C; };", parserOptions: { ecmaVersion: 2022 } },
106 { code: "class C { static field = () => C; }", parserOptions: { ecmaVersion: 2022 } },
107 { code: "(class C { static field = () => C; });", parserOptions: { ecmaVersion: 2022 } },
108 { code: "const C = class { static field = () => C; };", parserOptions: { ecmaVersion: 2022 } },
109 { code: "class C { field = class extends C {}; }", parserOptions: { ecmaVersion: 2022 } },
110 { code: "(class C { field = class extends C {}; });", parserOptions: { ecmaVersion: 2022 } },
111 { code: "const C = class { field = class extends C {}; }", parserOptions: { ecmaVersion: 2022 } },
112 { code: "class C { static field = class extends C {}; }", parserOptions: { ecmaVersion: 2022 } },
113 { code: "(class C { static field = class extends C {}; });", parserOptions: { ecmaVersion: 2022 } }, // `const C = class { static field = class extends C {}; };` is TDZ error
114 { code: "class C { static field = class { [C]; }; }", parserOptions: { ecmaVersion: 2022 } },
115 { code: "(class C { static field = class { [C]; }; });", parserOptions: { ecmaVersion: 2022 } }, // `const C = class { static field = class { [C]; } };` is TDZ error
116 { code: "const C = class { static field = class { field = C; }; };", parserOptions: { ecmaVersion: 2022 } },
117 {
118 code: "class C { method() { a; } } let a;",
119 options: [{ variables: false }],
120 parserOptions: { ecmaVersion: 6 }
121 },
122 {
123 code: "class C { static method() { a; } } let a;",
124 options: [{ variables: false }],
125 parserOptions: { ecmaVersion: 6 }
126 },
127 {
128 code: "class C { field = a; } let a;", // `class C { static field = a; } let a;` is TDZ error
129 options: [{ variables: false }],
130 parserOptions: { ecmaVersion: 2022 }
131 },
132 {
133 code: "class C { field = D; } class D {}", // `class C { static field = D; } class D {}` is TDZ error
134 options: [{ classes: false }],
135 parserOptions: { ecmaVersion: 2022 }
136 },
137 {
138 code: "class C { field = class extends D {}; } class D {}", // `class C { static field = class extends D {}; } class D {}` is TDZ error
139 options: [{ classes: false }],
140 parserOptions: { ecmaVersion: 2022 }
141 },
142 {
143 code: "class C { field = () => a; } let a;",
144 options: [{ variables: false }],
145 parserOptions: { ecmaVersion: 2022 }
146 },
147 {
148 code: "class C { static field = () => a; } let a;",
149 options: [{ variables: false }],
150 parserOptions: { ecmaVersion: 2022 }
151 },
152 {
153 code: "class C { field = () => D; } class D {}",
154 options: [{ classes: false }],
155 parserOptions: { ecmaVersion: 2022 }
156 },
157 {
158 code: "class C { static field = () => D; } class D {}",
159 options: [{ classes: false }],
160 parserOptions: { ecmaVersion: 2022 }
161 },
162 {
163 code: "class C { static field = class { field = a; }; } let a;",
164 options: [{ variables: false }],
165 parserOptions: { ecmaVersion: 2022 }
166 },
167 {
168 code: "class C { static { C; } }", // `const C = class { static { C; } }` is TDZ error
169 parserOptions: { ecmaVersion: 2022 }
170 },
171 {
172 code: "class C { static { C; } static {} static { C; } }",
173 parserOptions: { ecmaVersion: 2022 }
174 },
175 {
176 code: "(class C { static { C; } })",
177 parserOptions: { ecmaVersion: 2022 }
178 },
179 {
180 code: "class C { static { class D extends C {} } }",
181 parserOptions: { ecmaVersion: 2022 }
182 },
183 {
184 code: "class C { static { (class { static { C } }) } }",
185 parserOptions: { ecmaVersion: 2022 }
186 },
187 {
188 code: "class C { static { () => C; } }",
189 parserOptions: { ecmaVersion: 2022 }
190 },
191 {
192 code: "(class C { static { () => C; } })",
193 parserOptions: { ecmaVersion: 2022 }
194 },
195 {
196 code: "const C = class { static { () => C; } }",
197 parserOptions: { ecmaVersion: 2022 }
198 },
199 {
200 code: "class C { static { () => D; } } class D {}",
201 options: [{ classes: false }],
202 parserOptions: { ecmaVersion: 2022 }
203 },
204 {
205 code: "class C { static { () => a; } } let a;",
206 options: [{ variables: false }],
207 parserOptions: { ecmaVersion: 2022 }
208 },
209 {
210 code: "const C = class C { static { C.x; } }",
211 parserOptions: { ecmaVersion: 2022 }
8f9d1d4d
DC
212 },
213
214 // "allowNamedExports" option
215 {
216 code: "export { a }; const a = 1;",
217 options: [{ allowNamedExports: true }],
218 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
219 },
220 {
221 code: "export { a as b }; const a = 1;",
222 options: [{ allowNamedExports: true }],
223 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
224 },
225 {
226 code: "export { a, b }; let a, b;",
227 options: [{ allowNamedExports: true }],
228 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
229 },
230 {
231 code: "export { a }; var a;",
232 options: [{ allowNamedExports: true }],
233 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
234 },
235 {
236 code: "export { f }; function f() {}",
237 options: [{ allowNamedExports: true }],
238 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
239 },
240 {
241 code: "export { C }; class C {}",
242 options: [{ allowNamedExports: true }],
243 parserOptions: { ecmaVersion: 2015, sourceType: "module" }
eb39fafa
DC
244 }
245 ],
246 invalid: [
247 {
248 code: "a++; var a=19;",
249 parserOptions: { ecmaVersion: 6, sourceType: "module" },
250 errors: [{
251 messageId: "usedBeforeDefined",
252 data: { name: "a" },
253 type: "Identifier"
254 }]
255 },
256 {
257 code: "a++; var a=19;",
258 parserOptions: { parserOptions: { ecmaVersion: 6 } },
259 errors: [{
260 messageId: "usedBeforeDefined",
261 data: { name: "a" },
262 type: "Identifier"
263 }]
264 },
265 {
266 code: "a++; var a=19;",
267 errors: [{
268 messageId: "usedBeforeDefined",
269 data: { name: "a" },
270 type: "Identifier"
271 }]
272 },
273 {
274 code: "a(); var a=function() {};",
275 errors: [{
276 messageId: "usedBeforeDefined",
277 data: { name: "a" },
278 type: "Identifier"
279 }]
280 },
281 {
282 code: "alert(a[1]); var a=[1,3];",
283 errors: [{
284 messageId: "usedBeforeDefined",
285 data: { name: "a" },
286 type: "Identifier"
287 }]
288 },
289 {
290 code: "a(); function a() { alert(b); var b=10; a(); }",
291 errors: [
292 {
293 messageId: "usedBeforeDefined",
294 data: { name: "a" },
295 type: "Identifier"
296 },
297 {
298 messageId: "usedBeforeDefined",
299 data: { name: "b" },
300 type: "Identifier"
301 }
302 ]
303 },
304 {
305 code: "a(); var a=function() {};",
306 options: ["nofunc"],
307 errors: [{
308 messageId: "usedBeforeDefined",
309 data: { name: "a" },
310 type: "Identifier"
311 }]
312 },
313 {
314 code: "(() => { alert(a); var a = 42; })();",
315 parserOptions: { ecmaVersion: 6 },
316 errors: [{
317 messageId: "usedBeforeDefined",
318 data: { name: "a" },
319 type: "Identifier"
320 }]
321 },
322 {
323 code: "(() => a())(); function a() { }",
324 parserOptions: { ecmaVersion: 6 },
325 errors: [{
326 messageId: "usedBeforeDefined",
327 data: { name: "a" },
328 type: "Identifier"
329 }]
330 },
331 {
332 code: "\"use strict\"; a(); { function a() {} }",
333 errors: [{
334 messageId: "usedBeforeDefined",
335 data: { name: "a" },
336 type: "Identifier"
337 }]
338 },
339 {
340 code: "a(); try { throw new Error() } catch (foo) {var a;}",
341 errors: [{
342 messageId: "usedBeforeDefined",
343 data: { name: "a" },
344 type: "Identifier"
345 }]
346 },
347 {
348 code: "var f = () => a; var a;",
349 parserOptions: { ecmaVersion: 6 },
350 errors: [{
351 messageId: "usedBeforeDefined",
352 data: { name: "a" },
353 type: "Identifier"
354 }]
355 },
356 {
357 code: "new A(); class A {};",
358 parserOptions: { ecmaVersion: 6 },
359 errors: [{
360 messageId: "usedBeforeDefined",
361 data: { name: "A" },
362 type: "Identifier"
363 }]
364 },
365 {
366 code: "function foo() { new A(); } class A {};",
367 parserOptions: { ecmaVersion: 6 },
368 errors: [{
369 messageId: "usedBeforeDefined",
370 data: { name: "A" },
371 type: "Identifier"
372 }]
373 },
374 {
375 code: "new A(); var A = class {};",
376 parserOptions: { ecmaVersion: 6 },
377 errors: [{
378 messageId: "usedBeforeDefined",
379 data: { name: "A" },
380 type: "Identifier"
381 }]
382 },
383 {
384 code: "function foo() { new A(); } var A = class {};",
385 parserOptions: { ecmaVersion: 6 },
386 errors: [{
387 messageId: "usedBeforeDefined",
388 data: { name: "A" },
389 type: "Identifier"
390 }]
391 },
392
393 // Block-level bindings
394 {
395 code: "a++; { var a; }",
396 parserOptions: { ecmaVersion: 6 },
397 errors: [{
398 messageId: "usedBeforeDefined",
399 data: { name: "a" },
400 type: "Identifier"
401 }]
402 },
403 {
404 code: "\"use strict\"; { a(); function a() {} }",
405 parserOptions: { ecmaVersion: 6 },
406 errors: [{
407 messageId: "usedBeforeDefined",
408 data: { name: "a" },
409 type: "Identifier"
410 }]
411 },
412 {
413 code: "{a; let a = 1}",
414 parserOptions: { ecmaVersion: 6 },
415 errors: [{
416 messageId: "usedBeforeDefined",
417 data: { name: "a" },
418 type: "Identifier"
419 }]
420 },
421 {
422 code: "switch (foo) { case 1: a();\n default: \n let a;}",
423 parserOptions: { ecmaVersion: 6 },
424 errors: [{
425 messageId: "usedBeforeDefined",
426 data: { name: "a" },
427 type: "Identifier"
428 }]
429 },
430 {
431 code: "if (true) { function foo() { a; } let a;}",
432 parserOptions: { ecmaVersion: 6 },
433 errors: [{
434 messageId: "usedBeforeDefined",
435 data: { name: "a" },
436 type: "Identifier"
437 }]
438 },
439
440 // object style options
441 {
442 code: "a(); var a=function() {};",
443 options: [{ functions: false, classes: false }],
444 errors: [{
445 messageId: "usedBeforeDefined",
446 data: { name: "a" },
447 type: "Identifier"
448 }]
449 },
450 {
451 code: "new A(); class A {};",
452 options: [{ functions: false, classes: false }],
453 parserOptions: { ecmaVersion: 6 },
454 errors: [{
455 messageId: "usedBeforeDefined",
456 data: { name: "A" },
457 type: "Identifier"
458 }]
459 },
460 {
461 code: "new A(); var A = class {};",
462 options: [{ classes: false }],
463 parserOptions: { ecmaVersion: 6 },
464 errors: [{
465 messageId: "usedBeforeDefined",
466 data: { name: "A" },
467 type: "Identifier"
468 }]
469 },
470 {
471 code: "function foo() { new A(); } var A = class {};",
472 options: [{ classes: false }],
473 parserOptions: { ecmaVersion: 6 },
474 errors: [{
475 messageId: "usedBeforeDefined",
476 data: { name: "A" },
477 type: "Identifier"
478 }]
479 },
480
481 // invalid initializers
482 {
483 code: "var a = a;",
484 errors: [{
485 messageId: "usedBeforeDefined",
486 data: { name: "a" },
487 type: "Identifier"
488 }]
489 },
490 {
491 code: "let a = a + b;",
492 parserOptions: { ecmaVersion: 6 },
493 errors: [{
494 messageId: "usedBeforeDefined",
495 data: { name: "a" },
496 type: "Identifier"
497 }]
498 },
499 {
500 code: "const a = foo(a);",
501 parserOptions: { ecmaVersion: 6 },
502 errors: [{
503 messageId: "usedBeforeDefined",
504 data: { name: "a" },
505 type: "Identifier"
506 }]
507 },
508 {
509 code: "function foo(a = a) {}",
510 parserOptions: { ecmaVersion: 6 },
511 errors: [{
512 messageId: "usedBeforeDefined",
513 data: { name: "a" },
514 type: "Identifier"
515 }]
516 },
517 {
518 code: "var {a = a} = [];",
519 parserOptions: { ecmaVersion: 6 },
520 errors: [{
521 messageId: "usedBeforeDefined",
522 data: { name: "a" },
523 type: "Identifier"
524 }]
525 },
526 {
527 code: "var [a = a] = [];",
528 parserOptions: { ecmaVersion: 6 },
529 errors: [{
530 messageId: "usedBeforeDefined",
531 data: { name: "a" },
532 type: "Identifier"
533 }]
534 },
535 {
536 code: "var {b = a, a} = {};",
537 parserOptions: { ecmaVersion: 6 },
538 errors: [{
539 messageId: "usedBeforeDefined",
540 data: { name: "a" },
541 type: "Identifier"
542 }]
543 },
544 {
545 code: "var [b = a, a] = {};",
546 parserOptions: { ecmaVersion: 6 },
547 errors: [{
548 messageId: "usedBeforeDefined",
549 data: { name: "a" },
550 type: "Identifier"
551 }]
552 },
553 {
554 code: "var {a = 0} = a;",
555 parserOptions: { ecmaVersion: 6 },
556 errors: [{
557 messageId: "usedBeforeDefined",
558 data: { name: "a" },
559 type: "Identifier"
560 }]
561 },
562 {
563 code: "var [a = 0] = a;",
564 parserOptions: { ecmaVersion: 6 },
565 errors: [{
566 messageId: "usedBeforeDefined",
567 data: { name: "a" },
568 type: "Identifier"
569 }]
570 },
571 {
572 code: "for (var a in a) {}",
573 errors: [{
574 messageId: "usedBeforeDefined",
575 data: { name: "a" },
576 type: "Identifier"
577 }]
578 },
579 {
580 code: "for (var a of a) {}",
581 parserOptions: { ecmaVersion: 6 },
582 errors: [{
583 messageId: "usedBeforeDefined",
584 data: { name: "a" },
585 type: "Identifier"
586 }]
587 },
588
589 // "variables" option
590 {
591 code: "function foo() { bar; var bar = 1; } var bar;",
592 options: [{ variables: false }],
593 errors: [{
594 messageId: "usedBeforeDefined",
595 data: { name: "bar" },
596 type: "Identifier"
597 }]
598 },
599 {
600 code: "foo; var foo;",
601 options: [{ variables: false }],
602 errors: [{
603 messageId: "usedBeforeDefined",
604 data: { name: "foo" },
605 type: "Identifier"
606 }]
607 },
608
609 // https://github.com/eslint/eslint/issues/10227
610 {
611 code: "for (let x = x;;); let x = 0",
612 parserOptions: { ecmaVersion: 2015 },
613 errors: [{
614 messageId: "usedBeforeDefined",
615 data: { name: "x" }
616 }]
617 },
618 {
619 code: "for (let x in xs); let xs = []",
620 parserOptions: { ecmaVersion: 2015 },
621 errors: [{
622 messageId: "usedBeforeDefined",
623 data: { name: "xs" }
624 }]
625 },
626 {
627 code: "for (let x of xs); let xs = []",
628 parserOptions: { ecmaVersion: 2015 },
629 errors: [{
630 messageId: "usedBeforeDefined",
631 data: { name: "xs" }
632 }]
633 },
634 {
635 code: "try {} catch ({message = x}) {} let x = ''",
636 parserOptions: { ecmaVersion: 2015 },
637 errors: [{
638 messageId: "usedBeforeDefined",
639 data: { name: "x" }
640 }]
641 },
642 {
643 code: "with (obj) x; let x = {}",
644 parserOptions: { ecmaVersion: 2015 },
645 errors: [{
646 messageId: "usedBeforeDefined",
647 data: { name: "x" }
648 }]
649 },
650
651 // WithStatements.
652 {
653 code: "with (x); let x = {}",
654 parserOptions: { ecmaVersion: 2015 },
655 errors: [{
656 messageId: "usedBeforeDefined",
657 data: { name: "x" }
658 }]
659 },
660 {
661 code: "with (obj) { x } let x = {}",
662 parserOptions: { ecmaVersion: 2015 },
663 errors: [{
664 messageId: "usedBeforeDefined",
665 data: { name: "x" }
666 }]
667 },
668 {
669 code: "with (obj) { if (a) { x } } let x = {}",
670 parserOptions: { ecmaVersion: 2015 },
671 errors: [{
672 messageId: "usedBeforeDefined",
673 data: { name: "x" }
674 }]
675 },
676 {
677 code: "with (obj) { (() => { if (a) { x } })() } let x = {}",
678 parserOptions: { ecmaVersion: 2015 },
679 errors: [{
680 messageId: "usedBeforeDefined",
681 data: { name: "x" }
682 }]
609c276f
TL
683 },
684
685 // Tests related to class definition evaluation. These are TDZ errors.
686 {
687 code: "class C extends C {}",
688 options: [{ classes: false }],
689 parserOptions: { ecmaVersion: 6 },
690 errors: [{
691 messageId: "usedBeforeDefined",
692 data: { name: "C" }
693 }]
694 },
695 {
696 code: "const C = class extends C {};",
697 options: [{ variables: false }],
698 parserOptions: { ecmaVersion: 6 },
699 errors: [{
700 messageId: "usedBeforeDefined",
701 data: { name: "C" }
702 }]
703 },
704 {
705 code: "class C extends (class { [C](){} }) {}",
706 options: [{ classes: false }],
707 parserOptions: { ecmaVersion: 6 },
708 errors: [{
709 messageId: "usedBeforeDefined",
710 data: { name: "C" }
711 }]
712 },
713 {
714 code: "const C = class extends (class { [C](){} }) {};",
715 options: [{ variables: false }],
716 parserOptions: { ecmaVersion: 6 },
717 errors: [{
718 messageId: "usedBeforeDefined",
719 data: { name: "C" }
720 }]
721 },
722 {
723 code: "class C extends (class { static field = C; }) {}",
724 options: [{ classes: false }],
725 parserOptions: { ecmaVersion: 2022 },
726 errors: [{
727 messageId: "usedBeforeDefined",
728 data: { name: "C" }
729 }]
730 },
731 {
732 code: "const C = class extends (class { static field = C; }) {};",
733 options: [{ variables: false }],
734 parserOptions: { ecmaVersion: 2022 },
735 errors: [{
736 messageId: "usedBeforeDefined",
737 data: { name: "C" }
738 }]
739 },
740 {
741 code: "class C { [C](){} }",
742 options: [{ classes: false }],
743 parserOptions: { ecmaVersion: 6 },
744 errors: [{
745 messageId: "usedBeforeDefined",
746 data: { name: "C" }
747 }]
748 },
749 {
750 code: "(class C { [C](){} });",
751 options: [{ classes: false }],
752 parserOptions: { ecmaVersion: 6 },
753 errors: [{
754 messageId: "usedBeforeDefined",
755 data: { name: "C" }
756 }]
757 },
758 {
759 code: "const C = class { [C](){} };",
760 options: [{ variables: false }],
761 parserOptions: { ecmaVersion: 6 },
762 errors: [{
763 messageId: "usedBeforeDefined",
764 data: { name: "C" }
765 }]
766 },
767 {
768 code: "class C { static [C](){} }",
769 options: [{ classes: false }],
770 parserOptions: { ecmaVersion: 6 },
771 errors: [{
772 messageId: "usedBeforeDefined",
773 data: { name: "C" }
774 }]
775 },
776 {
777 code: "(class C { static [C](){} });",
778 options: [{ classes: false }],
779 parserOptions: { ecmaVersion: 6 },
780 errors: [{
781 messageId: "usedBeforeDefined",
782 data: { name: "C" }
783 }]
784 },
785 {
786 code: "const C = class { static [C](){} };",
787 options: [{ variables: false }],
788 parserOptions: { ecmaVersion: 6 },
789 errors: [{
790 messageId: "usedBeforeDefined",
791 data: { name: "C" }
792 }]
793 },
794 {
795 code: "class C { [C]; }",
796 options: [{ classes: false }],
797 parserOptions: { ecmaVersion: 2022 },
798 errors: [{
799 messageId: "usedBeforeDefined",
800 data: { name: "C" }
801 }]
802 },
803 {
804 code: "(class C { [C]; });",
805 options: [{ classes: false }],
806 parserOptions: { ecmaVersion: 2022 },
807 errors: [{
808 messageId: "usedBeforeDefined",
809 data: { name: "C" }
810 }]
811 },
812 {
813 code: "const C = class { [C]; };",
814 options: [{ variables: false }],
815 parserOptions: { ecmaVersion: 2022 },
816 errors: [{
817 messageId: "usedBeforeDefined",
818 data: { name: "C" }
819 }]
820 },
821 {
822 code: "class C { [C] = foo; }",
823 options: [{ classes: false }],
824 parserOptions: { ecmaVersion: 2022 },
825 errors: [{
826 messageId: "usedBeforeDefined",
827 data: { name: "C" }
828 }]
829 },
830 {
831 code: "(class C { [C] = foo; });",
832 options: [{ classes: false }],
833 parserOptions: { ecmaVersion: 2022 },
834 errors: [{
835 messageId: "usedBeforeDefined",
836 data: { name: "C" }
837 }]
838 },
839 {
840 code: "const C = class { [C] = foo; };",
841 options: [{ variables: false }],
842 parserOptions: { ecmaVersion: 2022 },
843 errors: [{
844 messageId: "usedBeforeDefined",
845 data: { name: "C" }
846 }]
847 },
848 {
849 code: "class C { static [C]; }",
850 options: [{ classes: false }],
851 parserOptions: { ecmaVersion: 2022 },
852 errors: [{
853 messageId: "usedBeforeDefined",
854 data: { name: "C" }
855 }]
856 },
857 {
858 code: "(class C { static [C]; });",
859 options: [{ classes: false }],
860 parserOptions: { ecmaVersion: 2022 },
861 errors: [{
862 messageId: "usedBeforeDefined",
863 data: { name: "C" }
864 }]
865 },
866 {
867 code: "const C = class { static [C]; };",
868 options: [{ variables: false }],
869 parserOptions: { ecmaVersion: 2022 },
870 errors: [{
871 messageId: "usedBeforeDefined",
872 data: { name: "C" }
873 }]
874 },
875 {
876 code: "class C { static [C] = foo; }",
877 options: [{ classes: false }],
878 parserOptions: { ecmaVersion: 2022 },
879 errors: [{
880 messageId: "usedBeforeDefined",
881 data: { name: "C" }
882 }]
883 },
884 {
885 code: "(class C { static [C] = foo; });",
886 options: [{ classes: false }],
887 parserOptions: { ecmaVersion: 2022 },
888 errors: [{
889 messageId: "usedBeforeDefined",
890 data: { name: "C" }
891 }]
892 },
893 {
894 code: "const C = class { static [C] = foo; };",
895 options: [{ variables: false }],
896 parserOptions: { ecmaVersion: 2022 },
897 errors: [{
898 messageId: "usedBeforeDefined",
899 data: { name: "C" }
900 }]
901 },
902 {
903 code: "const C = class { static field = C; };",
904 options: [{ variables: false }],
905 parserOptions: { ecmaVersion: 2022 },
906 errors: [{
907 messageId: "usedBeforeDefined",
908 data: { name: "C" }
909 }]
910 },
911 {
912 code: "const C = class { static field = class extends C {}; };",
913 options: [{ variables: false }],
914 parserOptions: { ecmaVersion: 2022 },
915 errors: [{
916 messageId: "usedBeforeDefined",
917 data: { name: "C" }
918 }]
919 },
920 {
921 code: "const C = class { static field = class { [C]; } };",
922 options: [{ variables: false }],
923 parserOptions: { ecmaVersion: 2022 },
924 errors: [{
925 messageId: "usedBeforeDefined",
926 data: { name: "C" }
927 }]
928 },
929 {
930 code: "const C = class { static field = class { static field = C; }; };",
931 options: [{ variables: false }],
932 parserOptions: { ecmaVersion: 2022 },
933 errors: [{
934 messageId: "usedBeforeDefined",
935 data: { name: "C" }
936 }]
937 },
938 {
939 code: "class C extends D {} class D {}",
940 options: [{ classes: false }],
941 parserOptions: { ecmaVersion: 6 },
942 errors: [{
943 messageId: "usedBeforeDefined",
944 data: { name: "D" }
945 }]
946 },
947 {
948 code: "class C extends (class { [a](){} }) {} let a;",
949 options: [{ variables: false }],
950 parserOptions: { ecmaVersion: 6 },
951 errors: [{
952 messageId: "usedBeforeDefined",
953 data: { name: "a" }
954 }]
955 },
956 {
957 code: "class C extends (class { static field = a; }) {} let a;",
958 options: [{ variables: false }],
959 parserOptions: { ecmaVersion: 2022 },
960 errors: [{
961 messageId: "usedBeforeDefined",
962 data: { name: "a" }
963 }]
964 },
965 {
966 code: "class C { [a]() {} } let a;",
967 options: [{ variables: false }],
968 parserOptions: { ecmaVersion: 6 },
969 errors: [{
970 messageId: "usedBeforeDefined",
971 data: { name: "a" }
972 }]
973 },
974 {
975 code: "class C { static [a]() {} } let a;",
976 options: [{ variables: false }],
977 parserOptions: { ecmaVersion: 6 },
978 errors: [{
979 messageId: "usedBeforeDefined",
980 data: { name: "a" }
981 }]
982 },
983 {
984 code: "class C { [a]; } let a;",
985 options: [{ variables: false }],
986 parserOptions: { ecmaVersion: 2022 },
987 errors: [{
988 messageId: "usedBeforeDefined",
989 data: { name: "a" }
990 }]
991 },
992 {
993 code: "class C { static [a]; } let a;",
994 options: [{ variables: false }],
995 parserOptions: { ecmaVersion: 2022 },
996 errors: [{
997 messageId: "usedBeforeDefined",
998 data: { name: "a" }
999 }]
1000 },
1001 {
1002 code: "class C { [a] = foo; } let a;",
1003 options: [{ variables: false }],
1004 parserOptions: { ecmaVersion: 2022 },
1005 errors: [{
1006 messageId: "usedBeforeDefined",
1007 data: { name: "a" }
1008 }]
1009 },
1010 {
1011 code: "class C { static [a] = foo; } let a;",
1012 options: [{ variables: false }],
1013 parserOptions: { ecmaVersion: 2022 },
1014 errors: [{
1015 messageId: "usedBeforeDefined",
1016 data: { name: "a" }
1017 }]
1018 },
1019 {
1020 code: "class C { static field = a; } let a;",
1021 options: [{ variables: false }],
1022 parserOptions: { ecmaVersion: 2022 },
1023 errors: [{
1024 messageId: "usedBeforeDefined",
1025 data: { name: "a" }
1026 }]
1027 },
1028 {
1029 code: "class C { static field = D; } class D {}",
1030 options: [{ classes: false }],
1031 parserOptions: { ecmaVersion: 2022 },
1032 errors: [{
1033 messageId: "usedBeforeDefined",
1034 data: { name: "D" }
1035 }]
1036 },
1037 {
1038 code: "class C { static field = class extends D {}; } class D {}",
1039 options: [{ classes: false }],
1040 parserOptions: { ecmaVersion: 2022 },
1041 errors: [{
1042 messageId: "usedBeforeDefined",
1043 data: { name: "D" }
1044 }]
1045 },
1046 {
1047 code: "class C { static field = class { [a](){} } } let a;",
1048 options: [{ variables: false }],
1049 parserOptions: { ecmaVersion: 2022 },
1050 errors: [{
1051 messageId: "usedBeforeDefined",
1052 data: { name: "a" }
1053 }]
1054 },
1055 {
1056 code: "class C { static field = class { static field = a; }; } let a;",
1057 options: [{ variables: false }],
1058 parserOptions: { ecmaVersion: 2022 },
1059 errors: [{
1060 messageId: "usedBeforeDefined",
1061 data: { name: "a" }
1062 }]
1063 },
1064 {
1065 code: "const C = class { static { C; } };",
1066 options: [{ variables: false }],
1067 parserOptions: { ecmaVersion: 2022 },
1068 errors: [{
1069 messageId: "usedBeforeDefined",
1070 data: { name: "C" }
1071 }]
1072 },
1073 {
1074 code: "const C = class { static { (class extends C {}); } };",
1075 options: [{ variables: false }],
1076 parserOptions: { ecmaVersion: 2022 },
1077 errors: [{
1078 messageId: "usedBeforeDefined",
1079 data: { name: "C" }
1080 }]
1081 },
1082 {
1083 code: "class C { static { a; } } let a;",
1084 options: [{ variables: false }],
1085 parserOptions: { ecmaVersion: 2022 },
1086 errors: [{
1087 messageId: "usedBeforeDefined",
1088 data: { name: "a" }
1089 }]
1090 },
1091 {
1092 code: "class C { static { D; } } class D {}",
1093 options: [{ classes: false }],
1094 parserOptions: { ecmaVersion: 2022 },
1095 errors: [{
1096 messageId: "usedBeforeDefined",
1097 data: { name: "D" }
1098 }]
1099 },
1100 {
1101 code: "class C { static { (class extends D {}); } } class D {}",
1102 options: [{ classes: false }],
1103 parserOptions: { ecmaVersion: 2022 },
1104 errors: [{
1105 messageId: "usedBeforeDefined",
1106 data: { name: "D" }
1107 }]
1108 },
1109 {
1110 code: "class C { static { (class { [a](){} }); } } let a;",
1111 options: [{ variables: false }],
1112 parserOptions: { ecmaVersion: 2022 },
1113 errors: [{
1114 messageId: "usedBeforeDefined",
1115 data: { name: "a" }
1116 }]
1117 },
1118 {
1119 code: "class C { static { (class { static field = a; }); } } let a;",
1120 options: [{ variables: false }],
1121 parserOptions: { ecmaVersion: 2022 },
1122 errors: [{
1123 messageId: "usedBeforeDefined",
1124 data: { name: "a" }
1125 }]
8f9d1d4d 1126 },
609c276f
TL
1127
1128 /*
1129 * TODO(mdjermanovic): Add the following test cases once https://github.com/eslint/eslint-scope/issues/59 gets fixed:
1130 * {
1131 * code: "(class C extends C {});",
1132 * options: [{ classes: false }],
1133 * parserOptions: { ecmaVersion: 6 },
1134 * errors: [{
1135 * messageId: "usedBeforeDefined",
1136 * data: { name: "C" }
1137 * }]
1138 * },
1139 * {
1140 * code: "(class C extends (class { [C](){} }) {});",
1141 * options: [{ classes: false }],
1142 * parserOptions: { ecmaVersion: 6 },
1143 * errors: [{
1144 * messageId: "usedBeforeDefined",
1145 * data: { name: "C" }
1146 * }]
1147 * },
1148 * {
1149 * code: "(class C extends (class { static field = C; }) {});",
1150 * options: [{ classes: false }],
1151 * parserOptions: { ecmaVersion: 2022 },
1152 * errors: [{
1153 * messageId: "usedBeforeDefined",
1154 * data: { name: "C" }
1155 * }]
1156 * }
1157 */
8f9d1d4d
DC
1158
1159 // "allowNamedExports" option
1160 {
1161 code: "export { a }; const a = 1;",
1162 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1163 errors: [{
1164 messageId: "usedBeforeDefined",
1165 data: { name: "a" }
1166 }]
1167 },
1168 {
1169 code: "export { a }; const a = 1;",
1170 options: [{}],
1171 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1172 errors: [{
1173 messageId: "usedBeforeDefined",
1174 data: { name: "a" }
1175 }]
1176 },
1177 {
1178 code: "export { a }; const a = 1;",
1179 options: [{ allowNamedExports: false }],
1180 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1181 errors: [{
1182 messageId: "usedBeforeDefined",
1183 data: { name: "a" }
1184 }]
1185 },
1186 {
1187 code: "export { a }; const a = 1;",
1188 options: ["nofunc"],
1189 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1190 errors: [{
1191 messageId: "usedBeforeDefined",
1192 data: { name: "a" }
1193 }]
1194 },
1195 {
1196 code: "export { a as b }; const a = 1;",
1197 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1198 errors: [{
1199 messageId: "usedBeforeDefined",
1200 data: { name: "a" }
1201 }]
1202 },
1203 {
1204 code: "export { a, b }; let a, b;",
1205 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1206 errors: [
1207 {
1208 messageId: "usedBeforeDefined",
1209 data: { name: "a" }
1210 },
1211 {
1212 messageId: "usedBeforeDefined",
1213 data: { name: "b" }
1214 }
1215 ]
1216 },
1217 {
1218 code: "export { a }; var a;",
1219 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1220 errors: [{
1221 messageId: "usedBeforeDefined",
1222 data: { name: "a" }
1223 }]
1224 },
1225 {
1226 code: "export { f }; function f() {}",
1227 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1228 errors: [{
1229 messageId: "usedBeforeDefined",
1230 data: { name: "f" }
1231 }]
1232 },
1233 {
1234 code: "export { C }; class C {}",
1235 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1236 errors: [{
1237 messageId: "usedBeforeDefined",
1238 data: { name: "C" }
1239 }]
1240 },
1241 {
1242 code: "export const foo = a; const a = 1;",
1243 options: [{ allowNamedExports: true }],
1244 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1245 errors: [{
1246 messageId: "usedBeforeDefined",
1247 data: { name: "a" }
1248 }]
1249 },
1250 {
1251 code: "export default a; const a = 1;",
1252 options: [{ allowNamedExports: true }],
1253 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1254 errors: [{
1255 messageId: "usedBeforeDefined",
1256 data: { name: "a" }
1257 }]
1258 },
1259 {
1260 code: "export function foo() { return a; }; const a = 1;",
1261 options: [{ allowNamedExports: true }],
1262 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1263 errors: [{
1264 messageId: "usedBeforeDefined",
1265 data: { name: "a" }
1266 }]
1267 },
1268 {
1269 code: "export class C { foo() { return a; } }; const a = 1;",
1270 options: [{ allowNamedExports: true }],
1271 parserOptions: { ecmaVersion: 2015, sourceType: "module" },
1272 errors: [{
1273 messageId: "usedBeforeDefined",
1274 data: { name: "a" }
1275 }]
1276 }
eb39fafa
DC
1277 ]
1278});