]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-const.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / prefer-const.js
1 /**
2 * @fileoverview Tests for prefer-const rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-const"),
13 fixtureParser = require("../../fixtures/fixture-parser"),
14 { RuleTester } = require("../../../lib/rule-tester");
15
16 //------------------------------------------------------------------------------
17 // Tests
18 //------------------------------------------------------------------------------
19
20 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
21
22 ruleTester.defineRule("use-x", context => ({
23 VariableDeclaration() {
24 context.markVariableAsUsed("x");
25 }
26 }));
27
28 ruleTester.run("prefer-const", rule, {
29 valid: [
30 "var x = 0;",
31 "let x;",
32 "let x; { x = 0; } foo(x);",
33 "let x = 0; x = 1;",
34 "const x = 0;",
35 "for (let i = 0, end = 10; i < end; ++i) {}",
36 "for (let i in [1,2,3]) { i = 0; }",
37 "for (let x of [1,2,3]) { x = 0; }",
38 "(function() { var x = 0; })();",
39 "(function() { let x; })();",
40 "(function() { let x; { x = 0; } foo(x); })();",
41 "(function() { let x = 0; x = 1; })();",
42 "(function() { const x = 0; })();",
43 "(function() { for (let i = 0, end = 10; i < end; ++i) {} })();",
44 "(function() { for (let i in [1,2,3]) { i = 0; } })();",
45 "(function() { for (let x of [1,2,3]) { x = 0; } })();",
46 "(function(x = 0) { })();",
47 "let a; while (a = foo());",
48 "let a; do {} while (a = foo());",
49 "let a; for (; a = foo(); );",
50 "let a; for (;; ++a);",
51 "let a; for (const {b = ++a} in foo());",
52 "let a; for (const {b = ++a} of foo());",
53 "let a; for (const x of [1,2,3]) { if (a) {} a = foo(); }",
54 "let a; for (const x of [1,2,3]) { a = a || foo(); bar(a); }",
55 "let a; for (const x of [1,2,3]) { foo(++a); }",
56 "let a; function foo() { if (a) {} a = bar(); }",
57 "let a; function foo() { a = a || bar(); baz(a); }",
58 "let a; function foo() { bar(++a); }",
59 [
60 "let id;",
61 "function foo() {",
62 " if (typeof id !== 'undefined') {",
63 " return;",
64 " }",
65 " id = setInterval(() => {}, 250);",
66 "}",
67 "foo();"
68 ].join("\n"),
69 "/*exported a*/ let a; function init() { a = foo(); }",
70 "/*exported a*/ let a = 1",
71 "let a; if (true) a = 0; foo(a);",
72 `
73 (function (a) {
74 let b;
75 ({ a, b } = obj);
76 })();
77 `,
78 `
79 (function (a) {
80 let b;
81 ([ a, b ] = obj);
82 })();
83 `,
84 "var a; { var b; ({ a, b } = obj); }",
85 "let a; { let b; ({ a, b } = obj); }",
86 "var a; { var b; ([ a, b ] = obj); }",
87 "let a; { let b; ([ a, b ] = obj); }",
88
89 /*
90 * The assignment is located in a different scope.
91 * Those are warned by prefer-smaller-scope.
92 */
93 "let x; { x = 0; foo(x); }",
94 "(function() { let x; { x = 0; foo(x); } })();",
95 "let x; for (const a of [1,2,3]) { x = foo(); bar(x); }",
96 "(function() { let x; for (const a of [1,2,3]) { x = foo(); bar(x); } })();",
97 "let x; for (x of array) { x; }",
98
99 {
100 code: "let {a, b} = obj; b = 0;",
101 options: [{ destructuring: "all" }]
102 },
103 {
104 code: "let a, b; ({a, b} = obj); b++;",
105 options: [{ destructuring: "all" }]
106 },
107
108 // https://github.com/eslint/eslint/issues/8187
109 {
110 code: "let { name, ...otherStuff } = obj; otherStuff = {};",
111 options: [{ destructuring: "all" }],
112 parserOptions: { ecmaVersion: 2018 }
113 },
114 {
115 code: "let { name, ...otherStuff } = obj; otherStuff = {};",
116 options: [{ destructuring: "all" }],
117 parser: fixtureParser("babel-eslint5/destructuring-object-spread")
118 },
119
120 // https://github.com/eslint/eslint/issues/8308
121 {
122 code: "let predicate; [typeNode.returnType, predicate] = foo();",
123 parserOptions: { ecmaVersion: 2018 }
124 },
125 {
126 code: "let predicate; [typeNode.returnType, ...predicate] = foo();",
127 parserOptions: { ecmaVersion: 2018 }
128 },
129 {
130
131 // intentionally testing empty slot in destructuring assignment
132 code: "let predicate; [typeNode.returnType,, predicate] = foo();",
133 parserOptions: { ecmaVersion: 2018 }
134 },
135 {
136 code: "let predicate; [typeNode.returnType=5, predicate] = foo();",
137 parserOptions: { ecmaVersion: 2018 }
138 },
139 {
140 code: "let predicate; [[typeNode.returnType=5], predicate] = foo();",
141 parserOptions: { ecmaVersion: 2018 }
142 },
143 {
144 code: "let predicate; [[typeNode.returnType, predicate]] = foo();",
145 parserOptions: { ecmaVersion: 2018 }
146 },
147 {
148 code: "let predicate; [typeNode.returnType, [predicate]] = foo();",
149 parserOptions: { ecmaVersion: 2018 }
150 },
151 {
152 code: "let predicate; [, [typeNode.returnType, predicate]] = foo();",
153 parserOptions: { ecmaVersion: 2018 }
154 },
155 {
156 code: "let predicate; [, {foo:typeNode.returnType, predicate}] = foo();",
157 parserOptions: { ecmaVersion: 2018 }
158 },
159 {
160 code: "let predicate; [, {foo:typeNode.returnType, ...predicate}] = foo();",
161 parserOptions: { ecmaVersion: 2018 }
162 },
163 {
164 code: "let a; const b = {}; ({ a, c: b.c } = func());",
165 parserOptions: { ecmaVersion: 2018 }
166 },
167
168 // ignoreReadBeforeAssign
169 {
170 code: "let x; function foo() { bar(x); } x = 0;",
171 options: [{ ignoreReadBeforeAssign: true }]
172 },
173
174 // https://github.com/eslint/eslint/issues/10520
175 "const x = [1,2]; let y; [,y] = x; y = 0;",
176 "const x = [1,2,3]; let y, z; [y,,z] = x; y = 0; z = 0;",
177
178 {
179 code: "class C { static { let a = 1; a = 2; } }",
180 parserOptions: { ecmaVersion: 2022 }
181 },
182 {
183 code: "class C { static { let a; a = 1; a = 2; } }",
184 parserOptions: { ecmaVersion: 2022 }
185 },
186 {
187 code: "let a; class C { static { a = 1; } }",
188 parserOptions: { ecmaVersion: 2022 }
189 },
190 {
191 code: "class C { static { let a; if (foo) { a = 1; } } }",
192 parserOptions: { ecmaVersion: 2022 }
193 },
194 {
195 code: "class C { static { let a; if (foo) a = 1; } }",
196 parserOptions: { ecmaVersion: 2022 }
197 },
198 {
199 code: "class C { static { let a, b; if (foo) { ({ a, b } = foo); } } }",
200 output: null,
201 parserOptions: { ecmaVersion: 2022 },
202 errors: [
203 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
204 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
205 ]
206 },
207 {
208 code: "class C { static { let a, b; if (foo) ({ a, b } = foo); } }",
209 output: null,
210 parserOptions: { ecmaVersion: 2022 },
211 errors: [
212 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
213 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
214 ]
215 },
216 {
217 code: "class C { static { a; } } let a = 1; ",
218 options: [{ ignoreReadBeforeAssign: true }],
219 parserOptions: { ecmaVersion: 2022 }
220 },
221 {
222 code: "class C { static { () => a; let a = 1; } };",
223 options: [{ ignoreReadBeforeAssign: true }],
224 parserOptions: { ecmaVersion: 2022 }
225 }
226 ],
227 invalid: [
228 {
229 code: "let x = 1; foo(x);",
230 output: "const x = 1; foo(x);",
231 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
232 },
233 {
234 code: "for (let i in [1,2,3]) { foo(i); }",
235 output: "for (const i in [1,2,3]) { foo(i); }",
236 errors: [{ messageId: "useConst", data: { name: "i" }, type: "Identifier" }]
237 },
238 {
239 code: "for (let x of [1,2,3]) { foo(x); }",
240 output: "for (const x of [1,2,3]) { foo(x); }",
241 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
242 },
243 {
244 code: "let [x = -1, y] = [1,2]; y = 0;",
245 output: null,
246 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
247 },
248 {
249 code: "let {a: x = -1, b: y} = {a:1,b:2}; y = 0;",
250 output: null,
251 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
252 },
253 {
254 code: "(function() { let x = 1; foo(x); })();",
255 output: "(function() { const x = 1; foo(x); })();",
256 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
257 },
258 {
259 code: "(function() { for (let i in [1,2,3]) { foo(i); } })();",
260 output: "(function() { for (const i in [1,2,3]) { foo(i); } })();",
261 errors: [{ messageId: "useConst", data: { name: "i" }, type: "Identifier" }]
262 },
263 {
264 code: "(function() { for (let x of [1,2,3]) { foo(x); } })();",
265 output: "(function() { for (const x of [1,2,3]) { foo(x); } })();",
266 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
267 },
268 {
269 code: "(function() { let [x = -1, y] = [1,2]; y = 0; })();",
270 output: null,
271 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
272 },
273 {
274 code: "let f = (function() { let g = x; })(); f = 1;",
275 output: "let f = (function() { const g = x; })(); f = 1;",
276 errors: [{ messageId: "useConst", data: { name: "g" }, type: "Identifier" }]
277 },
278 {
279 code: "(function() { let {a: x = -1, b: y} = {a:1,b:2}; y = 0; })();",
280 output: null,
281 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
282 },
283 {
284 code: "let x = 0; { let x = 1; foo(x); } x = 0;",
285 output: "let x = 0; { const x = 1; foo(x); } x = 0;",
286 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
287 },
288 {
289 code: "for (let i = 0; i < 10; ++i) { let x = 1; foo(x); }",
290 output: "for (let i = 0; i < 10; ++i) { const x = 1; foo(x); }",
291 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
292 },
293 {
294 code: "for (let i in [1,2,3]) { let x = 1; foo(x); }",
295 output: "for (const i in [1,2,3]) { const x = 1; foo(x); }",
296 errors: [
297 { messageId: "useConst", data: { name: "i" }, type: "Identifier" },
298 { messageId: "useConst", data: { name: "x" }, type: "Identifier" }
299 ]
300 },
301 {
302 code: [
303 "var foo = function() {",
304 " for (const b of c) {",
305 " let a;",
306 " a = 1;",
307 " }",
308 "};"
309 ].join("\n"),
310 output: null,
311 errors: [
312 { messageId: "useConst", data: { name: "a" }, type: "Identifier" }
313 ]
314 },
315 {
316 code: [
317 "var foo = function() {",
318 " for (const b of c) {",
319 " let a;",
320 " ({a} = 1);",
321 " }",
322 "};"
323 ].join("\n"),
324 output: null,
325 errors: [
326 { messageId: "useConst", data: { name: "a" }, type: "Identifier" }
327 ]
328 },
329
330 {
331 code: "let x; x = 0;",
332 output: null,
333 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier", column: 8 }]
334 },
335 {
336 code: "switch (a) { case 0: let x; x = 0; }",
337 output: null,
338 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier", column: 29 }]
339 },
340 {
341 code: "(function() { let x; x = 1; })();",
342 output: null,
343 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier", column: 22 }]
344 },
345
346 {
347 code: "let {a = 0, b} = obj; b = 0; foo(a, b);",
348 output: null,
349 options: [{ destructuring: "any" }],
350 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
351 },
352 {
353 code: "let {a: {b, c}} = {a: {b: 1, c: 2}}; b = 3;",
354 output: null,
355 options: [{ destructuring: "any" }],
356 errors: [{ messageId: "useConst", data: { name: "c" }, type: "Identifier" }]
357 },
358 {
359 code: "let {a: {b, c}} = {a: {b: 1, c: 2}}",
360 output: "const {a: {b, c}} = {a: {b: 1, c: 2}}",
361 options: [{ destructuring: "all" }],
362 errors: [
363 { messageId: "useConst", data: { name: "b" }, type: "Identifier" },
364 { messageId: "useConst", data: { name: "c" }, type: "Identifier" }
365 ]
366 },
367 {
368 code: "let a, b; ({a = 0, b} = obj); b = 0; foo(a, b);",
369 output: null,
370 options: [{ destructuring: "any" }],
371 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
372 },
373 {
374 code: "let {a = 0, b} = obj; foo(a, b);",
375 output: "const {a = 0, b} = obj; foo(a, b);",
376 options: [{ destructuring: "all" }],
377 errors: [
378 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
379 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
380 ]
381 },
382 {
383 code: "let [a] = [1]",
384 output: "const [a] = [1]",
385 options: [],
386 errors: [
387 { messageId: "useConst", data: { name: "a" }, type: "Identifier" }
388 ]
389 },
390 {
391 code: "let {a} = obj",
392 output: "const {a} = obj",
393 options: [],
394 errors: [
395 { messageId: "useConst", data: { name: "a" }, type: "Identifier" }
396 ]
397 },
398 {
399 code: "let a, b; ({a = 0, b} = obj); foo(a, b);",
400 output: null,
401 options: [{ destructuring: "all" }],
402 errors: [
403 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
404 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
405 ]
406 },
407 {
408 code: "let {a = 0, b} = obj, c = a; b = a;",
409 output: null,
410 options: [{ destructuring: "any" }],
411 errors: [
412 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
413 { messageId: "useConst", data: { name: "c" }, type: "Identifier" }
414 ]
415 },
416 {
417 code: "let {a = 0, b} = obj, c = a; b = a;",
418 output: null,
419 options: [{ destructuring: "all" }],
420 errors: [{ messageId: "useConst", data: { name: "c" }, type: "Identifier" }]
421 },
422
423 // https://github.com/eslint/eslint/issues/8187
424 {
425 code: "let { name, ...otherStuff } = obj; otherStuff = {};",
426 output: null,
427 options: [{ destructuring: "any" }],
428 parserOptions: { ecmaVersion: 2018 },
429 errors: [{ messageId: "useConst", data: { name: "name" }, type: "Identifier", column: 7 }]
430 },
431 {
432 code: "let { name, ...otherStuff } = obj; otherStuff = {};",
433 output: null,
434 options: [{ destructuring: "any" }],
435 parser: fixtureParser("babel-eslint5/destructuring-object-spread"),
436 errors: [{ messageId: "useConst", data: { name: "name" }, type: "Identifier", column: 7 }]
437 },
438
439 // Warnings are located at declaration if there are reading references before assignments.
440 {
441 code: "let x; function foo() { bar(x); } x = 0;",
442 output: null,
443 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier", column: 5 }]
444 },
445
446 // https://github.com/eslint/eslint/issues/5837
447 {
448 code: "/*eslint use-x:error*/ let x = 1",
449 output: "/*eslint use-x:error*/ const x = 1",
450 parserOptions: { ecmaFeatures: { globalReturn: true } },
451 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
452 },
453 {
454 code: "/*eslint use-x:error*/ { let x = 1 }",
455 output: "/*eslint use-x:error*/ { const x = 1 }",
456 errors: [{ messageId: "useConst", data: { name: "x" }, type: "Identifier" }]
457 },
458 {
459 code: "let { foo, bar } = baz;",
460 output: "const { foo, bar } = baz;",
461 errors: [
462 { messageId: "useConst", data: { name: "foo" }, type: "Identifier" },
463 { messageId: "useConst", data: { name: "bar" }, type: "Identifier" }
464 ]
465 },
466
467 // https://github.com/eslint/eslint/issues/10520
468 {
469 code: "const x = [1,2]; let [,y] = x;",
470 output: "const x = [1,2]; const [,y] = x;",
471 errors: [{ messageId: "useConst", data: { name: "y" }, type: "Identifier" }]
472 },
473 {
474 code: "const x = [1,2,3]; let [y,,z] = x;",
475 output: "const x = [1,2,3]; const [y,,z] = x;",
476 errors: [
477 { messageId: "useConst", data: { name: "y" }, type: "Identifier" },
478 { messageId: "useConst", data: { name: "z" }, type: "Identifier" }
479 ]
480 },
481
482 // https://github.com/eslint/eslint/issues/8308
483 {
484 code: "let predicate; [, {foo:returnType, predicate}] = foo();",
485 output: null,
486 parserOptions: { ecmaVersion: 2018 },
487 errors: [
488 { message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" }
489 ]
490 },
491 {
492 code: "let predicate; [, {foo:returnType, predicate}, ...bar ] = foo();",
493 output: null,
494 parserOptions: { ecmaVersion: 2018 },
495 errors: [
496 { message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" }
497 ]
498 },
499 {
500 code: "let predicate; [, {foo:returnType, ...predicate} ] = foo();",
501 output: null,
502 parserOptions: { ecmaVersion: 2018 },
503 errors: [
504 { message: "'predicate' is never reassigned. Use 'const' instead.", type: "Identifier" }
505 ]
506 },
507 {
508 code: "let x = 'x', y = 'y';",
509 output: "const x = 'x', y = 'y';",
510 errors: [
511 { message: "'x' is never reassigned. Use 'const' instead.", type: "Identifier" },
512 { message: "'y' is never reassigned. Use 'const' instead.", type: "Identifier" }
513 ]
514 },
515 {
516 code: "let x = 'x', y = 'y'; x = 1",
517 output: null,
518 errors: [
519 { message: "'y' is never reassigned. Use 'const' instead.", type: "Identifier" }
520 ]
521 },
522 {
523 code: "let x = 1, y = 'y'; let z = 1;",
524 output: "const x = 1, y = 'y'; const z = 1;",
525 errors: [
526 { message: "'x' is never reassigned. Use 'const' instead.", type: "Identifier" },
527 { message: "'y' is never reassigned. Use 'const' instead.", type: "Identifier" },
528 { message: "'z' is never reassigned. Use 'const' instead.", type: "Identifier" }
529 ]
530 },
531 {
532 code: "let { a, b, c} = obj; let { x, y, z} = anotherObj; x = 2;",
533 output: "const { a, b, c} = obj; let { x, y, z} = anotherObj; x = 2;",
534 errors: [
535 { message: "'a' is never reassigned. Use 'const' instead.", type: "Identifier" },
536 { message: "'b' is never reassigned. Use 'const' instead.", type: "Identifier" },
537 { message: "'c' is never reassigned. Use 'const' instead.", type: "Identifier" },
538 { message: "'y' is never reassigned. Use 'const' instead.", type: "Identifier" },
539 { message: "'z' is never reassigned. Use 'const' instead.", type: "Identifier" }
540 ]
541 },
542 {
543 code: "let x = 'x', y = 'y'; function someFunc() { let a = 1, b = 2; foo(a, b) }",
544 output: "const x = 'x', y = 'y'; function someFunc() { const a = 1, b = 2; foo(a, b) }",
545 errors: [
546 { message: "'x' is never reassigned. Use 'const' instead.", type: "Identifier" },
547 { message: "'y' is never reassigned. Use 'const' instead.", type: "Identifier" },
548 { message: "'a' is never reassigned. Use 'const' instead.", type: "Identifier" },
549 { message: "'b' is never reassigned. Use 'const' instead.", type: "Identifier" }
550 ]
551 },
552
553 // The inner `let` will be auto-fixed in the second pass
554 {
555 code: "let someFunc = () => { let a = 1, b = 2; foo(a, b) }",
556 output: "const someFunc = () => { let a = 1, b = 2; foo(a, b) }",
557 errors: [
558 { message: "'someFunc' is never reassigned. Use 'const' instead.", type: "Identifier" },
559 { message: "'a' is never reassigned. Use 'const' instead.", type: "Identifier" },
560 { message: "'b' is never reassigned. Use 'const' instead.", type: "Identifier" }
561 ]
562 },
563
564 // https://github.com/eslint/eslint/issues/11699
565 {
566 code: "let {a, b} = c, d;",
567 output: null,
568 errors: [
569 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
570 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
571 ]
572 },
573 {
574 code: "let {a, b, c} = {}, e, f;",
575 output: null,
576 errors: [
577 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
578 { messageId: "useConst", data: { name: "b" }, type: "Identifier" },
579 { messageId: "useConst", data: { name: "c" }, type: "Identifier" }
580 ]
581 },
582 {
583 code: [
584 "function a() {",
585 "let foo = 0,",
586 " bar = 1;",
587 "foo = 1;",
588 "}",
589 "function b() {",
590 "let foo = 0,",
591 " bar = 2;",
592 "foo = 2;",
593 "}"
594 ].join("\n"),
595 output: null,
596 errors: [
597 { message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" },
598 { message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" }
599 ]
600 },
601
602 // https://github.com/eslint/eslint/issues/13899
603 {
604 code: "/*eslint no-undef-init:error*/ let foo = undefined;",
605 output: "/*eslint no-undef-init:error*/ const foo = undefined;",
606 errors: 2
607 },
608
609 {
610 code: "let a = 1; class C { static { a; } }",
611 output: "const a = 1; class C { static { a; } }",
612 parserOptions: { ecmaVersion: 2022 },
613 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
614 },
615 {
616
617 // this is a TDZ error with either `let` or `const`, but that isn't a concern of this rule
618 code: "class C { static { a; } } let a = 1;",
619 output: "class C { static { a; } } const a = 1;",
620 parserOptions: { ecmaVersion: 2022 },
621 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
622 },
623 {
624 code: "class C { static { let a = 1; } }",
625 output: "class C { static { const a = 1; } }",
626 parserOptions: { ecmaVersion: 2022 },
627 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
628 },
629 {
630 code: "class C { static { if (foo) { let a = 1; } } }",
631 output: "class C { static { if (foo) { const a = 1; } } }",
632 parserOptions: { ecmaVersion: 2022 },
633 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
634 },
635 {
636 code: "class C { static { let a = 1; if (foo) { a; } } }",
637 output: "class C { static { const a = 1; if (foo) { a; } } }",
638 parserOptions: { ecmaVersion: 2022 },
639 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
640 },
641 {
642 code: "class C { static { if (foo) { let a; a = 1; } } }",
643 output: null,
644 parserOptions: { ecmaVersion: 2022 },
645 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier" }]
646 },
647 {
648 code: "class C { static { let a; a = 1; } }",
649 output: null,
650 parserOptions: { ecmaVersion: 2022 },
651 errors: [{ messageId: "useConst", data: { name: "a" }, type: "Identifier", column: 27 }]
652 },
653 {
654 code: "class C { static { let { a, b } = foo; } }",
655 output: "class C { static { const { a, b } = foo; } }",
656 parserOptions: { ecmaVersion: 2022 },
657 errors: [
658 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
659 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
660 ]
661 },
662 {
663 code: "class C { static { let a, b; ({ a, b } = foo); } }",
664 output: null,
665 parserOptions: { ecmaVersion: 2022 },
666 errors: [
667 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
668 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
669 ]
670 },
671 {
672 code: "class C { static { let a; let b; ({ a, b } = foo); } }",
673 output: null,
674 parserOptions: { ecmaVersion: 2022 },
675 errors: [
676 { messageId: "useConst", data: { name: "a" }, type: "Identifier" },
677 { messageId: "useConst", data: { name: "b" }, type: "Identifier" }
678 ]
679 },
680 {
681 code: "class C { static { let a; a = 0; console.log(a); } }",
682 output: null,
683 parserOptions: { ecmaVersion: 2022 },
684 errors: [
685 { messageId: "useConst", data: { name: "a" }, type: "Identifier" }
686 ]
687 }
688 ]
689 });