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 } },
183 { code
: "let x = 0; foo = (0, x = x + 1);", parserOptions
: { ecmaVersion
: 6 } },
187 code
: "try{}catch(err){console.error(err);}",
188 options
: [{ caughtErrors
: "all" }]
191 code
: "try{}catch(err){}",
192 options
: [{ caughtErrors
: "none" }]
195 code
: "try{}catch(ignoreErr){}",
196 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }]
199 // caughtErrors with other combinations
201 code
: "try{}catch(err){}",
202 options
: [{ vars
: "all", args
: "all" }]
205 // Using object rest for variable omission
207 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
208 options
: [{ ignoreRestSiblings
: true }],
209 parserOptions
: { ecmaVersion
: 2018 }
212 // https://github.com/eslint/eslint/issues/6348
213 "var a = 0, b; b = a = a + 1; foo(b);",
214 "var a = 0, b; b = a += a + 1; foo(b);",
215 "var a = 0, b; b = a++; foo(b);",
216 "function foo(a) { var b = a = a + 1; bar(b) } foo();",
217 "function foo(a) { var b = a += a + 1; bar(b) } foo();",
218 "function foo(a) { var b = a++; bar(b) } foo();",
220 // https://github.com/eslint/eslint/issues/6576
222 "var unregisterFooWatcher;",
224 "unregisterFooWatcher = $scope.$watch( \"foo\", function() {",
225 " // ...some code..",
226 " unregisterFooWatcher();",
231 "ref = setInterval(",
233 " clearInterval(ref);",
239 " _timer = setTimeout(function () {}, _timer ? 100 : 0);",
243 "function foo(cb) { cb = function() { function something(a) { cb(1 + a); } register(something); }(); } foo();",
244 { code
: "function* foo(cb) { cb = yield function(a) { cb(1 + a); }; } foo();", parserOptions
: { ecmaVersion
: 6 } },
245 { code
: "function foo(cb) { cb = tag`hello${function(a) { cb(1 + a); }}`; } foo();", parserOptions
: { ecmaVersion
: 6 } },
246 "function foo(cb) { var b; cb = b = function(a) { cb(1 + a); }; b(); } foo();",
248 // https://github.com/eslint/eslint/issues/6646
250 "function someFunction() {",
252 " for (i = 0; i < 2; i++) {",
253 " a = myFunction(a);",
259 // https://github.com/eslint/eslint/issues/7124
261 code
: "(function(a, b, {c, d}) { d })",
262 options
: [{ argsIgnorePattern
: "c" }],
263 parserOptions
: { ecmaVersion
: 6 }
266 code
: "(function(a, b, {c, d}) { c })",
267 options
: [{ argsIgnorePattern
: "d" }],
268 parserOptions
: { ecmaVersion
: 6 }
271 // https://github.com/eslint/eslint/issues/7250
273 code
: "(function(a, b, c) { c })",
274 options
: [{ argsIgnorePattern
: "c" }]
277 code
: "(function(a, b, {c, d}) { c })",
278 options
: [{ argsIgnorePattern
: "[cd]" }],
279 parserOptions
: { ecmaVersion
: 6 }
282 // https://github.com/eslint/eslint/issues/7351
284 code
: "(class { set foo(UNUSED) {} })",
285 parserOptions
: { ecmaVersion
: 6 }
288 code
: "class Foo { set bar(UNUSED) {} } console.log(Foo)",
289 parserOptions
: { ecmaVersion
: 6 }
292 // https://github.com/eslint/eslint/issues/8119
294 code
: "(({a, ...rest}) => rest)",
295 options
: [{ args
: "all", ignoreRestSiblings
: true }],
296 parserOptions
: { ecmaVersion
: 2018 }
299 // https://github.com/eslint/eslint/issues/14163
301 code
: "let foo, rest;\n({ foo, ...rest } = something);\nconsole.log(rest);",
302 options
: [{ ignoreRestSiblings
: true }],
303 parserOptions
: { ecmaVersion
: 2020 }
306 // https://github.com/eslint/eslint/issues/10952
307 "/*eslint use-every-a:1*/ !function(b, a) { return 1 }",
309 // https://github.com/eslint/eslint/issues/10982
310 "var a = function () { a(); }; a();",
311 "var a = function(){ return function () { a(); } }; a();",
313 code
: "const a = () => { a(); }; a();",
314 parserOptions
: { ecmaVersion
: 2015 }
317 code
: "const a = () => () => { a(); }; a();",
318 parserOptions
: { ecmaVersion
: 2015 }
321 // export * as ns from "source"
323 code
: 'export * as ns from "source"',
324 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
330 parserOptions
: { ecmaVersion
: 2020, sourceType
: "module" }
334 { code
: "function foox() { return foox(); }", errors
: [definedError("foox")] },
335 { code
: "(function() { function foox() { if (true) { return foox(); } } }())", errors
: [definedError("foox")] },
336 { code
: "var a=10", errors
: [assignedError("a")] },
337 { code
: "function f() { var a = 1; return function(){ f(a *= 2); }; }", errors
: [definedError("f")] },
338 { code
: "function f() { var a = 1; return function(){ f(++a); }; }", errors
: [definedError("f")] },
339 { code
: "/*global a */", errors
: [definedError("a", "", "Program")] },
340 { code
: "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});};", errors
: [definedError("foo")] },
341 { code
: "var a=10;", options
: ["all"], errors
: [assignedError("a")] },
342 { code
: "var a=10; a=20;", options
: ["all"], errors
: [assignedError("a")] },
343 { code
: "var a=10; (function() { var a = 1; alert(a); })();", options
: ["all"], errors
: [assignedError("a")] },
344 { code
: "var a=10, b=0, c=null; alert(a+b)", options
: ["all"], errors
: [assignedError("c")] },
345 { code
: "var a=10, b=0, c=null; setTimeout(function() { var b=2; alert(a+b+c); }, 0);", options
: ["all"], errors
: [assignedError("b")] },
346 { 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")] },
347 { code
: "function f(){var a=[];return a.map(function(){});}", options
: ["all"], errors
: [definedError("f")] },
348 { code
: "function f(){var a=[];return a.map(function g(){});}", options
: ["all"], errors
: [definedError("f")] },
350 code
: "function foo() {function foo(x) {\nreturn x; }; return function() {return foo; }; }",
352 messageId
: "unusedVar",
353 data
: { varName
: "foo", action
: "defined", additional
: "" },
358 { code
: "function f(){var x;function a(){x=42;}function b(){alert(x);}}", options
: ["all"], errors
: 3 },
359 { code
: "function f(a) {}; f();", options
: ["all"], errors
: [definedError("a")] },
360 { code
: "function a(x, y, z){ return y; }; a();", options
: ["all"], errors
: [definedError("z")] },
361 { code
: "var min = Math.min", options
: ["all"], errors
: [assignedError("min")] },
362 { code
: "var min = {min: 1}", options
: ["all"], errors
: [assignedError("min")] },
363 { code
: "Foo.bar = function(baz) { return 1; };", options
: ["all"], errors
: [definedError("baz")] },
364 { code
: "var min = {min: 1}", options
: [{ vars
: "all" }], errors
: [assignedError("min")] },
365 { code
: "function gg(baz, bar) { return baz; }; gg();", options
: [{ vars
: "all" }], errors
: [definedError("bar")] },
366 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "after-used" }], errors
: [definedError("bar")] },
367 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), definedError("bar")] },
368 { code
: "(function z(foo) { var bar = 33; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), assignedError("bar")] },
369 { code
: "(function z(foo) { z(); })();", options
: [{}], errors
: [definedError("foo")] },
370 { code
: "function f() { var a = 1; return function(){ f(a = 2); }; }", options
: [{}], errors
: [definedError("f"), assignedError("a")] },
371 { code
: "import x from \"y\";", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("x")] },
372 { code
: "export function fn2({ x, y }) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
373 { code
: "export function fn2( x, y ) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
376 { code
: "/*exported max*/ var max = 1, min = {min: 1}", errors
: [assignedError("min")] },
377 { code
: "/*exported x*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 }, errors
: [assignedError("y")] },
381 code
: "var _a; var b;",
382 options
: [{ vars
: "all", varsIgnorePattern
: "^_" }],
386 messageId
: "unusedVar",
390 additional
: ". Allowed unused vars must match /^_/u"
395 code
: "var a; function foo() { var _b; var c_; } foo();",
396 options
: [{ vars
: "local", varsIgnorePattern
: "^_" }],
400 messageId
: "unusedVar",
404 additional
: ". Allowed unused vars must match /^_/u"
409 code
: "function foo(a, _b) { } foo();",
410 options
: [{ args
: "all", argsIgnorePattern
: "^_" }],
414 messageId
: "unusedVar",
418 additional
: ". Allowed unused args must match /^_/u"
423 code
: "function foo(a, _b, c) { return a; } foo();",
424 options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }],
428 messageId
: "unusedVar",
432 additional
: ". Allowed unused args must match /^_/u"
437 code
: "function foo(_a) { } foo();",
438 options
: [{ args
: "all", argsIgnorePattern
: "[iI]gnored" }],
442 messageId
: "unusedVar",
446 additional
: ". Allowed unused args must match /[iI]gnored/u"
451 code
: "var [ firstItemIgnored, secondItem ] = items;",
452 options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }],
453 parserOptions
: { ecmaVersion
: 6 },
457 messageId
: "unusedVar",
459 varName
: "secondItem",
460 action
: "assigned a value",
461 additional
: ". Allowed unused vars must match /[iI]gnored/u"
466 // for-in loops (see #2342)
468 code
: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});",
472 messageId
: "unusedVar",
475 action
: "assigned a value",
481 code
: "(function(obj) { var name; for ( name in obj ) { } })({});",
485 messageId
: "unusedVar",
488 action
: "assigned a value",
494 code
: "(function(obj) { for ( var name in obj ) { } })({});",
498 messageId
: "unusedVar",
501 action
: "assigned a value",
507 // https://github.com/eslint/eslint/issues/3617
509 code
: "\n/* global foobar, foo, bar */\nfoobar;",
516 messageId
: "unusedVar",
528 messageId
: "unusedVar",
538 code
: "\n/* global foobar,\n foo,\n bar\n */\nfoobar;",
545 messageId
: "unusedVar",
557 messageId
: "unusedVar",
567 // Rest property sibling without ignoreRestSiblings
569 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
570 parserOptions
: { ecmaVersion
: 2018 },
575 messageId
: "unusedVar",
578 action
: "assigned a value",
585 // Unused rest property with ignoreRestSiblings
587 code
: "const data = { type: 'coords', x: 2, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
588 options
: [{ ignoreRestSiblings
: true }],
589 parserOptions
: { ecmaVersion
: 2018 },
594 messageId
: "unusedVar",
597 action
: "assigned a value",
604 code
: "let type, coords;\n({ type, ...coords } = data);\n console.log(type)",
605 options
: [{ ignoreRestSiblings
: true }],
606 parserOptions
: { ecmaVersion
: 2018 },
611 messageId
: "unusedVar",
614 action
: "assigned a value",
621 // Unused rest property without ignoreRestSiblings
623 code
: "const data = { type: 'coords', x: 3, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
624 parserOptions
: { ecmaVersion
: 2018 },
629 messageId
: "unusedVar",
632 action
: "assigned a value",
639 // Nested array destructuring with rest property
641 code
: "const data = { vars: ['x','y'], x: 1, y: 2 };\nconst { vars: [x], ...coords } = data;\n console.log(coords)",
642 parserOptions
: { ecmaVersion
: 2018 },
647 messageId
: "unusedVar",
650 action
: "assigned a value",
657 // Nested object destructuring with rest property
659 code
: "const data = { defaults: { x: 0 }, x: 1, y: 2 };\nconst { defaults: { x }, ...coords } = data;\n console.log(coords)",
660 parserOptions
: { ecmaVersion
: 2018 },
665 messageId
: "unusedVar",
668 action
: "assigned a value",
675 // https://github.com/eslint/eslint/issues/8119
677 code
: "(({a, ...rest}) => {})",
678 options
: [{ args
: "all", ignoreRestSiblings
: true }],
679 parserOptions
: { ecmaVersion
: 2018 },
680 errors
: [definedError("rest")]
683 // https://github.com/eslint/eslint/issues/3714
685 code
: "/* global a$fooz,$foo */\na$fooz;",
692 messageId
: "unusedVar",
702 code
: "/* globals a$fooz, $ */\na$fooz;",
709 messageId
: "unusedVar",
719 code
: "/*globals $foo*/",
726 messageId
: "unusedVar",
736 code
: "/* global global*/",
743 messageId
: "unusedVar",
753 code
: "/*global foo:true*/",
760 messageId
: "unusedVar",
772 code
: "/*global 変数, 数*/\n変数;",
779 messageId
: "unusedVar",
791 code
: "/*global 𠮷𩸽, 𠮷*/\n\\u{20BB7}\\u{29E3D};",
799 messageId
: "unusedVar",
809 // https://github.com/eslint/eslint/issues/4047
811 code
: "export default function(a) {}",
812 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
813 errors
: [definedError("a")]
816 code
: "export default function(a, b) { console.log(a); }",
817 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
818 errors
: [definedError("b")]
821 code
: "export default (function(a) {});",
822 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
823 errors
: [definedError("a")]
826 code
: "export default (function(a, b) { console.log(a); });",
827 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
828 errors
: [definedError("b")]
831 code
: "export default (a) => {};",
832 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
833 errors
: [definedError("a")]
836 code
: "export default (a, b) => { console.log(a); };",
837 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
838 errors
: [definedError("b")]
843 code
: "try{}catch(err){};",
844 options
: [{ caughtErrors
: "all" }],
845 errors
: [definedError("err")]
848 code
: "try{}catch(err){};",
849 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
850 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
853 // multiple try catch with one success
855 code
: "try{}catch(ignoreErr){}try{}catch(err){};",
856 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
857 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
860 // multiple try catch both fail
862 code
: "try{}catch(error){}try{}catch(err){};",
863 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
865 definedError("error", ". Allowed unused args must match /^ignore/u"),
866 definedError("err", ". Allowed unused args must match /^ignore/u")
870 // caughtErrors with other configs
872 code
: "try{}catch(err){};",
873 options
: [{ vars
: "all", args
: "all", caughtErrors
: "all" }],
874 errors
: [definedError("err")]
877 // no conflict in ignore patterns
879 code
: "try{}catch(err){};",
885 argsIgnorePattern
: "^er"
888 errors
: [definedError("err")]
891 // Ignore reads for modifications to itself: https://github.com/eslint/eslint/issues/6348
892 { code
: "var a = 0; a = a + 1;", errors
: [assignedError("a")] },
893 { code
: "var a = 0; a = a + a;", errors
: [assignedError("a")] },
894 { code
: "var a = 0; a += a + 1;", errors
: [assignedError("a")] },
895 { code
: "var a = 0; a++;", errors
: [assignedError("a")] },
896 { code
: "function foo(a) { a = a + 1 } foo();", errors
: [assignedError("a")] },
897 { code
: "function foo(a) { a += a + 1 } foo();", errors
: [assignedError("a")] },
898 { code
: "function foo(a) { a++ } foo();", errors
: [assignedError("a")] },
899 { code
: "var a = 3; a = a * 5 + 6;", errors
: [assignedError("a")] },
900 { code
: "var a = 2, b = 4; a = a * 2 + b;", errors
: [assignedError("a")] },
902 // https://github.com/eslint/eslint/issues/6576 (For coverage)
904 code
: "function foo(cb) { cb = function(a) { cb(1 + a); }; bar(not_cb); } foo();",
905 errors
: [assignedError("cb")]
908 code
: "function foo(cb) { cb = function(a) { return cb(1 + a); }(); } foo();",
909 errors
: [assignedError("cb")]
912 code
: "function foo(cb) { cb = (function(a) { cb(1 + a); }, cb); } foo();",
913 errors
: [assignedError("cb")]
916 code
: "function foo(cb) { cb = (0, function(a) { cb(1 + a); }); } foo();",
917 errors
: [assignedError("cb")]
920 // https://github.com/eslint/eslint/issues/6646
924 " function foo(b) {",
930 errors
: [assignedError("b")]
933 // https://github.com/eslint/eslint/issues/7124
935 code
: "(function(a, b, c) {})",
936 options
: [{ argsIgnorePattern
: "c" }],
938 definedError("a", ". Allowed unused args must match /c/u"),
939 definedError("b", ". Allowed unused args must match /c/u")
943 code
: "(function(a, b, {c, d}) {})",
944 options
: [{ argsIgnorePattern
: "[cd]" }],
945 parserOptions
: { ecmaVersion
: 6 },
947 definedError("a", ". Allowed unused args must match /[cd]/u"),
948 definedError("b", ". Allowed unused args must match /[cd]/u")
952 code
: "(function(a, b, {c, d}) {})",
953 options
: [{ argsIgnorePattern
: "c" }],
954 parserOptions
: { ecmaVersion
: 6 },
956 definedError("a", ". Allowed unused args must match /c/u"),
957 definedError("b", ". Allowed unused args must match /c/u"),
958 definedError("d", ". Allowed unused args must match /c/u")
962 code
: "(function(a, b, {c, d}) {})",
963 options
: [{ argsIgnorePattern
: "d" }],
964 parserOptions
: { ecmaVersion
: 6 },
966 definedError("a", ". Allowed unused args must match /d/u"),
967 definedError("b", ". Allowed unused args must match /d/u"),
968 definedError("c", ". Allowed unused args must match /d/u")
972 code
: "/*global\rfoo*/",
978 messageId
: "unusedVar",
987 // https://github.com/eslint/eslint/issues/8442
989 code
: "(function ({ a }, b ) { return b; })();",
990 parserOptions
: { ecmaVersion
: 2015 },
996 code
: "(function ({ a }, { b, c } ) { return b; })();",
997 parserOptions
: { ecmaVersion
: 2015 },
1004 // https://github.com/eslint/eslint/issues/14325
1008 parserOptions
: { ecmaVersion
: 2015 },
1009 errors
: [{ ...assignedError("x"), line
: 2, column
: 18 }]
1015 parserOptions
: { ecmaVersion
: 2015 },
1016 errors
: [{ ...assignedError("x"), line
: 3, column
: 13 }]
1019 code
: "let x = 0; x++, 0;",
1020 parserOptions
: { ecmaVersion
: 2015 },
1021 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1024 code
: "let x = 0; 0, x++;",
1025 parserOptions
: { ecmaVersion
: 2015 },
1026 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1029 code
: "let x = 0; 0, (1, x++);",
1030 parserOptions
: { ecmaVersion
: 2015 },
1031 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1034 code
: "let x = 0; foo = (x++, 0);",
1035 parserOptions
: { ecmaVersion
: 2015 },
1036 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1039 code
: "let x = 0; foo = ((0, x++), 0);",
1040 parserOptions
: { ecmaVersion
: 2015 },
1041 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1044 code
: "let x = 0; x += 1, 0;",
1045 parserOptions
: { ecmaVersion
: 2015 },
1046 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1049 code
: "let x = 0; 0, x += 1;",
1050 parserOptions
: { ecmaVersion
: 2015 },
1051 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1054 code
: "let x = 0; 0, (1, x += 1);",
1055 parserOptions
: { ecmaVersion
: 2015 },
1056 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1059 code
: "let x = 0; foo = (x += 1, 0);",
1060 parserOptions
: { ecmaVersion
: 2015 },
1061 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1064 code
: "let x = 0; foo = ((0, x += 1), 0);",
1065 parserOptions
: { ecmaVersion
: 2015 },
1066 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1069 // https://github.com/eslint/eslint/issues/14866
1074 parserOptions
: { ecmaVersion
: 2020 },
1075 errors
: [{ ...assignedError("z"), line
: 2, column
: 24 }]
1081 parserOptions
: { ecmaVersion
: 2020 },
1082 errors
: [{ ...assignedError("z"), line
: 3, column
: 13 }]
1089 parserOptions
: { ecmaVersion
: 2020 },
1090 errors
: [{ ...assignedError("z"), line
: 3, column
: 13 }]
1093 code
: "let x = 0; 0, x = x+1;",
1094 parserOptions
: { ecmaVersion
: 2020 },
1095 errors
: [{ ...assignedError("x"), line
: 1, column
: 15 }]
1098 code
: "let x = 0; x = x+1, 0;",
1099 parserOptions
: { ecmaVersion
: 2020 },
1100 errors
: [{ ...assignedError("x"), line
: 1, column
: 12 }]
1103 code
: "let x = 0; foo = ((0, x = x + 1), 0);",
1104 parserOptions
: { ecmaVersion
: 2020 },
1105 errors
: [{ ...assignedError("x"), line
: 1, column
: 23 }]
1108 code
: "let x = 0; foo = (x = x+1, 0);",
1109 parserOptions
: { ecmaVersion
: 2020 },
1110 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1113 code
: "let x = 0; 0, (1, x=x+1);",
1114 parserOptions
: { ecmaVersion
: 2020 },
1115 errors
: [{ ...assignedError("x"), line
: 1, column
: 19 }]
1118 code
: "(function ({ a, b }, { c } ) { return b; })();",
1119 parserOptions
: { ecmaVersion
: 2015 },
1126 code
: "(function ([ a ], b ) { return b; })();",
1127 parserOptions
: { ecmaVersion
: 2015 },
1133 code
: "(function ([ a ], [ b, c ] ) { return b; })();",
1134 parserOptions
: { ecmaVersion
: 2015 },
1141 code
: "(function ([ a, b ], [ c ] ) { return b; })();",
1142 parserOptions
: { ecmaVersion
: 2015 },
1149 // https://github.com/eslint/eslint/issues/9774
1151 code
: "(function(_a) {})();",
1152 options
: [{ args
: "all", varsIgnorePattern
: "^_" }],
1153 errors
: [definedError("_a")]
1156 code
: "(function(_a) {})();",
1157 options
: [{ args
: "all", caughtErrorsIgnorePattern
: "^_" }],
1158 errors
: [definedError("_a")]
1161 // https://github.com/eslint/eslint/issues/10982
1163 code
: "var a = function() { a(); };",
1164 errors
: [assignedError("a")]
1167 code
: "var a = function(){ return function() { a(); } };",
1168 errors
: [assignedError("a")]
1171 code
: "const a = () => { a(); };",
1172 parserOptions
: { ecmaVersion
: 2015 },
1173 errors
: [assignedError("a")]
1176 code
: "const a = () => () => { a(); };",
1177 parserOptions
: { ecmaVersion
: 2015 },
1178 errors
: [assignedError("a")]
1181 code
: `let myArray = [1,2,3,4].filter((x) => x == 0);
1182 myArray = myArray.filter((x) => x == 1);`,
1183 parserOptions
: { ecmaVersion
: 2015 },
1184 errors
: [{ ...assignedError("myArray"), line
: 2, column
: 5 }]
1187 code
: "const a = 1; a += 1;",
1188 parserOptions
: { ecmaVersion
: 2015 },
1189 errors
: [{ ...assignedError("a"), line
: 1, column
: 14 }]
1192 code
: "var a = function() { a(); };",
1193 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1196 code
: "var a = function(){ return function() { a(); } };",
1197 errors
: [{ ...assignedError("a"), line
: 1, column
: 5 }]
1200 code
: "const a = () => { a(); };",
1201 parserOptions
: { ecmaVersion
: 2015 },
1202 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1205 code
: "const a = () => () => { a(); };",
1206 parserOptions
: { ecmaVersion
: 2015 },
1207 errors
: [{ ...assignedError("a"), line
: 1, column
: 7 }]
1210 // https://github.com/eslint/eslint/issues/14324
1212 code
: "let x = [];\nx = x.concat(x);",
1213 parserOptions
: { ecmaVersion
: 2015 },
1214 errors
: [{ ...assignedError("x"), line
: 2, column
: 1 }]
1226 parserOptions
: { ecmaVersion
: 2020 },
1227 errors
: [{ ...assignedError("a"), line
: 2, column
: 13 }, { ...definedError("foo"), line
: 3, column
: 22 }]
1236 parserOptions
: { ecmaVersion
: 2020 },
1237 errors
: [{ ...assignedError("foo"), line
: 3, column
: 13 }]
1240 code
: `function foo(n) {
1241 if (n < 2) return 1;
1242 return n * foo(n - 1);
1244 parserOptions
: { ecmaVersion
: 2020 },
1245 errors
: [{ ...definedError("foo"), line
: 1, column
: 10 }]
1258 parserOptions
: { ecmaVersion
: 2020 },
1259 errors
: [{ ...assignedError("c"), line
: 10, column
: 1 }]