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", {
24 const sourceCode
= context
.sourceCode
;
27 * Mark a variable as used
28 * @param {ASTNode} node The node representing the scope to search
33 sourceCode
.markVariableAsUsed("a", node
);
36 VariableDeclaration
: useA
,
43 * Returns an expected error for defined-but-not-used variables.
44 * @param {string} varName The name of the variable
45 * @param {string} [additional] The additional text for the message data
46 * @param {string} [type] The node type (defaults to "Identifier")
47 * @returns {Object} An expected error object
49 function definedError(varName
, additional
= "", type
= "Identifier") {
51 messageId
: "unusedVar",
62 * Returns an expected error for assigned-but-not-used variables.
63 * @param {string} varName The name of the variable
64 * @param {string} [additional] The additional text for the message data
65 * @param {string} [type] The node type (defaults to "Identifier")
66 * @returns {Object} An expected error object
68 function assignedError(varName
, additional
= "", type
= "Identifier") {
70 messageId
: "unusedVar",
73 action
: "assigned a value",
80 ruleTester
.run("no-unused-vars", rule
, {
82 "var foo = 5;\n\nlabel: while (true) {\n console.log(foo);\n break label;\n}",
83 "var foo = 5;\n\nwhile (true) {\n console.log(foo);\n break;\n}",
84 { code
: "for (let prop in box) {\n box[prop] = parseInt(box[prop]);\n}", parserOptions
: { ecmaVersion
: 6 } },
85 "var box = {a: 2};\n for (var prop in box) {\n box[prop] = parseInt(box[prop]);\n}",
86 "f({ set foo(a) { return; } });",
87 { code
: "a; var a;", options
: ["all"] },
88 { code
: "var a=10; alert(a);", options
: ["all"] },
89 { code
: "var a=10; (function() { alert(a); })();", options
: ["all"] },
90 { code
: "var a=10; (function() { setTimeout(function() { alert(a); }, 0); })();", options
: ["all"] },
91 { code
: "var a=10; d[a] = 0;", options
: ["all"] },
92 { code
: "(function() { var a=10; return a; })();", options
: ["all"] },
93 { code
: "(function g() {})()", options
: ["all"] },
94 { code
: "function f(a) {alert(a);}; f();", options
: ["all"] },
95 { code
: "var c = 0; function f(a){ var b = a; return b; }; f(c);", options
: ["all"] },
96 { code
: "function a(x, y){ return y; }; a();", options
: ["all"] },
97 { 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"] },
98 { code
: "var a=10;", options
: ["local"] },
99 { code
: "var min = \"min\"; Math[min];", options
: ["all"] },
100 { code
: "Foo.bar = function(baz) { return baz; };", options
: ["all"] },
101 "myFunc(function foo() {}.bind(this))",
102 "myFunc(function foo(){}.toString())",
103 "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});}; foo()",
104 "(function() { var doSomething = function doSomething() {}; doSomething() }())",
105 "try {} catch(e) {}",
107 { code
: "var a=10; (function() { alert(a); })();", options
: [{ vars
: "all" }] },
108 { code
: "function g(bar, baz) { return baz; }; g();", options
: [{ vars
: "all" }] },
109 { code
: "function g(bar, baz) { return baz; }; g();", options
: [{ vars
: "all", args
: "after-used" }] },
110 { code
: "function g(bar, baz) { return bar; }; g();", options
: [{ vars
: "all", args
: "none" }] },
111 { code
: "function g(bar, baz) { return 2; }; g();", options
: [{ vars
: "all", args
: "none" }] },
112 { code
: "function g(bar, baz) { return bar + baz; }; g();", options
: [{ vars
: "local", args
: "all" }] },
113 { code
: "var g = function(bar, baz) { return 2; }; g();", options
: [{ vars
: "all", args
: "none" }] },
114 "(function z() { z(); })();",
115 { code
: " ", globals
: { a
: true } },
116 { code
: "var who = \"Paul\";\nmodule.exports = `Hello ${who}!`;", parserOptions
: { ecmaVersion
: 6 } },
117 { code
: "export var foo = 123;", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
118 { code
: "export function foo () {}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
119 { code
: "let toUpper = (partial) => partial.toUpperCase; export {toUpper}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
120 { code
: "export class foo {}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
121 { code
: "class Foo{}; var x = new Foo(); x.foo()", parserOptions
: { ecmaVersion
: 6 } },
122 { code
: "const foo = \"hello!\";function bar(foobar = foo) { foobar.replace(/!$/, \" world!\");}\nbar();", parserOptions
: { ecmaVersion
: 6 } },
123 "function Foo(){}; var x = new Foo(); x.foo()",
124 "function foo() {var foo = 1; return foo}; foo();",
125 "function foo(foo) {return foo}; foo(1);",
126 "function foo() {function foo() {return 1;}; return foo()}; foo();",
127 { code
: "function foo() {var foo = 1; return foo}; foo();", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
128 { code
: "function foo(foo) {return foo}; foo(1);", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
129 { code
: "function foo() {function foo() {return 1;}; return foo()}; foo();", parserOptions
: { parserOptions
: { ecmaVersion
: 6 } } },
130 { code
: "const x = 1; const [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
131 { code
: "const x = 1; const {y = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
132 { code
: "const x = 1; const {z: [y = x]} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
133 { code
: "const x = []; const {z: [y] = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
134 { code
: "const x = 1; let y; [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
135 { code
: "const x = 1; let y; ({z: [y = x]} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
136 { code
: "const x = []; let y; ({z: [y] = x} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
137 { code
: "const x = 1; function foo(y = x) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
138 { code
: "const x = 1; function foo({y = x} = {}) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
139 { code
: "const x = 1; function foo(y = function(z = x) { bar(z); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
140 { code
: "const x = 1; function foo(y = function() { bar(x); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
141 { code
: "var x = 1; var [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
142 { code
: "var x = 1; var {y = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
143 { code
: "var x = 1; var {z: [y = x]} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
144 { code
: "var x = []; var {z: [y] = x} = {}; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
145 { code
: "var x = 1, y; [y = x] = []; foo(y);", parserOptions
: { ecmaVersion
: 6 } },
146 { code
: "var x = 1, y; ({z: [y = x]} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
147 { code
: "var x = [], y; ({z: [y] = x} = {}); foo(y);", parserOptions
: { ecmaVersion
: 6 } },
148 { code
: "var x = 1; function foo(y = x) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
149 { code
: "var x = 1; function foo({y = x} = {}) { bar(y); } foo();", parserOptions
: { ecmaVersion
: 6 } },
150 { code
: "var x = 1; function foo(y = function(z = x) { bar(z); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
151 { code
: "var x = 1; function foo(y = function() { bar(x); }) { y(); } foo();", parserOptions
: { ecmaVersion
: 6 } },
153 // exported variables should work
154 "/*exported toaster*/ var toaster = 'great'",
155 "/*exported toaster, poster*/ var toaster = 1; poster = 0;",
156 { code
: "/*exported x*/ var { x } = y", parserOptions
: { ecmaVersion
: 6 } },
157 { code
: "/*exported x, y*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 } },
159 // Can mark variables as used via context.markVariableAsUsed()
160 "/*eslint use-every-a:1*/ var a;",
161 "/*eslint use-every-a:1*/ !function(a) { return 1; }",
162 "/*eslint use-every-a:1*/ !function() { var a; return 1 }",
165 { code
: "var _a;", options
: [{ vars
: "all", varsIgnorePattern
: "^_" }] },
166 { code
: "var a; function foo() { var _b; } foo();", options
: [{ vars
: "local", varsIgnorePattern
: "^_" }] },
167 { code
: "function foo(_a) { } foo();", options
: [{ args
: "all", argsIgnorePattern
: "^_" }] },
168 { code
: "function foo(a, _b) { return a; } foo();", options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }] },
169 { code
: "var [ firstItemIgnored, secondItem ] = items;\nconsole.log(secondItem);", options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }], parserOptions
: { ecmaVersion
: 6 } },
171 code
: "const [ a, _b, c ] = items;\nconsole.log(a+c);",
172 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
173 parserOptions
: { ecmaVersion
: 6 }
176 code
: "const [ [a, _b, c] ] = items;\nconsole.log(a+c);",
177 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
178 parserOptions
: { ecmaVersion
: 6 }
181 code
: "const { x: [_a, foo] } = bar;\nconsole.log(foo);",
182 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
183 parserOptions
: { ecmaVersion
: 6 }
186 code
: "function baz([_b, foo]) { foo; };\nbaz()",
187 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
188 parserOptions
: { ecmaVersion
: 6 }
191 code
: "function baz({x: [_b, foo]}) {foo};\nbaz()",
192 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
193 parserOptions
: { ecmaVersion
: 6 }
196 code
: "function baz([{x: [_b, foo]}]) {foo};\nbaz()",
197 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
198 parserOptions
: { ecmaVersion
: 6 }
203 foo.forEach(item => {
208 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
209 parserOptions
: { ecmaVersion
: 6 }
225 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
226 parserOptions
: { ecmaVersion
: 2018 }
242 options
: [{ destructuredArrayIgnorePattern
: "^_", ignoreRestSiblings
: true }],
243 parserOptions
: { ecmaVersion
: 2018 }
246 // for-in loops (see #2342)
247 "(function(obj) { var name; for ( name in obj ) return; })({});",
248 "(function(obj) { var name; for ( name in obj ) { return; } })({});",
249 "(function(obj) { for ( var name in obj ) { return true } })({})",
250 "(function(obj) { for ( var name in obj ) return true })({})",
252 { code
: "(function(obj) { let name; for ( name in obj ) return; })({});", parserOptions
: { ecmaVersion
: 6 } },
253 { code
: "(function(obj) { let name; for ( name in obj ) { return; } })({});", parserOptions
: { ecmaVersion
: 6 } },
254 { code
: "(function(obj) { for ( let name in obj ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
255 { code
: "(function(obj) { for ( let name in obj ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
257 { code
: "(function(obj) { for ( const name in obj ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
258 { code
: "(function(obj) { for ( const name in obj ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
261 { code
: "(function(iter) { let name; for ( name of iter ) return; })({});", parserOptions
: { ecmaVersion
: 6 } },
262 { code
: "(function(iter) { let name; for ( name of iter ) { return; } })({});", parserOptions
: { ecmaVersion
: 6 } },
263 { code
: "(function(iter) { for ( let name of iter ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
264 { code
: "(function(iter) { for ( let name of iter ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
266 { code
: "(function(iter) { for ( const name of iter ) { return true } })({})", parserOptions
: { ecmaVersion
: 6 } },
267 { code
: "(function(iter) { for ( const name of iter ) return true })({})", parserOptions
: { ecmaVersion
: 6 } },
269 // Sequence Expressions (See https://github.com/eslint/eslint/issues/14325)
270 { code
: "let x = 0; foo = (0, x++);", parserOptions
: { ecmaVersion
: 6 } },
271 { code
: "let x = 0; foo = (0, x += 1);", parserOptions
: { ecmaVersion
: 6 } },
272 { code
: "let x = 0; foo = (0, x = x + 1);", parserOptions
: { ecmaVersion
: 6 } },
276 code
: "try{}catch(err){console.error(err);}",
277 options
: [{ caughtErrors
: "all" }]
280 code
: "try{}catch(err){}",
281 options
: [{ caughtErrors
: "none" }]
284 code
: "try{}catch(ignoreErr){}",
285 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }]
288 // caughtErrors with other combinations
290 code
: "try{}catch(err){}",
291 options
: [{ vars
: "all", args
: "all" }]
294 // Using object rest for variable omission
296 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
297 options
: [{ ignoreRestSiblings
: true }],
298 parserOptions
: { ecmaVersion
: 2018 }
301 // https://github.com/eslint/eslint/issues/6348
302 "var a = 0, b; b = a = a + 1; foo(b);",
303 "var a = 0, b; b = a += a + 1; foo(b);",
304 "var a = 0, b; b = a++; foo(b);",
305 "function foo(a) { var b = a = a + 1; bar(b) } foo();",
306 "function foo(a) { var b = a += a + 1; bar(b) } foo();",
307 "function foo(a) { var b = a++; bar(b) } foo();",
309 // https://github.com/eslint/eslint/issues/6576
311 "var unregisterFooWatcher;",
313 "unregisterFooWatcher = $scope.$watch( \"foo\", function() {",
314 " // ...some code..",
315 " unregisterFooWatcher();",
320 "ref = setInterval(",
322 " clearInterval(ref);",
328 " _timer = setTimeout(function () {}, _timer ? 100 : 0);",
332 "function foo(cb) { cb = function() { function something(a) { cb(1 + a); } register(something); }(); } foo();",
333 { code
: "function* foo(cb) { cb = yield function(a) { cb(1 + a); }; } foo();", parserOptions
: { ecmaVersion
: 6 } },
334 { code
: "function foo(cb) { cb = tag`hello${function(a) { cb(1 + a); }}`; } foo();", parserOptions
: { ecmaVersion
: 6 } },
335 "function foo(cb) { var b; cb = b = function(a) { cb(1 + a); }; b(); } foo();",
337 // https://github.com/eslint/eslint/issues/6646
339 "function someFunction() {",
341 " for (i = 0; i < 2; i++) {",
342 " a = myFunction(a);",
348 // https://github.com/eslint/eslint/issues/7124
350 code
: "(function(a, b, {c, d}) { d })",
351 options
: [{ argsIgnorePattern
: "c" }],
352 parserOptions
: { ecmaVersion
: 6 }
355 code
: "(function(a, b, {c, d}) { c })",
356 options
: [{ argsIgnorePattern
: "d" }],
357 parserOptions
: { ecmaVersion
: 6 }
360 // https://github.com/eslint/eslint/issues/7250
362 code
: "(function(a, b, c) { c })",
363 options
: [{ argsIgnorePattern
: "c" }]
366 code
: "(function(a, b, {c, d}) { c })",
367 options
: [{ argsIgnorePattern
: "[cd]" }],
368 parserOptions
: { ecmaVersion
: 6 }
371 // https://github.com/eslint/eslint/issues/7351
373 code
: "(class { set foo(UNUSED) {} })",
374 parserOptions
: { ecmaVersion
: 6 }
377 code
: "class Foo { set bar(UNUSED) {} } console.log(Foo)",
378 parserOptions
: { ecmaVersion
: 6 }
381 // https://github.com/eslint/eslint/issues/8119
383 code
: "(({a, ...rest}) => rest)",
384 options
: [{ args
: "all", ignoreRestSiblings
: true }],
385 parserOptions
: { ecmaVersion
: 2018 }
388 // https://github.com/eslint/eslint/issues/14163
390 code
: "let foo, rest;\n({ foo, ...rest } = something);\nconsole.log(rest);",
391 options
: [{ ignoreRestSiblings
: true }],
392 parserOptions
: { ecmaVersion
: 2020 }
395 // https://github.com/eslint/eslint/issues/10952
396 "/*eslint use-every-a:1*/ !function(b, a) { return 1 }",
398 // https://github.com/eslint/eslint/issues/10982
399 "var a = function () { a(); }; a();",
400 "var a = function(){ return function () { a(); } }; a();",
402 code
: "const a = () => { a(); }; a();",
403 parserOptions
: { ecmaVersion
: 2015 }
406 code
: "const a = () => () => { a(); }; a();",
407 parserOptions
: { ecmaVersion
: 2015 }
410 // export * as ns from "source"
412 code
: 'export * as ns from "source"',
413 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
419 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
423 { code
: "function foox() { return foox(); }", errors
: [definedError("foox")] },
424 { code
: "(function() { function foox() { if (true) { return foox(); } } }())", errors
: [definedError("foox")] },
425 { code
: "var a=10", errors
: [assignedError("a")] },
426 { code
: "function f() { var a = 1; return function(){ f(a *= 2); }; }", errors
: [definedError("f")] },
427 { code
: "function f() { var a = 1; return function(){ f(++a); }; }", errors
: [definedError("f")] },
428 { code
: "/*global a */", errors
: [definedError("a", "", "Program")] },
429 { code
: "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});};", errors
: [definedError("foo")] },
430 { code
: "var a=10;", options
: ["all"], errors
: [assignedError("a")] },
431 { code
: "var a=10; a=20;", options
: ["all"], errors
: [assignedError("a")] },
432 { code
: "var a=10; (function() { var a = 1; alert(a); })();", options
: ["all"], errors
: [assignedError("a")] },
433 { code
: "var a=10, b=0, c=null; alert(a+b)", options
: ["all"], errors
: [assignedError("c")] },
434 { code
: "var a=10, b=0, c=null; setTimeout(function() { var b=2; alert(a+b+c); }, 0);", options
: ["all"], errors
: [assignedError("b")] },
435 { 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")] },
436 { code
: "function f(){var a=[];return a.map(function(){});}", options
: ["all"], errors
: [definedError("f")] },
437 { code
: "function f(){var a=[];return a.map(function g(){});}", options
: ["all"], errors
: [definedError("f")] },
439 code
: "function foo() {function foo(x) {\nreturn x; }; return function() {return foo; }; }",
441 messageId
: "unusedVar",
442 data
: { varName
: "foo", action
: "defined", additional
: "" },
447 { code
: "function f(){var x;function a(){x=42;}function b(){alert(x);}}", options
: ["all"], errors
: 3 },
448 { code
: "function f(a) {}; f();", options
: ["all"], errors
: [definedError("a")] },
449 { code
: "function a(x, y, z){ return y; }; a();", options
: ["all"], errors
: [definedError("z")] },
450 { code
: "var min = Math.min", options
: ["all"], errors
: [assignedError("min")] },
451 { code
: "var min = {min: 1}", options
: ["all"], errors
: [assignedError("min")] },
452 { code
: "Foo.bar = function(baz) { return 1; };", options
: ["all"], errors
: [definedError("baz")] },
453 { code
: "var min = {min: 1}", options
: [{ vars
: "all" }], errors
: [assignedError("min")] },
454 { code
: "function gg(baz, bar) { return baz; }; gg();", options
: [{ vars
: "all" }], errors
: [definedError("bar")] },
455 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "after-used" }], errors
: [definedError("bar")] },
456 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), definedError("bar")] },
457 { code
: "(function z(foo) { var bar = 33; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), assignedError("bar")] },
458 { code
: "(function z(foo) { z(); })();", options
: [{}], errors
: [definedError("foo")] },
459 { code
: "function f() { var a = 1; return function(){ f(a = 2); }; }", options
: [{}], errors
: [definedError("f"), assignedError("a")] },
460 { code
: "import x from \"y\";", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("x")] },
461 { code
: "export function fn2({ x, y }) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
462 { code
: "export function fn2( x, y ) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
465 { code
: "/*exported max*/ var max = 1, min = {min: 1}", errors
: [assignedError("min")] },
466 { code
: "/*exported x*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 }, errors
: [assignedError("y")] },
470 code
: "var _a; var b;",
471 options
: [{ vars
: "all", varsIgnorePattern
: "^_" }],
475 messageId
: "unusedVar",
479 additional
: ". Allowed unused vars must match /^_/u"
484 code
: "var a; function foo() { var _b; var c_; } foo();",
485 options
: [{ vars
: "local", varsIgnorePattern
: "^_" }],
489 messageId
: "unusedVar",
493 additional
: ". Allowed unused vars must match /^_/u"
498 code
: "function foo(a, _b) { } foo();",
499 options
: [{ args
: "all", argsIgnorePattern
: "^_" }],
503 messageId
: "unusedVar",
507 additional
: ". Allowed unused args must match /^_/u"
512 code
: "function foo(a, _b, c) { return a; } foo();",
513 options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }],
517 messageId
: "unusedVar",
521 additional
: ". Allowed unused args must match /^_/u"
526 code
: "function foo(_a) { } foo();",
527 options
: [{ args
: "all", argsIgnorePattern
: "[iI]gnored" }],
531 messageId
: "unusedVar",
535 additional
: ". Allowed unused args must match /[iI]gnored/u"
540 code
: "var [ firstItemIgnored, secondItem ] = items;",
541 options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }],
542 parserOptions
: { ecmaVersion
: 6 },
546 messageId
: "unusedVar",
548 varName
: "secondItem",
549 action
: "assigned a value",
550 additional
: ". Allowed unused vars must match /[iI]gnored/u"
555 // https://github.com/eslint/eslint/issues/15611
558 const array = ['a', 'b', 'c'];
559 const [a, _b, c] = array;
560 const newArray = [a, c];
562 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
563 parserOptions
: { ecmaVersion
: 2020 },
566 // should report only `newArray`
567 { ...assignedError("newArray"), line
: 4, column
: 19 }
572 const array = ['a', 'b', 'c', 'd', 'e'];
573 const [a, _b, c] = array;
575 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
576 parserOptions
: { ecmaVersion
: 2020 },
579 ...assignedError("a", ". Allowed unused elements of array destructuring patterns must match /^_/u"),
584 ...assignedError("c", ". Allowed unused elements of array destructuring patterns must match /^_/u"),
592 const array = ['a', 'b', 'c'];
593 const [a, _b, c] = array;
594 const fooArray = ['foo'];
595 const barArray = ['bar'];
596 const ignoreArray = ['ignore'];
598 options
: [{ destructuredArrayIgnorePattern
: "^_", varsIgnorePattern
: "ignore" }],
599 parserOptions
: { ecmaVersion
: 2020 },
602 ...assignedError("a", ". Allowed unused elements of array destructuring patterns must match /^_/u"),
607 ...assignedError("c", ". Allowed unused elements of array destructuring patterns must match /^_/u"),
612 ...assignedError("fooArray", ". Allowed unused vars must match /ignore/u"),
617 ...assignedError("barArray", ". Allowed unused vars must match /ignore/u"),
626 const [{_a, foo}] = array;
629 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
630 parserOptions
: { ecmaVersion
: 2020 },
633 ...assignedError("_a"),
641 function foo([{_a, bar}]) {
646 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
647 parserOptions
: { ecmaVersion
: 2020 },
650 ...definedError("_a"),
660 foo.forEach(item => {
664 options
: [{ destructuredArrayIgnorePattern
: "^_" }],
665 parserOptions
: { ecmaVersion
: 2020 },
668 ...definedError("_a"),
673 ...assignedError("b"),
680 // for-in loops (see #2342)
682 code
: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});",
686 messageId
: "unusedVar",
689 action
: "assigned a value",
695 code
: "(function(obj) { var name; for ( name in obj ) { } })({});",
699 messageId
: "unusedVar",
702 action
: "assigned a value",
708 code
: "(function(obj) { for ( var name in obj ) { } })({});",
712 messageId
: "unusedVar",
715 action
: "assigned a value",
723 code
: "(function(iter) { var name; for ( name of iter ) { i(); return; } })({});",
728 messageId
: "unusedVar",
731 action
: "assigned a value",
737 code
: "(function(iter) { var name; for ( name of iter ) { } })({});",
742 messageId
: "unusedVar",
745 action
: "assigned a value",
751 code
: "(function(iter) { for ( var name of iter ) { } })({});",
756 messageId
: "unusedVar",
759 action
: "assigned a value",
765 // https://github.com/eslint/eslint/issues/3617
767 code
: "\n/* global foobar, foo, bar */\nfoobar;",
774 messageId
: "unusedVar",
786 messageId
: "unusedVar",
796 code
: "\n/* global foobar,\n foo,\n bar\n */\nfoobar;",
803 messageId
: "unusedVar",
815 messageId
: "unusedVar",
825 // Rest property sibling without ignoreRestSiblings
827 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
828 parserOptions
: { ecmaVersion
: 2018 },
833 messageId
: "unusedVar",
836 action
: "assigned a value",
843 // Unused rest property with ignoreRestSiblings
845 code
: "const data = { type: 'coords', x: 2, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
846 options
: [{ ignoreRestSiblings
: true }],
847 parserOptions
: { ecmaVersion
: 2018 },
852 messageId
: "unusedVar",
855 action
: "assigned a value",
862 code
: "let type, coords;\n({ type, ...coords } = data);\n console.log(type)",
863 options
: [{ ignoreRestSiblings
: true }],
864 parserOptions
: { ecmaVersion
: 2018 },
869 messageId
: "unusedVar",
872 action
: "assigned a value",
879 // Unused rest property without ignoreRestSiblings
881 code
: "const data = { type: 'coords', x: 3, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
882 parserOptions
: { ecmaVersion
: 2018 },
887 messageId
: "unusedVar",
890 action
: "assigned a value",
897 // Nested array destructuring with rest property
899 code
: "const data = { vars: ['x','y'], x: 1, y: 2 };\nconst { vars: [x], ...coords } = data;\n console.log(coords)",
900 parserOptions
: { ecmaVersion
: 2018 },
905 messageId
: "unusedVar",
908 action
: "assigned a value",
915 // Nested object destructuring with rest property
917 code
: "const data = { defaults: { x: 0 }, x: 1, y: 2 };\nconst { defaults: { x }, ...coords } = data;\n console.log(coords)",
918 parserOptions
: { ecmaVersion
: 2018 },
923 messageId
: "unusedVar",
926 action
: "assigned a value",
933 // https://github.com/eslint/eslint/issues/8119
935 code
: "(({a, ...rest}) => {})",
936 options
: [{ args
: "all", ignoreRestSiblings
: true }],
937 parserOptions
: { ecmaVersion
: 2018 },
938 errors
: [definedError("rest")]
941 // https://github.com/eslint/eslint/issues/3714
943 code
: "/* global a$fooz,$foo */\na$fooz;",
950 messageId
: "unusedVar",
960 code
: "/* globals a$fooz, $ */\na$fooz;",
967 messageId
: "unusedVar",
977 code
: "/*globals $foo*/",
984 messageId
: "unusedVar",
994 code
: "/* global global*/",
1001 messageId
: "unusedVar",
1011 code
: "/*global foo:true*/",
1018 messageId
: "unusedVar",
1030 code
: "/*global 変数, 数*/\n変数;",
1037 messageId
: "unusedVar",
1049 code
: "/*global 𠮷𩸽, 𠮷*/\n\\u{20BB7}\\u{29E3D};",
1057 messageId
: "unusedVar",
1067 // https://github.com/eslint/eslint/issues/4047
1069 code
: "export default function(a) {}",
1070 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1071 errors
: [definedError("a")]
1074 code
: "export default function(a, b) { console.log(a); }",
1075 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1076 errors
: [definedError("b")]
1079 code
: "export default (function(a) {});",
1080 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1081 errors
: [definedError("a")]
1084 code
: "export default (function(a, b) { console.log(a); });",
1085 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1086 errors
: [definedError("b")]
1089 code
: "export default (a) => {};",
1090 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1091 errors
: [definedError("a")]
1094 code
: "export default (a, b) => { console.log(a); };",
1095 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
1096 errors
: [definedError("b")]
1101 code
: "try{}catch(err){};",
1102 options
: [{ caughtErrors
: "all" }],
1103 errors
: [definedError("err")]
1106 code
: "try{}catch(err){};",
1107 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
1108 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
1111 // multiple try catch with one success
1113 code
: "try{}catch(ignoreErr){}try{}catch(err){};",
1114 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
1115 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
1118 // multiple try catch both fail
1120 code
: "try{}catch(error){}try{}catch(err){};",
1121 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
1123 definedError("error", ". Allowed unused args must match /^ignore/u"),
1124 definedError("err", ". Allowed unused args must match /^ignore/u")
1128 // caughtErrors with other configs
1130 code
: "try{}catch(err){};",
1131 options
: [{ vars
: "all", args
: "all", caughtErrors
: "all" }],
1132 errors
: [definedError("err")]
1135 // no conflict in ignore patterns
1137 code
: "try{}catch(err){};",
1142 caughtErrors
: "all",
1143 argsIgnorePattern
: "^er"
1146 errors
: [definedError("err")]
1149 // Ignore reads for modifications to itself: https://github.com/eslint/eslint/issues/6348
1150 { code
: "var a = 0; a = a + 1;", errors
: [assignedError("a")] },
1151 { code
: "var a = 0; a = a + a;", errors
: [assignedError("a")] },
1152 { code
: "var a = 0; a += a + 1;", errors
: [assignedError("a")] },
1153 { code
: "var a = 0; a++;", errors
: [assignedError("a")] },
1154 { code
: "function foo(a) { a = a + 1 } foo();", errors
: [assignedError("a")] },
1155 { code
: "function foo(a) { a += a + 1 } foo();", errors
: [assignedError("a")] },
1156 { code
: "function foo(a) { a++ } foo();", errors
: [assignedError("a")] },
1157 { code
: "var a = 3; a = a * 5 + 6;", errors
: [assignedError("a")] },
1158 { code
: "var a = 2, b = 4; a = a * 2 + b;", errors
: [assignedError("a")] },
1160 // https://github.com/eslint/eslint/issues/6576 (For coverage)
1162 code
: "function foo(cb) { cb = function(a) { cb(1 + a); }; bar(not_cb); } foo();",
1163 errors
: [assignedError("cb")]
1166 code
: "function foo(cb) { cb = function(a) { return cb(1 + a); }(); } foo();",
1167 errors
: [assignedError("cb")]
1170 code
: "function foo(cb) { cb = (function(a) { cb(1 + a); }, cb); } foo();",
1171 errors
: [assignedError("cb")]
1174 code
: "function foo(cb) { cb = (0, function(a) { cb(1 + a); }); } foo();",
1175 errors
: [assignedError("cb")]
1178 // https://github.com/eslint/eslint/issues/6646
1182 " function foo(b) {",
1188 errors
: [assignedError("b")]
1191 // https://github.com/eslint/eslint/issues/7124
1193 code
: "(function(a, b, c) {})",
1194 options
: [{ argsIgnorePattern
: "c" }],
1196 definedError("a", ". Allowed unused args must match /c/u"),
1197 definedError("b", ". Allowed unused args must match /c/u")
1201 code
: "(function(a, b, {c, d}) {})",
1202 options
: [{ argsIgnorePattern
: "[cd]" }],
1203 parserOptions
: { ecmaVersion
: 6 },
1205 definedError("a", ". Allowed unused args must match /[cd]/u"),
1206 definedError("b", ". Allowed unused args must match /[cd]/u")
1210 code
: "(function(a, b, {c, d}) {})",
1211 options
: [{ argsIgnorePattern
: "c" }],
1212 parserOptions
: { ecmaVersion
: 6 },
1214 definedError("a", ". Allowed unused args must match /c/u"),
1215 definedError("b", ". Allowed unused args must match /c/u"),
1216 definedError("d", ". Allowed unused args must match /c/u")
1220 code
: "(function(a, b, {c, d}) {})",
1221 options
: [{ argsIgnorePattern
: "d" }],
1222 parserOptions
: { ecmaVersion
: 6 },
1224 definedError("a", ". Allowed unused args must match /d/u"),
1225 definedError("b", ". Allowed unused args must match /d/u"),
1226 definedError("c", ". Allowed unused args must match /d/u")
1230 code
: "/*global\rfoo*/",
1236 messageId
: "unusedVar",
1245 // https://github.com/eslint/eslint/issues/8442
1247 code
: "(function ({ a }, b ) { return b; })();",
1248 parserOptions
: { ecmaVersion
: 2015 },
1254 code
: "(function ({ a }, { b, c } ) { return b; })();",
1255 parserOptions
: { ecmaVersion
: 2015 },
1262 // https://github.com/eslint/eslint/issues/14325
1266 parserOptions
: { ecmaVersion
: 2015 },
1267 errors
: [{ ...assignedError("x"), line
: 2, column
: 18 }]
1273 parserOptions
: { ecmaVersion
: 2015 },
1274 errors
: [{ ...assignedError("x"), line
: 3, column
: 13 }]
1277 code
: "let x = 0; x++, 0;",
1278 parserOptions
: { ecmaVersion
: 2015 },
1279 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1282 code
: "let x = 0; 0, x++;",
1283 parserOptions
: { ecmaVersion
: 2015 },
1284 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1287 code
: "let x = 0; 0, (1, x++);",
1288 parserOptions
: { ecmaVersion
: 2015 },
1289 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1292 code
: "let x = 0; foo = (x++, 0);",
1293 parserOptions
: { ecmaVersion
: 2015 },
1294 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1297 code
: "let x = 0; foo = ((0, x++), 0);",
1298 parserOptions
: { ecmaVersion
: 2015 },
1299 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1302 code
: "let x = 0; x += 1, 0;",
1303 parserOptions
: { ecmaVersion
: 2015 },
1304 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1307 code
: "let x = 0; 0, x += 1;",
1308 parserOptions
: { ecmaVersion
: 2015 },
1309 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1312 code
: "let x = 0; 0, (1, x += 1);",
1313 parserOptions
: { ecmaVersion
: 2015 },
1314 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1317 code
: "let x = 0; foo = (x += 1, 0);",
1318 parserOptions
: { ecmaVersion
: 2015 },
1319 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1322 code
: "let x = 0; foo = ((0, x += 1), 0);",
1323 parserOptions
: { ecmaVersion
: 2015 },
1324 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1327 // https://github.com/eslint/eslint/issues/14866
1332 parserOptions
: { ecmaVersion
: 2020 },
1333 errors
: [{ ...assignedError("z"), line
: 2, column
: 24 }]
1339 parserOptions
: { ecmaVersion
: 2020 },
1340 errors
: [{ ...assignedError("z"), line
: 3, column
: 13 }]
1347 parserOptions
: { ecmaVersion
: 2020 },
1348 errors
: [{ ...assignedError("z"), line
: 3, column
: 13 }]
1351 code
: "let x = 0; 0, x = x+1;",
1352 parserOptions
: { ecmaVersion
: 2020 },
1353 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1356 code
: "let x = 0; x = x+1, 0;",
1357 parserOptions
: { ecmaVersion
: 2020 },
1358 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1361 code
: "let x = 0; foo = ((0, x = x + 1), 0);",
1362 parserOptions
: { ecmaVersion
: 2020 },
1363 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1366 code
: "let x = 0; foo = (x = x+1, 0);",
1367 parserOptions
: { ecmaVersion
: 2020 },
1368 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1371 code
: "let x = 0; 0, (1, x=x+1);",
1372 parserOptions
: { ecmaVersion
: 2020 },
1373 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1376 code
: "(function ({ a, b }, { c } ) { return b; })();",
1377 parserOptions
: { ecmaVersion
: 2015 },
1384 code
: "(function ([ a ], b ) { return b; })();",
1385 parserOptions
: { ecmaVersion
: 2015 },
1391 code
: "(function ([ a ], [ b, c ] ) { return b; })();",
1392 parserOptions
: { ecmaVersion
: 2015 },
1399 code
: "(function ([ a, b ], [ c ] ) { return b; })();",
1400 parserOptions
: { ecmaVersion
: 2015 },
1407 // https://github.com/eslint/eslint/issues/9774
1409 code
: "(function(_a) {})();",
1410 options
: [{ args
: "all", varsIgnorePattern
: "^_" }],
1411 errors
: [definedError("_a")]
1414 code
: "(function(_a) {})();",
1415 options
: [{ args
: "all", caughtErrorsIgnorePattern
: "^_" }],
1416 errors
: [definedError("_a")]
1419 // https://github.com/eslint/eslint/issues/10982
1421 code
: "var a = function() { a(); };",
1422 errors
: [assignedError("a")]
1425 code
: "var a = function(){ return function() { a(); } };",
1426 errors
: [assignedError("a")]
1429 code
: "const a = () => { a(); };",
1430 parserOptions
: { ecmaVersion
: 2015 },
1431 errors
: [assignedError("a")]
1434 code
: "const a = () => () => { a(); };",
1435 parserOptions
: { ecmaVersion
: 2015 },
1436 errors
: [assignedError("a")]
1439 code
: `let myArray = [1,2,3,4].filter((x) => x == 0);
1440 myArray = myArray.filter((x) => x == 1);`,
1441 parserOptions
: { ecmaVersion
: 2015 },
1442 errors
: [{ ...assignedError("myArray"), line
: 2, column
: 5 }]
1445 code
: "const a = 1; a += 1;",
1446 parserOptions
: { ecmaVersion
: 2015 },
1447 errors
: [{ ...assignedError("a"), line
: 1, column
: 14 }]
1450 code
: "var a = function() { a(); };",
1451 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1454 code
: "var a = function(){ return function() { a(); } };",
1455 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1458 code
: "const a = () => { a(); };",
1459 parserOptions
: { ecmaVersion
: 2015 },
1460 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1463 code
: "const a = () => () => { a(); };",
1464 parserOptions
: { ecmaVersion
: 2015 },
1465 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1468 // https://github.com/eslint/eslint/issues/14324
1470 code
: "let x = [];\nx = x.concat(x);",
1471 parserOptions
: { ecmaVersion
: 2015 },
1472 errors
: [{ ...assignedError("x"), line
: 2, column
: 1 }]
1484 parserOptions
: { ecmaVersion
: 2020 },
1485 errors
: [{ ...assignedError("a"), line
: 2, column
: 13 }, { ...definedError("foo"), line
: 3, column
: 22 }]
1494 parserOptions
: { ecmaVersion
: 2020 },
1495 errors
: [{ ...assignedError("foo"), line
: 3, column
: 13 }]
1498 code
: `function foo(n) {
1499 if (n < 2) return 1;
1500 return n * foo(n - 1);
1502 parserOptions
: { ecmaVersion
: 2020 },
1503 errors
: [{ ...definedError("foo"), line
: 1, column
: 10 }]
1516 parserOptions
: { ecmaVersion
: 2020 },
1517 errors
: [{ ...assignedError("c"), line
: 10, column
: 1 }]