2 * @fileoverview Tests for no-unused-vars rule.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/no-unused-vars"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.defineRule("use-every-a", context
=> {
24 * Mark a variable as used
29 context
.markVariableAsUsed("a");
32 VariableDeclaration
: useA
,
38 * Returns an expected error for defined-but-not-used variables.
39 * @param {string} varName The name of the variable
40 * @param {string} [additional] The additional text for the message data
41 * @param {string} [type] The node type (defaults to "Identifier")
42 * @returns {Object} An expected error object
44 function definedError(varName
, additional
= "", type
= "Identifier") {
46 messageId
: "unusedVar",
57 * Returns an expected error for assigned-but-not-used variables.
58 * @param {string} varName The name of the variable
59 * @param {string} [additional] The additional text for the message data
60 * @param {string} [type] The node type (defaults to "Identifier")
61 * @returns {Object} An expected error object
63 function assignedError(varName
, additional
= "", type
= "Identifier") {
65 messageId
: "unusedVar",
68 action
: "assigned a value",
75 ruleTester
.run("no-unused-vars", rule
, {
77 "var foo = 5;\n\nlabel: while (true) {\n console.log(foo);\n break label;\n}",
78 "var foo = 5;\n\nwhile (true) {\n console.log(foo);\n break;\n}",
79 { code
: "for (let prop in box) {\n box[prop] = parseInt(box[prop]);\n}", parserOptions
: { ecmaVersion
: 6 } },
80 "var box = {a: 2};\n for (var prop in box) {\n box[prop] = parseInt(box[prop]);\n}",
81 "f({ set foo(a) { return; } });",
82 { code
: "a; var a;", options
: ["all"] },
83 { code
: "var a=10; alert(a);", options
: ["all"] },
84 { code
: "var a=10; (function() { alert(a); })();", options
: ["all"] },
85 { code
: "var a=10; (function() { setTimeout(function() { alert(a); }, 0); })();", options
: ["all"] },
86 { code
: "var a=10; d[a] = 0;", options
: ["all"] },
87 { code
: "(function() { var a=10; return a; })();", options
: ["all"] },
88 { code
: "(function g() {})()", options
: ["all"] },
89 { code
: "function f(a) {alert(a);}; f();", options
: ["all"] },
90 { code
: "var c = 0; function f(a){ var b = a; return b; }; f(c);", options
: ["all"] },
91 { code
: "function a(x, y){ return y; }; a();", options
: ["all"] },
92 { code
: "var arr1 = [1, 2]; var arr2 = [3, 4]; for (var i in arr1) { arr1[i] = 5; } for (var i in arr2) { arr2[i] = 10; }", options
: ["all"] },
93 { code
: "var a=10;", options
: ["local"] },
94 { code
: "var min = \"min\"; Math[min];", options
: ["all"] },
95 { code
: "Foo.bar = function(baz) { return baz; };", options
: ["all"] },
96 "myFunc(function foo() {}.bind(this))",
97 "myFunc(function foo(){}.toString())",
98 "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});}; foo()",
99 "(function() { var doSomething = function doSomething() {}; doSomething() }())",
100 "try {} catch(e) {}",
102 { code
: "var a=10; (function() { alert(a); })();", options
: [{ vars
: "all" }] },
103 { code
: "function g(bar, baz) { return baz; }; g();", options
: [{ vars
: "all" }] },
104 { code
: "function g(bar, baz) { return baz; }; g();", options
: [{ vars
: "all", args
: "after-used" }] },
105 { code
: "function g(bar, baz) { return bar; }; g();", options
: [{ vars
: "all", args
: "none" }] },
106 { code
: "function g(bar, baz) { return 2; }; g();", options
: [{ vars
: "all", args
: "none" }] },
107 { code
: "function g(bar, baz) { return bar + baz; }; g();", options
: [{ vars
: "local", args
: "all" }] },
108 { code
: "var g = function(bar, baz) { return 2; }; g();", options
: [{ vars
: "all", args
: "none" }] },
109 "(function z() { z(); })();",
110 { code
: " ", globals
: { a
: true } },
111 { code
: "var who = \"Paul\";\nmodule.exports = `Hello ${who}!`;", parserOptions
: { ecmaVersion
: 6 } },
112 { code
: "export var foo = 123;", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
113 { code
: "export function foo () {}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
114 { code
: "let toUpper = (partial) => partial.toUpperCase; export {toUpper}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
115 { code
: "export class foo {}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
116 { code
: "class Foo{}; var x = new Foo(); x.foo()", parserOptions
: { ecmaVersion
: 6 } },
117 { code
: "const foo = \"hello!\";function bar(foobar = foo) { foobar.replace(/!$/, \" world!\");}\nbar();", parserOptions
: { ecmaVersion
: 6 } },
118 "function Foo(){}; var x = new Foo(); x.foo()",
119 "function foo() {var foo = 1; return foo}; foo();",
120 "function foo(foo) {return foo}; foo(1);",
121 "function foo() {function foo() {return 1;}; return foo()}; foo();",
122 { code
: "function foo() {var foo = 1; return foo}; foo();", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
123 { code
: "function foo(foo) {return foo}; foo(1);", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
124 { code
: "function foo() {function foo() {return 1;}; return foo()}; foo();", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
125 { code
: "const x = 1; const [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
126 { code
: "const x = 1; const {y = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
127 { code
: "const x = 1; const {z: [y = x]} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
128 { code
: "const x = []; const {z: [y] = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
129 { code
: "const x = 1; let y; [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
130 { code
: "const x = 1; let y; ({z: [y = x]} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
131 { code
: "const x = []; let y; ({z: [y] = x} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
132 { code
: "const x = 1; function foo(y = x) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
133 { code
: "const x = 1; function foo({y = x} = {}) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
134 { code
: "const x = 1; function foo(y = function(z = x) { bar(z); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
135 { code
: "const x = 1; function foo(y = function() { bar(x); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
136 { code
: "var x = 1; var [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
137 { code
: "var x = 1; var {y = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
138 { code
: "var x = 1; var {z: [y = x]} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
139 { code
: "var x = []; var {z: [y] = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
140 { code
: "var x = 1, y; [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
141 { code
: "var x = 1, y; ({z: [y = x]} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
142 { code
: "var x = [], y; ({z: [y] = x} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
143 { code
: "var x = 1; function foo(y = x) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
144 { code
: "var x = 1; function foo({y = x} = {}) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
145 { code
: "var x = 1; function foo(y = function(z = x) { bar(z); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
146 { code
: "var x = 1; function foo(y = function() { bar(x); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
148 // exported variables should work
149 "/*exported toaster*/ var toaster = 'great'",
150 "/*exported toaster, poster*/ var toaster = 1; poster = 0;",
151 { code
: "/*exported x*/ var { x } = y", parserOptions
: { ecmaVersion
: 6 } },
152 { code
: "/*exported x, y*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 } },
154 // Can mark variables as used via context.markVariableAsUsed()
155 "/*eslint use-every-a:1*/ var a;",
156 "/*eslint use-every-a:1*/ !function(a) { return 1; }",
157 "/*eslint use-every-a:1*/ !function() { var a; return 1 }",
160 { code
: "var _a;", options
: [{ vars
: "all", varsIgnorePattern
: "^_" }] },
161 { code
: "var a; function foo() { var _b; } foo();", options
: [{ vars
: "local", varsIgnorePattern
: "^_" }] },
162 { code
: "function foo(_a) { } foo();", options
: [{ args
: "all", argsIgnorePattern
: "^_" }] },
163 { code
: "function foo(a, _b) { return a; } foo();", options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }] },
164 { code
: "var [ firstItemIgnored, secondItem ] = items;\nconsole.log(secondItem);", options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }], parserOptions
: { ecmaVersion
: 6 } },
166 // for-in loops (see #2342)
167 "(function(obj) { var name; for ( name in obj ) return; })({});",
168 "(function(obj) { var name; for ( name in obj ) { return; } })({});",
169 "(function(obj) { for ( var name in obj ) { return true } })({})",
170 "(function(obj) { for ( var name in obj ) return true })({})",
172 { code
: "(function(obj) { let name; for ( name in obj ) return; })({});", parserOptions
: { ecmaVersion
: 6 } },
173 { code
: "(function(obj) { let name; for ( name in obj ) { return; } })({});", parserOptions
: { ecmaVersion
: 6 } },
174 { code
: "(function(obj) { for ( let name in obj ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
175 { code
: "(function(obj) { for ( let name in obj ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
177 { code
: "(function(obj) { for ( const name in obj ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
178 { code
: "(function(obj) { for ( const name in obj ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
180 // Sequence Expressions (See https://github.com/eslint/eslint/issues/14325)
181 { code
: "let x = 0; foo = (0, x++);", parserOptions
: { ecmaVersion
: 6 } },
182 { code
: "let x = 0; foo = (0, x += 1);", parserOptions
: { ecmaVersion
: 6 } },
186 code
: "try{}catch(err){console.error(err);}",
187 options
: [{ caughtErrors
: "all" }]
190 code
: "try{}catch(err){}",
191 options
: [{ caughtErrors
: "none" }]
194 code
: "try{}catch(ignoreErr){}",
195 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }]
198 // caughtErrors with other combinations
200 code
: "try{}catch(err){}",
201 options
: [{ vars
: "all", args
: "all" }]
204 // Using object rest for variable omission
206 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
207 options
: [{ ignoreRestSiblings
: true }],
208 parserOptions
: { ecmaVersion
: 2018 }
211 // https://github.com/eslint/eslint/issues/6348
212 "var a = 0, b; b = a = a + 1; foo(b);",
213 "var a = 0, b; b = a += a + 1; foo(b);",
214 "var a = 0, b; b = a++; foo(b);",
215 "function foo(a) { var b = a = a + 1; bar(b) } foo();",
216 "function foo(a) { var b = a += a + 1; bar(b) } foo();",
217 "function foo(a) { var b = a++; bar(b) } foo();",
219 // https://github.com/eslint/eslint/issues/6576
221 "var unregisterFooWatcher;",
223 "unregisterFooWatcher = $scope.$watch( \"foo\", function() {",
224 " // ...some code..",
225 " unregisterFooWatcher();",
230 "ref = setInterval(",
232 " clearInterval(ref);",
238 " _timer = setTimeout(function () {}, _timer ? 100 : 0);",
242 "function foo(cb) { cb = function() { function something(a) { cb(1 + a); } register(something); }(); } foo();",
243 { code
: "function* foo(cb) { cb = yield function(a) { cb(1 + a); }; } foo();", parserOptions
: { ecmaVersion
: 6 } },
244 { code
: "function foo(cb) { cb = tag`hello${function(a) { cb(1 + a); }}`; } foo();", parserOptions
: { ecmaVersion
: 6 } },
245 "function foo(cb) { var b; cb = b = function(a) { cb(1 + a); }; b(); } foo();",
247 // https://github.com/eslint/eslint/issues/6646
249 "function someFunction() {",
251 " for (i = 0; i < 2; i++) {",
252 " a = myFunction(a);",
258 // https://github.com/eslint/eslint/issues/7124
260 code
: "(function(a, b, {c, d}) { d })",
261 options
: [{ argsIgnorePattern
: "c" }],
262 parserOptions
: { ecmaVersion
: 6 }
265 code
: "(function(a, b, {c, d}) { c })",
266 options
: [{ argsIgnorePattern
: "d" }],
267 parserOptions
: { ecmaVersion
: 6 }
270 // https://github.com/eslint/eslint/issues/7250
272 code
: "(function(a, b, c) { c })",
273 options
: [{ argsIgnorePattern
: "c" }]
276 code
: "(function(a, b, {c, d}) { c })",
277 options
: [{ argsIgnorePattern
: "[cd]" }],
278 parserOptions
: { ecmaVersion
: 6 }
281 // https://github.com/eslint/eslint/issues/7351
283 code
: "(class { set foo(UNUSED) {} })",
284 parserOptions
: { ecmaVersion
: 6 }
287 code
: "class Foo { set bar(UNUSED) {} } console.log(Foo)",
288 parserOptions
: { ecmaVersion
: 6 }
291 // https://github.com/eslint/eslint/issues/8119
293 code
: "(({a, ...rest}) => rest)",
294 options
: [{ args
: "all", ignoreRestSiblings
: true }],
295 parserOptions
: { ecmaVersion
: 2018 }
298 // https://github.com/eslint/eslint/issues/14163
300 code
: "let foo, rest;\n({ foo, ...rest } = something);\nconsole.log(rest);",
301 options
: [{ ignoreRestSiblings
: true }],
302 parserOptions
: { ecmaVersion
: 2020 }
305 // https://github.com/eslint/eslint/issues/10952
306 "/*eslint use-every-a:1*/ !function(b, a) { return 1 }",
308 // https://github.com/eslint/eslint/issues/10982
309 "var a = function () { a(); }; a();",
310 "var a = function(){ return function () { a(); } }; a();",
312 code
: "const a = () => { a(); }; a();",
313 parserOptions
: { ecmaVersion
: 2015 }
316 code
: "const a = () => () => { a(); }; a();",
317 parserOptions
: { ecmaVersion
: 2015 }
320 // export * as ns from "source"
322 code
: 'export * as ns from "source"',
323 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
329 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
333 { code
: "function foox() { return foox(); }", errors
: [definedError("foox")] },
334 { code
: "(function() { function foox() { if (true) { return foox(); } } }())", errors
: [definedError("foox")] },
335 { code
: "var a=10", errors
: [assignedError("a")] },
336 { code
: "function f() { var a = 1; return function(){ f(a *= 2); }; }", errors
: [definedError("f")] },
337 { code
: "function f() { var a = 1; return function(){ f(++a); }; }", errors
: [definedError("f")] },
338 { code
: "/*global a */", errors
: [definedError("a", "", "Program")] },
339 { code
: "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});};", errors
: [definedError("foo")] },
340 { code
: "var a=10;", options
: ["all"], errors
: [assignedError("a")] },
341 { code
: "var a=10; a=20;", options
: ["all"], errors
: [assignedError("a")] },
342 { code
: "var a=10; (function() { var a = 1; alert(a); })();", options
: ["all"], errors
: [assignedError("a")] },
343 { code
: "var a=10, b=0, c=null; alert(a+b)", options
: ["all"], errors
: [assignedError("c")] },
344 { code
: "var a=10, b=0, c=null; setTimeout(function() { var b=2; alert(a+b+c); }, 0);", options
: ["all"], errors
: [assignedError("b")] },
345 { code
: "var a=10, b=0, c=null; setTimeout(function() { var b=2; var c=2; alert(a+b+c); }, 0);", options
: ["all"], errors
: [assignedError("b"), assignedError("c")] },
346 { code
: "function f(){var a=[];return a.map(function(){});}", options
: ["all"], errors
: [definedError("f")] },
347 { code
: "function f(){var a=[];return a.map(function g(){});}", options
: ["all"], errors
: [definedError("f")] },
349 code
: "function foo() {function foo(x) {\nreturn x; }; return function() {return foo; }; }",
351 messageId
: "unusedVar",
352 data
: { varName
: "foo", action
: "defined", additional
: "" },
357 { code
: "function f(){var x;function a(){x=42;}function b(){alert(x);}}", options
: ["all"], errors
: 3 },
358 { code
: "function f(a) {}; f();", options
: ["all"], errors
: [definedError("a")] },
359 { code
: "function a(x, y, z){ return y; }; a();", options
: ["all"], errors
: [definedError("z")] },
360 { code
: "var min = Math.min", options
: ["all"], errors
: [assignedError("min")] },
361 { code
: "var min = {min: 1}", options
: ["all"], errors
: [assignedError("min")] },
362 { code
: "Foo.bar = function(baz) { return 1; };", options
: ["all"], errors
: [definedError("baz")] },
363 { code
: "var min = {min: 1}", options
: [{ vars
: "all" }], errors
: [assignedError("min")] },
364 { code
: "function gg(baz, bar) { return baz; }; gg();", options
: [{ vars
: "all" }], errors
: [definedError("bar")] },
365 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "after-used" }], errors
: [definedError("bar")] },
366 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), definedError("bar")] },
367 { code
: "(function z(foo) { var bar = 33; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), assignedError("bar")] },
368 { code
: "(function z(foo) { z(); })();", options
: [{}], errors
: [definedError("foo")] },
369 { code
: "function f() { var a = 1; return function(){ f(a = 2); }; }", options
: [{}], errors
: [definedError("f"), assignedError("a")] },
370 { code
: "import x from \"y\";", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("x")] },
371 { code
: "export function fn2({ x, y }) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
372 { code
: "export function fn2( x, y ) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
375 { code
: "/*exported max*/ var max = 1, min = {min: 1}", errors
: [assignedError("min")] },
376 { code
: "/*exported x*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 }, errors
: [assignedError("y")] },
380 code
: "var _a; var b;",
381 options
: [{ vars
: "all", varsIgnorePattern
: "^_" }],
385 messageId
: "unusedVar",
389 additional
: ". Allowed unused vars must match /^_/u"
394 code
: "var a; function foo() { var _b; var c_; } foo();",
395 options
: [{ vars
: "local", varsIgnorePattern
: "^_" }],
399 messageId
: "unusedVar",
403 additional
: ". Allowed unused vars must match /^_/u"
408 code
: "function foo(a, _b) { } foo();",
409 options
: [{ args
: "all", argsIgnorePattern
: "^_" }],
413 messageId
: "unusedVar",
417 additional
: ". Allowed unused args must match /^_/u"
422 code
: "function foo(a, _b, c) { return a; } foo();",
423 options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }],
427 messageId
: "unusedVar",
431 additional
: ". Allowed unused args must match /^_/u"
436 code
: "function foo(_a) { } foo();",
437 options
: [{ args
: "all", argsIgnorePattern
: "[iI]gnored" }],
441 messageId
: "unusedVar",
445 additional
: ". Allowed unused args must match /[iI]gnored/u"
450 code
: "var [ firstItemIgnored, secondItem ] = items;",
451 options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }],
452 parserOptions
: { ecmaVersion
: 6 },
456 messageId
: "unusedVar",
458 varName
: "secondItem",
459 action
: "assigned a value",
460 additional
: ". Allowed unused vars must match /[iI]gnored/u"
465 // for-in loops (see #2342)
467 code
: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});",
471 messageId
: "unusedVar",
474 action
: "assigned a value",
480 code
: "(function(obj) { var name; for ( name in obj ) { } })({});",
484 messageId
: "unusedVar",
487 action
: "assigned a value",
493 code
: "(function(obj) { for ( var name in obj ) { } })({});",
497 messageId
: "unusedVar",
500 action
: "assigned a value",
506 // https://github.com/eslint/eslint/issues/3617
508 code
: "\n/* global foobar, foo, bar */\nfoobar;",
515 messageId
: "unusedVar",
527 messageId
: "unusedVar",
537 code
: "\n/* global foobar,\n foo,\n bar\n */\nfoobar;",
544 messageId
: "unusedVar",
556 messageId
: "unusedVar",
566 // Rest property sibling without ignoreRestSiblings
568 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
569 parserOptions
: { ecmaVersion
: 2018 },
574 messageId
: "unusedVar",
577 action
: "assigned a value",
584 // Unused rest property with ignoreRestSiblings
586 code
: "const data = { type: 'coords', x: 2, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
587 options
: [{ ignoreRestSiblings
: true }],
588 parserOptions
: { ecmaVersion
: 2018 },
593 messageId
: "unusedVar",
596 action
: "assigned a value",
603 code
: "let type, coords;\n({ type, ...coords } = data);\n console.log(type)",
604 options
: [{ ignoreRestSiblings
: true }],
605 parserOptions
: { ecmaVersion
: 2018 },
610 messageId
: "unusedVar",
613 action
: "assigned a value",
620 // Unused rest property without ignoreRestSiblings
622 code
: "const data = { type: 'coords', x: 3, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
623 parserOptions
: { ecmaVersion
: 2018 },
628 messageId
: "unusedVar",
631 action
: "assigned a value",
638 // Nested array destructuring with rest property
640 code
: "const data = { vars: ['x','y'], x: 1, y: 2 };\nconst { vars: [x], ...coords } = data;\n console.log(coords)",
641 parserOptions
: { ecmaVersion
: 2018 },
646 messageId
: "unusedVar",
649 action
: "assigned a value",
656 // Nested object destructuring with rest property
658 code
: "const data = { defaults: { x: 0 }, x: 1, y: 2 };\nconst { defaults: { x }, ...coords } = data;\n console.log(coords)",
659 parserOptions
: { ecmaVersion
: 2018 },
664 messageId
: "unusedVar",
667 action
: "assigned a value",
674 // https://github.com/eslint/eslint/issues/8119
676 code
: "(({a, ...rest}) => {})",
677 options
: [{ args
: "all", ignoreRestSiblings
: true }],
678 parserOptions
: { ecmaVersion
: 2018 },
679 errors
: [definedError("rest")]
682 // https://github.com/eslint/eslint/issues/3714
684 code
: "/* global a$fooz,$foo */\na$fooz;",
691 messageId
: "unusedVar",
701 code
: "/* globals a$fooz, $ */\na$fooz;",
708 messageId
: "unusedVar",
718 code
: "/*globals $foo*/",
725 messageId
: "unusedVar",
735 code
: "/* global global*/",
742 messageId
: "unusedVar",
752 code
: "/*global foo:true*/",
759 messageId
: "unusedVar",
771 code
: "/*global 変数, 数*/\n変数;",
778 messageId
: "unusedVar",
790 code
: "/*global 𠮷𩸽, 𠮷*/\n\\u{20BB7}\\u{29E3D};",
798 messageId
: "unusedVar",
808 // https://github.com/eslint/eslint/issues/4047
810 code
: "export default function(a) {}",
811 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
812 errors
: [definedError("a")]
815 code
: "export default function(a, b) { console.log(a); }",
816 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
817 errors
: [definedError("b")]
820 code
: "export default (function(a) {});",
821 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
822 errors
: [definedError("a")]
825 code
: "export default (function(a, b) { console.log(a); });",
826 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
827 errors
: [definedError("b")]
830 code
: "export default (a) => {};",
831 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
832 errors
: [definedError("a")]
835 code
: "export default (a, b) => { console.log(a); };",
836 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
837 errors
: [definedError("b")]
842 code
: "try{}catch(err){};",
843 options
: [{ caughtErrors
: "all" }],
844 errors
: [definedError("err")]
847 code
: "try{}catch(err){};",
848 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
849 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
852 // multiple try catch with one success
854 code
: "try{}catch(ignoreErr){}try{}catch(err){};",
855 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
856 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
859 // multiple try catch both fail
861 code
: "try{}catch(error){}try{}catch(err){};",
862 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
864 definedError("error", ". Allowed unused args must match /^ignore/u"),
865 definedError("err", ". Allowed unused args must match /^ignore/u")
869 // caughtErrors with other configs
871 code
: "try{}catch(err){};",
872 options
: [{ vars
: "all", args
: "all", caughtErrors
: "all" }],
873 errors
: [definedError("err")]
876 // no conflict in ignore patterns
878 code
: "try{}catch(err){};",
884 argsIgnorePattern
: "^er"
887 errors
: [definedError("err")]
890 // Ignore reads for modifications to itself: https://github.com/eslint/eslint/issues/6348
891 { code
: "var a = 0; a = a + 1;", errors
: [assignedError("a")] },
892 { code
: "var a = 0; a = a + a;", errors
: [assignedError("a")] },
893 { code
: "var a = 0; a += a + 1;", errors
: [assignedError("a")] },
894 { code
: "var a = 0; a++;", errors
: [assignedError("a")] },
895 { code
: "function foo(a) { a = a + 1 } foo();", errors
: [assignedError("a")] },
896 { code
: "function foo(a) { a += a + 1 } foo();", errors
: [assignedError("a")] },
897 { code
: "function foo(a) { a++ } foo();", errors
: [assignedError("a")] },
898 { code
: "var a = 3; a = a * 5 + 6;", errors
: [assignedError("a")] },
899 { code
: "var a = 2, b = 4; a = a * 2 + b;", errors
: [assignedError("a")] },
901 // https://github.com/eslint/eslint/issues/6576 (For coverage)
903 code
: "function foo(cb) { cb = function(a) { cb(1 + a); }; bar(not_cb); } foo();",
904 errors
: [assignedError("cb")]
907 code
: "function foo(cb) { cb = function(a) { return cb(1 + a); }(); } foo();",
908 errors
: [assignedError("cb")]
911 code
: "function foo(cb) { cb = (function(a) { cb(1 + a); }, cb); } foo();",
912 errors
: [assignedError("cb")]
915 code
: "function foo(cb) { cb = (0, function(a) { cb(1 + a); }); } foo();",
916 errors
: [assignedError("cb")]
919 // https://github.com/eslint/eslint/issues/6646
923 " function foo(b) {",
929 errors
: [assignedError("b")]
932 // https://github.com/eslint/eslint/issues/7124
934 code
: "(function(a, b, c) {})",
935 options
: [{ argsIgnorePattern
: "c" }],
937 definedError("a", ". Allowed unused args must match /c/u"),
938 definedError("b", ". Allowed unused args must match /c/u")
942 code
: "(function(a, b, {c, d}) {})",
943 options
: [{ argsIgnorePattern
: "[cd]" }],
944 parserOptions
: { ecmaVersion
: 6 },
946 definedError("a", ". Allowed unused args must match /[cd]/u"),
947 definedError("b", ". Allowed unused args must match /[cd]/u")
951 code
: "(function(a, b, {c, d}) {})",
952 options
: [{ argsIgnorePattern
: "c" }],
953 parserOptions
: { ecmaVersion
: 6 },
955 definedError("a", ". Allowed unused args must match /c/u"),
956 definedError("b", ". Allowed unused args must match /c/u"),
957 definedError("d", ". Allowed unused args must match /c/u")
961 code
: "(function(a, b, {c, d}) {})",
962 options
: [{ argsIgnorePattern
: "d" }],
963 parserOptions
: { ecmaVersion
: 6 },
965 definedError("a", ". Allowed unused args must match /d/u"),
966 definedError("b", ". Allowed unused args must match /d/u"),
967 definedError("c", ". Allowed unused args must match /d/u")
971 code
: "/*global\rfoo*/",
977 messageId
: "unusedVar",
986 // https://github.com/eslint/eslint/issues/8442
988 code
: "(function ({ a }, b ) { return b; })();",
989 parserOptions
: { ecmaVersion
: 2015 },
995 code
: "(function ({ a }, { b, c } ) { return b; })();",
996 parserOptions
: { ecmaVersion
: 2015 },
1003 // https://github.com/eslint/eslint/issues/14325
1007 parserOptions
: { ecmaVersion
: 2015 },
1008 errors
: [{ ...assignedError("x"), line
: 2, column
: 18 }]
1014 parserOptions
: { ecmaVersion
: 2015 },
1015 errors
: [{ ...assignedError("x"), line
: 3, column
: 13 }]
1018 code
: "let x = 0; x++, 0;",
1019 parserOptions
: { ecmaVersion
: 2015 },
1020 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1023 code
: "let x = 0; 0, x++;",
1024 parserOptions
: { ecmaVersion
: 2015 },
1025 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1028 code
: "let x = 0; 0, (1, x++);",
1029 parserOptions
: { ecmaVersion
: 2015 },
1030 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1033 code
: "let x = 0; foo = (x++, 0);",
1034 parserOptions
: { ecmaVersion
: 2015 },
1035 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1038 code
: "let x = 0; foo = ((0, x++), 0);",
1039 parserOptions
: { ecmaVersion
: 2015 },
1040 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1043 code
: "let x = 0; x += 1, 0;",
1044 parserOptions
: { ecmaVersion
: 2015 },
1045 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1048 code
: "let x = 0; 0, x += 1;",
1049 parserOptions
: { ecmaVersion
: 2015 },
1050 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1053 code
: "let x = 0; 0, (1, x += 1);",
1054 parserOptions
: { ecmaVersion
: 2015 },
1055 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1058 code
: "let x = 0; foo = (x += 1, 0);",
1059 parserOptions
: { ecmaVersion
: 2015 },
1060 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1063 code
: "let x = 0; foo = ((0, x += 1), 0);",
1064 parserOptions
: { ecmaVersion
: 2015 },
1065 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1068 code
: "(function ({ a, b }, { c } ) { return b; })();",
1069 parserOptions
: { ecmaVersion
: 2015 },
1076 code
: "(function ([ a ], b ) { return b; })();",
1077 parserOptions
: { ecmaVersion
: 2015 },
1083 code
: "(function ([ a ], [ b, c ] ) { return b; })();",
1084 parserOptions
: { ecmaVersion
: 2015 },
1091 code
: "(function ([ a, b ], [ c ] ) { return b; })();",
1092 parserOptions
: { ecmaVersion
: 2015 },
1099 // https://github.com/eslint/eslint/issues/9774
1101 code
: "(function(_a) {})();",
1102 options
: [{ args
: "all", varsIgnorePattern
: "^_" }],
1103 errors
: [definedError("_a")]
1106 code
: "(function(_a) {})();",
1107 options
: [{ args
: "all", caughtErrorsIgnorePattern
: "^_" }],
1108 errors
: [definedError("_a")]
1111 // https://github.com/eslint/eslint/issues/10982
1113 code
: "var a = function() { a(); };",
1114 errors
: [assignedError("a")]
1117 code
: "var a = function(){ return function() { a(); } };",
1118 errors
: [assignedError("a")]
1121 code
: "const a = () => { a(); };",
1122 parserOptions
: { ecmaVersion
: 2015 },
1123 errors
: [assignedError("a")]
1126 code
: "const a = () => () => { a(); };",
1127 parserOptions
: { ecmaVersion
: 2015 },
1128 errors
: [assignedError("a")]
1131 code
: `let myArray = [1,2,3,4].filter((x) => x == 0);
1132 myArray = myArray.filter((x) => x == 1);`,
1133 parserOptions
: { ecmaVersion
: 2015 },
1134 errors
: [{ ...assignedError("myArray"), line
: 2, column
: 5 }]
1137 code
: "const a = 1; a += 1;",
1138 parserOptions
: { ecmaVersion
: 2015 },
1139 errors
: [{ ...assignedError("a"), line
: 1, column
: 14 }]
1142 code
: "var a = function() { a(); };",
1143 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1146 code
: "var a = function(){ return function() { a(); } };",
1147 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1150 code
: "const a = () => { a(); };",
1151 parserOptions
: { ecmaVersion
: 2015 },
1152 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1155 code
: "const a = () => () => { a(); };",
1156 parserOptions
: { ecmaVersion
: 2015 },
1157 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1160 // https://github.com/eslint/eslint/issues/14324
1162 code
: "let x = [];\nx = x.concat(x);",
1163 parserOptions
: { ecmaVersion
: 2015 },
1164 errors
: [{ ...assignedError("x"), line
: 2, column
: 1 }]
1176 parserOptions
: { ecmaVersion
: 2020 },
1177 errors
: [{ ...assignedError("a"), line
: 2, column
: 13 }, { ...definedError("foo"), line
: 3, column
: 22 }]
1186 parserOptions
: { ecmaVersion
: 2020 },
1187 errors
: [{ ...assignedError("foo"), line
: 3, column
: 13 }]
1190 code
: `function foo(n) {
1191 if (n < 2) return 1;
1192 return n * foo(n - 1);
1194 parserOptions
: { ecmaVersion
: 2020 },
1195 errors
: [{ ...definedError("foo"), line
: 1, column
: 10 }]
1208 parserOptions
: { ecmaVersion
: 2020 },
1209 errors
: [{ ...assignedError("c"), line
: 10, column
: 1 }]