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 } },
182 code
: "try{}catch(err){console.error(err);}",
183 options
: [{ caughtErrors
: "all" }]
186 code
: "try{}catch(err){}",
187 options
: [{ caughtErrors
: "none" }]
190 code
: "try{}catch(ignoreErr){}",
191 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }]
194 // caughtErrors with other combinations
196 code
: "try{}catch(err){}",
197 options
: [{ vars
: "all", args
: "all" }]
200 // Using object rest for variable omission
202 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
203 options
: [{ ignoreRestSiblings
: true }],
204 parserOptions
: { ecmaVersion
: 2018 }
207 // https://github.com/eslint/eslint/issues/6348
208 "var a = 0, b; b = a = a + 1; foo(b);",
209 "var a = 0, b; b = a += a + 1; foo(b);",
210 "var a = 0, b; b = a++; foo(b);",
211 "function foo(a) { var b = a = a + 1; bar(b) } foo();",
212 "function foo(a) { var b = a += a + 1; bar(b) } foo();",
213 "function foo(a) { var b = a++; bar(b) } foo();",
215 // https://github.com/eslint/eslint/issues/6576
217 "var unregisterFooWatcher;",
219 "unregisterFooWatcher = $scope.$watch( \"foo\", function() {",
220 " // ...some code..",
221 " unregisterFooWatcher();",
226 "ref = setInterval(",
228 " clearInterval(ref);",
234 " _timer = setTimeout(function () {}, _timer ? 100 : 0);",
238 "function foo(cb) { cb = function() { function something(a) { cb(1 + a); } register(something); }(); } foo();",
239 { code
: "function* foo(cb) { cb = yield function(a) { cb(1 + a); }; } foo();", parserOptions
: { ecmaVersion
: 6 } },
240 { code
: "function foo(cb) { cb = tag`hello${function(a) { cb(1 + a); }}`; } foo();", parserOptions
: { ecmaVersion
: 6 } },
241 "function foo(cb) { var b; cb = b = function(a) { cb(1 + a); }; b(); } foo();",
243 // https://github.com/eslint/eslint/issues/6646
245 "function someFunction() {",
247 " for (i = 0; i < 2; i++) {",
248 " a = myFunction(a);",
254 // https://github.com/eslint/eslint/issues/7124
256 code
: "(function(a, b, {c, d}) { d })",
257 options
: [{ argsIgnorePattern
: "c" }],
258 parserOptions
: { ecmaVersion
: 6 }
261 code
: "(function(a, b, {c, d}) { c })",
262 options
: [{ argsIgnorePattern
: "d" }],
263 parserOptions
: { ecmaVersion
: 6 }
266 // https://github.com/eslint/eslint/issues/7250
268 code
: "(function(a, b, c) { c })",
269 options
: [{ argsIgnorePattern
: "c" }]
272 code
: "(function(a, b, {c, d}) { c })",
273 options
: [{ argsIgnorePattern
: "[cd]" }],
274 parserOptions
: { ecmaVersion
: 6 }
277 // https://github.com/eslint/eslint/issues/7351
279 code
: "(class { set foo(UNUSED) {} })",
280 parserOptions
: { ecmaVersion
: 6 }
283 code
: "class Foo { set bar(UNUSED) {} } console.log(Foo)",
284 parserOptions
: { ecmaVersion
: 6 }
287 // https://github.com/eslint/eslint/issues/8119
289 code
: "(({a, ...rest}) => rest)",
290 options
: [{ args
: "all", ignoreRestSiblings
: true }],
291 parserOptions
: { ecmaVersion
: 2018 }
294 // https://github.com/eslint/eslint/issues/10952
295 "/*eslint use-every-a:1*/ !function(b, a) { return 1 }",
297 // https://github.com/eslint/eslint/issues/10982
298 "var a = function () { a(); }; a();",
299 "var a = function(){ return function () { a(); } }; a();",
301 code
: "const a = () => { a(); }; a();",
302 parserOptions
: { ecmaVersion
: 2015 }
305 code
: "const a = () => () => { a(); }; a();",
306 parserOptions
: { ecmaVersion
: 2015 }
310 { code
: "function foox() { return foox(); }", errors
: [definedError("foox")] },
311 { code
: "(function() { function foox() { if (true) { return foox(); } } }())", errors
: [definedError("foox")] },
312 { code
: "var a=10", errors
: [assignedError("a")] },
313 { code
: "function f() { var a = 1; return function(){ f(a *= 2); }; }", errors
: [definedError("f")] },
314 { code
: "function f() { var a = 1; return function(){ f(++a); }; }", errors
: [definedError("f")] },
315 { code
: "/*global a */", errors
: [definedError("a", "", "Program")] },
316 { code
: "function foo(first, second) {\ndoStuff(function() {\nconsole.log(second);});};", errors
: [definedError("foo")] },
317 { code
: "var a=10;", options
: ["all"], errors
: [assignedError("a")] },
318 { code
: "var a=10; a=20;", options
: ["all"], errors
: [assignedError("a")] },
319 { code
: "var a=10; (function() { var a = 1; alert(a); })();", options
: ["all"], errors
: [assignedError("a")] },
320 { code
: "var a=10, b=0, c=null; alert(a+b)", options
: ["all"], errors
: [assignedError("c")] },
321 { code
: "var a=10, b=0, c=null; setTimeout(function() { var b=2; alert(a+b+c); }, 0);", options
: ["all"], errors
: [assignedError("b")] },
322 { 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")] },
323 { code
: "function f(){var a=[];return a.map(function(){});}", options
: ["all"], errors
: [definedError("f")] },
324 { code
: "function f(){var a=[];return a.map(function g(){});}", options
: ["all"], errors
: [definedError("f")] },
326 code
: "function foo() {function foo(x) {\nreturn x; }; return function() {return foo; }; }",
328 messageId
: "unusedVar",
329 data
: { varName
: "foo", action
: "defined", additional
: "" },
334 { code
: "function f(){var x;function a(){x=42;}function b(){alert(x);}}", options
: ["all"], errors
: 3 },
335 { code
: "function f(a) {}; f();", options
: ["all"], errors
: [definedError("a")] },
336 { code
: "function a(x, y, z){ return y; }; a();", options
: ["all"], errors
: [definedError("z")] },
337 { code
: "var min = Math.min", options
: ["all"], errors
: [assignedError("min")] },
338 { code
: "var min = {min: 1}", options
: ["all"], errors
: [assignedError("min")] },
339 { code
: "Foo.bar = function(baz) { return 1; };", options
: ["all"], errors
: [definedError("baz")] },
340 { code
: "var min = {min: 1}", options
: [{ vars
: "all" }], errors
: [assignedError("min")] },
341 { code
: "function gg(baz, bar) { return baz; }; gg();", options
: [{ vars
: "all" }], errors
: [definedError("bar")] },
342 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "after-used" }], errors
: [definedError("bar")] },
343 { code
: "(function(foo, baz, bar) { return baz; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), definedError("bar")] },
344 { code
: "(function z(foo) { var bar = 33; })();", options
: [{ vars
: "all", args
: "all" }], errors
: [definedError("foo"), assignedError("bar")] },
345 { code
: "(function z(foo) { z(); })();", options
: [{}], errors
: [definedError("foo")] },
346 { code
: "function f() { var a = 1; return function(){ f(a = 2); }; }", options
: [{}], errors
: [definedError("f"), assignedError("a")] },
347 { code
: "import x from \"y\";", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("x")] },
348 { code
: "export function fn2({ x, y }) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
349 { code
: "export function fn2( x, y ) {\n console.log(x); \n};", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" }, errors
: [definedError("y")] },
352 { code
: "/*exported max*/ var max = 1, min = {min: 1}", errors
: [assignedError("min")] },
353 { code
: "/*exported x*/ var { x, y } = z", parserOptions
: { ecmaVersion
: 6 }, errors
: [assignedError("y")] },
357 code
: "var _a; var b;",
358 options
: [{ vars
: "all", varsIgnorePattern
: "^_" }],
362 messageId
: "unusedVar",
366 additional
: ". Allowed unused vars must match /^_/u"
371 code
: "var a; function foo() { var _b; var c_; } foo();",
372 options
: [{ vars
: "local", varsIgnorePattern
: "^_" }],
376 messageId
: "unusedVar",
380 additional
: ". Allowed unused vars must match /^_/u"
385 code
: "function foo(a, _b) { } foo();",
386 options
: [{ args
: "all", argsIgnorePattern
: "^_" }],
390 messageId
: "unusedVar",
394 additional
: ". Allowed unused args must match /^_/u"
399 code
: "function foo(a, _b, c) { return a; } foo();",
400 options
: [{ args
: "after-used", argsIgnorePattern
: "^_" }],
404 messageId
: "unusedVar",
408 additional
: ". Allowed unused args must match /^_/u"
413 code
: "function foo(_a) { } foo();",
414 options
: [{ args
: "all", argsIgnorePattern
: "[iI]gnored" }],
418 messageId
: "unusedVar",
422 additional
: ". Allowed unused args must match /[iI]gnored/u"
427 code
: "var [ firstItemIgnored, secondItem ] = items;",
428 options
: [{ vars
: "all", varsIgnorePattern
: "[iI]gnored" }],
429 parserOptions
: { ecmaVersion
: 6 },
433 messageId
: "unusedVar",
435 varName
: "secondItem",
436 action
: "assigned a value",
437 additional
: ". Allowed unused vars must match /[iI]gnored/u"
442 // for-in loops (see #2342)
444 code
: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});",
448 messageId
: "unusedVar",
451 action
: "assigned a value",
457 code
: "(function(obj) { var name; for ( name in obj ) { } })({});",
461 messageId
: "unusedVar",
464 action
: "assigned a value",
470 code
: "(function(obj) { for ( var name in obj ) { } })({});",
474 messageId
: "unusedVar",
477 action
: "assigned a value",
483 // https://github.com/eslint/eslint/issues/3617
485 code
: "\n/* global foobar, foo, bar */\nfoobar;",
492 messageId
: "unusedVar",
504 messageId
: "unusedVar",
514 code
: "\n/* global foobar,\n foo,\n bar\n */\nfoobar;",
521 messageId
: "unusedVar",
533 messageId
: "unusedVar",
543 // Rest property sibling without ignoreRestSiblings
545 code
: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);",
546 parserOptions
: { ecmaVersion
: 2018 },
551 messageId
: "unusedVar",
554 action
: "assigned a value",
561 // Unused rest property with ignoreRestSiblings
563 code
: "const data = { type: 'coords', x: 2, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
564 options
: [{ ignoreRestSiblings
: true }],
565 parserOptions
: { ecmaVersion
: 2018 },
570 messageId
: "unusedVar",
573 action
: "assigned a value",
580 // Unused rest property without ignoreRestSiblings
582 code
: "const data = { type: 'coords', x: 3, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)",
583 parserOptions
: { ecmaVersion
: 2018 },
588 messageId
: "unusedVar",
591 action
: "assigned a value",
598 // Nested array destructuring with rest property
600 code
: "const data = { vars: ['x','y'], x: 1, y: 2 };\nconst { vars: [x], ...coords } = data;\n console.log(coords)",
601 parserOptions
: { ecmaVersion
: 2018 },
606 messageId
: "unusedVar",
609 action
: "assigned a value",
616 // Nested object destructuring with rest property
618 code
: "const data = { defaults: { x: 0 }, x: 1, y: 2 };\nconst { defaults: { x }, ...coords } = data;\n console.log(coords)",
619 parserOptions
: { ecmaVersion
: 2018 },
624 messageId
: "unusedVar",
627 action
: "assigned a value",
634 // https://github.com/eslint/eslint/issues/8119
636 code
: "(({a, ...rest}) => {})",
637 options
: [{ args
: "all", ignoreRestSiblings
: true }],
638 parserOptions
: { ecmaVersion
: 2018 },
639 errors
: [definedError("rest")]
642 // https://github.com/eslint/eslint/issues/3714
644 code
: "/* global a$fooz,$foo */\na$fooz;",
651 messageId
: "unusedVar",
661 code
: "/* globals a$fooz, $ */\na$fooz;",
668 messageId
: "unusedVar",
678 code
: "/*globals $foo*/",
685 messageId
: "unusedVar",
695 code
: "/* global global*/",
702 messageId
: "unusedVar",
712 code
: "/*global foo:true*/",
719 messageId
: "unusedVar",
731 code
: "/*global 変数, 数*/\n変数;",
738 messageId
: "unusedVar",
750 code
: "/*global 𠮷𩸽, 𠮷*/\n\\u{20BB7}\\u{29E3D};",
758 messageId
: "unusedVar",
768 // https://github.com/eslint/eslint/issues/4047
770 code
: "export default function(a) {}",
771 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
772 errors
: [definedError("a")]
775 code
: "export default function(a, b) { console.log(a); }",
776 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
777 errors
: [definedError("b")]
780 code
: "export default (function(a) {});",
781 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
782 errors
: [definedError("a")]
785 code
: "export default (function(a, b) { console.log(a); });",
786 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
787 errors
: [definedError("b")]
790 code
: "export default (a) => {};",
791 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
792 errors
: [definedError("a")]
795 code
: "export default (a, b) => { console.log(a); };",
796 parserOptions
: { ecmaVersion
: 6, sourceType
: "module" },
797 errors
: [definedError("b")]
802 code
: "try{}catch(err){};",
803 options
: [{ caughtErrors
: "all" }],
804 errors
: [definedError("err")]
807 code
: "try{}catch(err){};",
808 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
809 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
812 // multiple try catch with one success
814 code
: "try{}catch(ignoreErr){}try{}catch(err){};",
815 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
816 errors
: [definedError("err", ". Allowed unused args must match /^ignore/u")]
819 // multiple try catch both fail
821 code
: "try{}catch(error){}try{}catch(err){};",
822 options
: [{ caughtErrors
: "all", caughtErrorsIgnorePattern
: "^ignore" }],
824 definedError("error", ". Allowed unused args must match /^ignore/u"),
825 definedError("err", ". Allowed unused args must match /^ignore/u")
829 // caughtErrors with other configs
831 code
: "try{}catch(err){};",
832 options
: [{ vars
: "all", args
: "all", caughtErrors
: "all" }],
833 errors
: [definedError("err")]
836 // no conflict in ignore patterns
838 code
: "try{}catch(err){};",
844 argsIgnorePattern
: "^er"
847 errors
: [definedError("err")]
850 // Ignore reads for modifications to itself: https://github.com/eslint/eslint/issues/6348
851 { code
: "var a = 0; a = a + 1;", errors
: [assignedError("a")] },
852 { code
: "var a = 0; a = a + a;", errors
: [assignedError("a")] },
853 { code
: "var a = 0; a += a + 1;", errors
: [assignedError("a")] },
854 { code
: "var a = 0; a++;", errors
: [assignedError("a")] },
855 { code
: "function foo(a) { a = a + 1 } foo();", errors
: [assignedError("a")] },
856 { code
: "function foo(a) { a += a + 1 } foo();", errors
: [assignedError("a")] },
857 { code
: "function foo(a) { a++ } foo();", errors
: [assignedError("a")] },
858 { code
: "var a = 3; a = a * 5 + 6;", errors
: [assignedError("a")] },
859 { code
: "var a = 2, b = 4; a = a * 2 + b;", errors
: [assignedError("a")] },
861 // https://github.com/eslint/eslint/issues/6576 (For coverage)
863 code
: "function foo(cb) { cb = function(a) { cb(1 + a); }; bar(not_cb); } foo();",
864 errors
: [assignedError("cb")]
867 code
: "function foo(cb) { cb = function(a) { return cb(1 + a); }(); } foo();",
868 errors
: [assignedError("cb")]
871 code
: "function foo(cb) { cb = (function(a) { cb(1 + a); }, cb); } foo();",
872 errors
: [assignedError("cb")]
875 code
: "function foo(cb) { cb = (0, function(a) { cb(1 + a); }); } foo();",
876 errors
: [assignedError("cb")]
879 // https://github.com/eslint/eslint/issues/6646
883 " function foo(b) {",
889 errors
: [assignedError("b")]
892 // https://github.com/eslint/eslint/issues/7124
894 code
: "(function(a, b, c) {})",
895 options
: [{ argsIgnorePattern
: "c" }],
897 definedError("a", ". Allowed unused args must match /c/u"),
898 definedError("b", ". Allowed unused args must match /c/u")
902 code
: "(function(a, b, {c, d}) {})",
903 options
: [{ argsIgnorePattern
: "[cd]" }],
904 parserOptions
: { ecmaVersion
: 6 },
906 definedError("a", ". Allowed unused args must match /[cd]/u"),
907 definedError("b", ". Allowed unused args must match /[cd]/u")
911 code
: "(function(a, b, {c, d}) {})",
912 options
: [{ argsIgnorePattern
: "c" }],
913 parserOptions
: { ecmaVersion
: 6 },
915 definedError("a", ". Allowed unused args must match /c/u"),
916 definedError("b", ". Allowed unused args must match /c/u"),
917 definedError("d", ". Allowed unused args must match /c/u")
921 code
: "(function(a, b, {c, d}) {})",
922 options
: [{ argsIgnorePattern
: "d" }],
923 parserOptions
: { ecmaVersion
: 6 },
925 definedError("a", ". Allowed unused args must match /d/u"),
926 definedError("b", ". Allowed unused args must match /d/u"),
927 definedError("c", ". Allowed unused args must match /d/u")
931 code
: "/*global\rfoo*/",
937 messageId
: "unusedVar",
946 // https://github.com/eslint/eslint/issues/8442
948 code
: "(function ({ a }, b ) { return b; })();",
949 parserOptions
: { ecmaVersion
: 2015 },
955 code
: "(function ({ a }, { b, c } ) { return b; })();",
956 parserOptions
: { ecmaVersion
: 2015 },
963 code
: "(function ({ a, b }, { c } ) { return b; })();",
964 parserOptions
: { ecmaVersion
: 2015 },
971 code
: "(function ([ a ], b ) { return b; })();",
972 parserOptions
: { ecmaVersion
: 2015 },
978 code
: "(function ([ a ], [ b, c ] ) { return b; })();",
979 parserOptions
: { ecmaVersion
: 2015 },
986 code
: "(function ([ a, b ], [ c ] ) { return b; })();",
987 parserOptions
: { ecmaVersion
: 2015 },
994 // https://github.com/eslint/eslint/issues/9774
996 code
: "(function(_a) {})();",
997 options
: [{ args
: "all", varsIgnorePattern
: "^_" }],
998 errors
: [definedError("_a")]
1001 code
: "(function(_a) {})();",
1002 options
: [{ args
: "all", caughtErrorsIgnorePattern
: "^_" }],
1003 errors
: [definedError("_a")]
1006 // https://github.com/eslint/eslint/issues/10982
1008 code
: "var a = function() { a(); };",
1009 errors
: [assignedError("a")]
1012 code
: "var a = function(){ return function() { a(); } };",
1013 errors
: [assignedError("a")]
1016 code
: "const a = () => { a(); };",
1017 parserOptions
: { ecmaVersion
: 2015 },
1018 errors
: [assignedError("a")]
1021 code
: "const a = () => () => { a(); };",
1022 parserOptions
: { ecmaVersion
: 2015 },
1023 errors
: [assignedError("a")]