]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-const.js
2 * @fileoverview Tests for prefer-const rule.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/prefer-const"),
13 fixtureParser
= require("../../fixtures/fixture-parser"),
14 { RuleTester
} = require("../../../lib/rule-tester");
16 //------------------------------------------------------------------------------
18 //------------------------------------------------------------------------------
20 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 6 } });
22 ruleTester
.defineRule("use-x", context
=> ({
23 VariableDeclaration() {
24 context
.markVariableAsUsed("x");
28 ruleTester
.run("prefer-const", rule
, {
32 "let x; { x = 0; } foo(x);",
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); }",
62 " if (typeof id !== 'undefined') {",
65 " id = setInterval(() => {}, 250);",
69 "/*exported a*/ let a; function init() { a = foo(); }",
70 "/*exported a*/ let a = 1",
71 "let a; if (true) a = 0; foo(a);",
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); }",
90 * The assignment is located in a different scope.
91 * Those are warned by prefer-smaller-scope.
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; }",
100 code
: "let {a, b} = obj; b = 0;",
101 options
: [{ destructuring
: "all" }]
104 code
: "let a, b; ({a, b} = obj); b++;",
105 options
: [{ destructuring
: "all" }]
108 // https://github.com/eslint/eslint/issues/8187
110 code
: "let { name, ...otherStuff } = obj; otherStuff = {};",
111 options
: [{ destructuring
: "all" }],
112 parserOptions
: { ecmaVersion
: 2018 }
115 code
: "let { name, ...otherStuff } = obj; otherStuff = {};",
116 options
: [{ destructuring
: "all" }],
117 parser
: fixtureParser("babel-eslint5/destructuring-object-spread")
120 // https://github.com/eslint/eslint/issues/8308
122 code
: "let predicate; [typeNode.returnType, predicate] = foo();",
123 parserOptions
: { ecmaVersion
: 2018 }
126 code
: "let predicate; [typeNode.returnType, ...predicate] = foo();",
127 parserOptions
: { ecmaVersion
: 2018 }
131 // intentionally testing empty slot in destructuring assignment
132 code
: "let predicate; [typeNode.returnType,, predicate] = foo();",
133 parserOptions
: { ecmaVersion
: 2018 }
136 code
: "let predicate; [typeNode.returnType=5, predicate] = foo();",
137 parserOptions
: { ecmaVersion
: 2018 }
140 code
: "let predicate; [[typeNode.returnType=5], predicate] = foo();",
141 parserOptions
: { ecmaVersion
: 2018 }
144 code
: "let predicate; [[typeNode.returnType, predicate]] = foo();",
145 parserOptions
: { ecmaVersion
: 2018 }
148 code
: "let predicate; [typeNode.returnType, [predicate]] = foo();",
149 parserOptions
: { ecmaVersion
: 2018 }
152 code
: "let predicate; [, [typeNode.returnType, predicate]] = foo();",
153 parserOptions
: { ecmaVersion
: 2018 }
156 code
: "let predicate; [, {foo:typeNode.returnType, predicate}] = foo();",
157 parserOptions
: { ecmaVersion
: 2018 }
160 code
: "let predicate; [, {foo:typeNode.returnType, ...predicate}] = foo();",
161 parserOptions
: { ecmaVersion
: 2018 }
164 code
: "let a; const b = {}; ({ a, c: b.c } = func());",
165 parserOptions
: { ecmaVersion
: 2018 }
168 // ignoreReadBeforeAssign
170 code
: "let x; function foo() { bar(x); } x = 0;",
171 options
: [{ ignoreReadBeforeAssign
: true }]
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;"
180 code
: "let x = 1; foo(x);",
181 output
: "const x = 1; foo(x);",
182 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
185 code
: "for (let i in [1,2,3]) { foo(i); }",
186 output
: "for (const i in [1,2,3]) { foo(i); }",
187 errors
: [{ messageId
: "useConst", data
: { name
: "i" }, type
: "Identifier" }]
190 code
: "for (let x of [1,2,3]) { foo(x); }",
191 output
: "for (const x of [1,2,3]) { foo(x); }",
192 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
195 code
: "let [x = -1, y] = [1,2]; y = 0;",
197 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
200 code
: "let {a: x = -1, b: y} = {a:1,b:2}; y = 0;",
202 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
205 code
: "(function() { let x = 1; foo(x); })();",
206 output
: "(function() { const x = 1; foo(x); })();",
207 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
210 code
: "(function() { for (let i in [1,2,3]) { foo(i); } })();",
211 output
: "(function() { for (const i in [1,2,3]) { foo(i); } })();",
212 errors
: [{ messageId
: "useConst", data
: { name
: "i" }, type
: "Identifier" }]
215 code
: "(function() { for (let x of [1,2,3]) { foo(x); } })();",
216 output
: "(function() { for (const x of [1,2,3]) { foo(x); } })();",
217 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
220 code
: "(function() { let [x = -1, y] = [1,2]; y = 0; })();",
222 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
225 code
: "let f = (function() { let g = x; })(); f = 1;",
226 output
: "let f = (function() { const g = x; })(); f = 1;",
227 errors
: [{ messageId
: "useConst", data
: { name
: "g" }, type
: "Identifier" }]
230 code
: "(function() { let {a: x = -1, b: y} = {a:1,b:2}; y = 0; })();",
232 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
235 code
: "let x = 0; { let x = 1; foo(x); } x = 0;",
236 output
: "let x = 0; { const x = 1; foo(x); } x = 0;",
237 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
240 code
: "for (let i = 0; i < 10; ++i) { let x = 1; foo(x); }",
241 output
: "for (let i = 0; i < 10; ++i) { const x = 1; foo(x); }",
242 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
245 code
: "for (let i in [1,2,3]) { let x = 1; foo(x); }",
246 output
: "for (const i in [1,2,3]) { const x = 1; foo(x); }",
248 { messageId
: "useConst", data
: { name
: "i" }, type
: "Identifier" },
249 { messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }
254 "var foo = function() {",
255 " for (const b of c) {",
263 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }
268 "var foo = function() {",
269 " for (const b of c) {",
277 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }
282 code
: "let x; x = 0;",
284 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier", column
: 8 }]
287 code
: "switch (a) { case 0: let x; x = 0; }",
289 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier", column
: 29 }]
292 code
: "(function() { let x; x = 1; })();",
294 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier", column
: 22 }]
298 code
: "let {a = 0, b} = obj; b = 0; foo(a, b);",
300 options
: [{ destructuring
: "any" }],
301 errors
: [{ messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }]
304 code
: "let {a: {b, c}} = {a: {b: 1, c: 2}}; b = 3;",
306 options
: [{ destructuring
: "any" }],
307 errors
: [{ messageId
: "useConst", data
: { name
: "c" }, type
: "Identifier" }]
310 code
: "let {a: {b, c}} = {a: {b: 1, c: 2}}",
311 output
: "const {a: {b, c}} = {a: {b: 1, c: 2}}",
312 options
: [{ destructuring
: "all" }],
314 { messageId
: "useConst", data
: { name
: "b" }, type
: "Identifier" },
315 { messageId
: "useConst", data
: { name
: "c" }, type
: "Identifier" }
319 code
: "let a, b; ({a = 0, b} = obj); b = 0; foo(a, b);",
321 options
: [{ destructuring
: "any" }],
322 errors
: [{ messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }]
325 code
: "let {a = 0, b} = obj; foo(a, b);",
326 output
: "const {a = 0, b} = obj; foo(a, b);",
327 options
: [{ destructuring
: "all" }],
329 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" },
330 { messageId
: "useConst", data
: { name
: "b" }, type
: "Identifier" }
334 code
: "let [a] = [1]",
335 output
: "const [a] = [1]",
338 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }
342 code
: "let {a} = obj",
343 output
: "const {a} = obj",
346 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" }
350 code
: "let a, b; ({a = 0, b} = obj); foo(a, b);",
352 options
: [{ destructuring
: "all" }],
354 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" },
355 { messageId
: "useConst", data
: { name
: "b" }, type
: "Identifier" }
359 code
: "let {a = 0, b} = obj, c = a; b = a;",
361 options
: [{ destructuring
: "any" }],
363 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" },
364 { messageId
: "useConst", data
: { name
: "c" }, type
: "Identifier" }
368 code
: "let {a = 0, b} = obj, c = a; b = a;",
370 options
: [{ destructuring
: "all" }],
371 errors
: [{ messageId
: "useConst", data
: { name
: "c" }, type
: "Identifier" }]
374 // https://github.com/eslint/eslint/issues/8187
376 code
: "let { name, ...otherStuff } = obj; otherStuff = {};",
378 options
: [{ destructuring
: "any" }],
379 parserOptions
: { ecmaVersion
: 2018 },
380 errors
: [{ messageId
: "useConst", data
: { name
: "name" }, type
: "Identifier", column
: 7 }]
383 code
: "let { name, ...otherStuff } = obj; otherStuff = {};",
385 options
: [{ destructuring
: "any" }],
386 parser
: fixtureParser("babel-eslint5/destructuring-object-spread"),
387 errors
: [{ messageId
: "useConst", data
: { name
: "name" }, type
: "Identifier", column
: 7 }]
390 // Warnings are located at declaration if there are reading references before assignments.
392 code
: "let x; function foo() { bar(x); } x = 0;",
394 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier", column
: 5 }]
397 // https://github.com/eslint/eslint/issues/5837
399 code
: "/*eslint use-x:error*/ let x = 1",
400 output
: "/*eslint use-x:error*/ const x = 1",
401 parserOptions
: { ecmaFeatures
: { globalReturn
: true } },
402 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
405 code
: "/*eslint use-x:error*/ { let x = 1 }",
406 output
: "/*eslint use-x:error*/ { const x = 1 }",
407 errors
: [{ messageId
: "useConst", data
: { name
: "x" }, type
: "Identifier" }]
410 code
: "let { foo, bar } = baz;",
411 output
: "const { foo, bar } = baz;",
413 { messageId
: "useConst", data
: { name
: "foo" }, type
: "Identifier" },
414 { messageId
: "useConst", data
: { name
: "bar" }, type
: "Identifier" }
418 // https://github.com/eslint/eslint/issues/10520
420 code
: "const x = [1,2]; let [,y] = x;",
421 output
: "const x = [1,2]; const [,y] = x;",
422 errors
: [{ messageId
: "useConst", data
: { name
: "y" }, type
: "Identifier" }]
425 code
: "const x = [1,2,3]; let [y,,z] = x;",
426 output
: "const x = [1,2,3]; const [y,,z] = x;",
428 { messageId
: "useConst", data
: { name
: "y" }, type
: "Identifier" },
429 { messageId
: "useConst", data
: { name
: "z" }, type
: "Identifier" }
433 // https://github.com/eslint/eslint/issues/8308
435 code
: "let predicate; [, {foo:returnType, predicate}] = foo();",
437 parserOptions
: { ecmaVersion
: 2018 },
439 { message
: "'predicate' is never reassigned. Use 'const' instead.", type
: "Identifier" }
443 code
: "let predicate; [, {foo:returnType, predicate}, ...bar ] = foo();",
445 parserOptions
: { ecmaVersion
: 2018 },
447 { message
: "'predicate' is never reassigned. Use 'const' instead.", type
: "Identifier" }
451 code
: "let predicate; [, {foo:returnType, ...predicate} ] = foo();",
453 parserOptions
: { ecmaVersion
: 2018 },
455 { message
: "'predicate' is never reassigned. Use 'const' instead.", type
: "Identifier" }
459 code
: "let x = 'x', y = 'y';",
460 output
: "const x = 'x', y = 'y';",
462 { message
: "'x' is never reassigned. Use 'const' instead.", type
: "Identifier" },
463 { message
: "'y' is never reassigned. Use 'const' instead.", type
: "Identifier" }
467 code
: "let x = 'x', y = 'y'; x = 1",
470 { message
: "'y' is never reassigned. Use 'const' instead.", type
: "Identifier" }
474 code
: "let x = 1, y = 'y'; let z = 1;",
475 output
: "const x = 1, y = 'y'; const z = 1;",
477 { message
: "'x' is never reassigned. Use 'const' instead.", type
: "Identifier" },
478 { message
: "'y' is never reassigned. Use 'const' instead.", type
: "Identifier" },
479 { message
: "'z' is never reassigned. Use 'const' instead.", type
: "Identifier" }
483 code
: "let { a, b, c} = obj; let { x, y, z} = anotherObj; x = 2;",
484 output
: "const { a, b, c} = obj; let { x, y, z} = anotherObj; x = 2;",
486 { message
: "'a' is never reassigned. Use 'const' instead.", type
: "Identifier" },
487 { message
: "'b' is never reassigned. Use 'const' instead.", type
: "Identifier" },
488 { message
: "'c' is never reassigned. Use 'const' instead.", type
: "Identifier" },
489 { message
: "'y' is never reassigned. Use 'const' instead.", type
: "Identifier" },
490 { message
: "'z' is never reassigned. Use 'const' instead.", type
: "Identifier" }
494 code
: "let x = 'x', y = 'y'; function someFunc() { let a = 1, b = 2; foo(a, b) }",
495 output
: "const x = 'x', y = 'y'; function someFunc() { const a = 1, b = 2; foo(a, b) }",
497 { message
: "'x' is never reassigned. Use 'const' instead.", type
: "Identifier" },
498 { message
: "'y' is never reassigned. Use 'const' instead.", type
: "Identifier" },
499 { message
: "'a' is never reassigned. Use 'const' instead.", type
: "Identifier" },
500 { message
: "'b' is never reassigned. Use 'const' instead.", type
: "Identifier" }
504 code
: "let someFunc = () => { let a = 1, b = 2; foo(a, b) }",
505 output
: "const someFunc = () => { const a = 1, b = 2; foo(a, b) }",
507 { message
: "'someFunc' is never reassigned. Use 'const' instead.", type
: "Identifier" },
508 { message
: "'a' is never reassigned. Use 'const' instead.", type
: "Identifier" },
509 { message
: "'b' is never reassigned. Use 'const' instead.", type
: "Identifier" }
513 // https://github.com/eslint/eslint/issues/11699
515 code
: "let {a, b} = c, d;",
518 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" },
519 { messageId
: "useConst", data
: { name
: "b" }, type
: "Identifier" }
523 code
: "let {a, b, c} = {}, e, f;",
526 { messageId
: "useConst", data
: { name
: "a" }, type
: "Identifier" },
527 { messageId
: "useConst", data
: { name
: "b" }, type
: "Identifier" },
528 { messageId
: "useConst", data
: { name
: "c" }, type
: "Identifier" }
546 { message
: "'bar' is never reassigned. Use 'const' instead.", type
: "Identifier" },
547 { message
: "'bar' is never reassigned. Use 'const' instead.", type
: "Identifier" }