]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-unused-vars rule. | |
3 | * @author Ilya Volodin | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-unused-vars"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
f2a92ac6 DC |
21 | ruleTester.defineRule("use-every-a", { |
22 | create(context) { | |
eb39fafa | 23 | |
f2a92ac6 DC |
24 | const sourceCode = context.sourceCode; |
25 | ||
26 | /** | |
27 | * Mark a variable as used | |
28 | * @param {ASTNode} node The node representing the scope to search | |
29 | * @returns {void} | |
30 | * @private | |
31 | */ | |
32 | function useA(node) { | |
33 | sourceCode.markVariableAsUsed("a", node); | |
34 | } | |
35 | return { | |
36 | VariableDeclaration: useA, | |
37 | ReturnStatement: useA | |
38 | }; | |
eb39fafa | 39 | } |
eb39fafa DC |
40 | }); |
41 | ||
42 | /** | |
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 | |
48 | */ | |
49 | function definedError(varName, additional = "", type = "Identifier") { | |
50 | return { | |
51 | messageId: "unusedVar", | |
52 | data: { | |
53 | varName, | |
54 | action: "defined", | |
55 | additional | |
56 | }, | |
57 | type | |
58 | }; | |
59 | } | |
60 | ||
61 | /** | |
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 | |
67 | */ | |
68 | function assignedError(varName, additional = "", type = "Identifier") { | |
69 | return { | |
70 | messageId: "unusedVar", | |
71 | data: { | |
72 | varName, | |
73 | action: "assigned a value", | |
74 | additional | |
75 | }, | |
76 | type | |
77 | }; | |
78 | } | |
79 | ||
80 | ruleTester.run("no-unused-vars", rule, { | |
81 | valid: [ | |
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) {}", | |
106 | "/*global a */ a;", | |
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 } }, | |
152 | ||
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 } }, | |
158 | ||
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 }", | |
163 | ||
164 | // ignore pattern | |
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 } }, | |
8f9d1d4d DC |
170 | { |
171 | code: "const [ a, _b, c ] = items;\nconsole.log(a+c);", | |
172 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
173 | parserOptions: { ecmaVersion: 6 } | |
174 | }, | |
175 | { | |
176 | code: "const [ [a, _b, c] ] = items;\nconsole.log(a+c);", | |
177 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
178 | parserOptions: { ecmaVersion: 6 } | |
179 | }, | |
180 | { | |
181 | code: "const { x: [_a, foo] } = bar;\nconsole.log(foo);", | |
182 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
183 | parserOptions: { ecmaVersion: 6 } | |
184 | }, | |
185 | { | |
186 | code: "function baz([_b, foo]) { foo; };\nbaz()", | |
187 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
188 | parserOptions: { ecmaVersion: 6 } | |
189 | }, | |
190 | { | |
191 | code: "function baz({x: [_b, foo]}) {foo};\nbaz()", | |
192 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
193 | parserOptions: { ecmaVersion: 6 } | |
194 | }, | |
195 | { | |
196 | code: "function baz([{x: [_b, foo]}]) {foo};\nbaz()", | |
197 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
198 | parserOptions: { ecmaVersion: 6 } | |
199 | }, | |
200 | { | |
201 | code: ` | |
202 | let _a, b; | |
203 | foo.forEach(item => { | |
204 | [_a, b] = item; | |
205 | doSomething(b); | |
206 | }); | |
207 | `, | |
208 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
209 | parserOptions: { ecmaVersion: 6 } | |
210 | }, | |
211 | { | |
212 | code: ` | |
213 | // doesn't report _x | |
214 | let _x, y; | |
215 | _x = 1; | |
216 | [_x, y] = foo; | |
217 | y; | |
218 | ||
219 | // doesn't report _a | |
220 | let _a, b; | |
221 | [_a, b] = foo; | |
222 | _a = 1; | |
223 | b; | |
224 | `, | |
225 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
226 | parserOptions: { ecmaVersion: 2018 } | |
227 | }, | |
228 | { | |
229 | code: ` | |
230 | // doesn't report _x | |
231 | let _x, y; | |
232 | _x = 1; | |
233 | [_x, y] = foo; | |
234 | y; | |
235 | ||
236 | // doesn't report _a | |
237 | let _a, b; | |
238 | _a = 1; | |
239 | ({_a, ...b } = foo); | |
240 | b; | |
241 | `, | |
242 | options: [{ destructuredArrayIgnorePattern: "^_", ignoreRestSiblings: true }], | |
243 | parserOptions: { ecmaVersion: 2018 } | |
244 | }, | |
eb39fafa DC |
245 | |
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 })({})", | |
251 | ||
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 } }, | |
256 | ||
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 } }, | |
259 | ||
8f9d1d4d DC |
260 | // For-of loops |
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 } }, | |
265 | ||
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 } }, | |
268 | ||
5422a9cc TL |
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 } }, | |
609c276f | 272 | { code: "let x = 0; foo = (0, x = x + 1);", parserOptions: { ecmaVersion: 6 } }, |
5422a9cc | 273 | |
eb39fafa DC |
274 | // caughtErrors |
275 | { | |
276 | code: "try{}catch(err){console.error(err);}", | |
277 | options: [{ caughtErrors: "all" }] | |
278 | }, | |
279 | { | |
280 | code: "try{}catch(err){}", | |
281 | options: [{ caughtErrors: "none" }] | |
282 | }, | |
283 | { | |
284 | code: "try{}catch(ignoreErr){}", | |
285 | options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^ignore" }] | |
286 | }, | |
287 | ||
288 | // caughtErrors with other combinations | |
289 | { | |
290 | code: "try{}catch(err){}", | |
291 | options: [{ vars: "all", args: "all" }] | |
292 | }, | |
293 | ||
294 | // Using object rest for variable omission | |
295 | { | |
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 } | |
299 | }, | |
300 | ||
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();", | |
308 | ||
309 | // https://github.com/eslint/eslint/issues/6576 | |
310 | [ | |
311 | "var unregisterFooWatcher;", | |
312 | "// ...", | |
313 | "unregisterFooWatcher = $scope.$watch( \"foo\", function() {", | |
314 | " // ...some code..", | |
315 | " unregisterFooWatcher();", | |
316 | "});" | |
317 | ].join("\n"), | |
318 | [ | |
319 | "var ref;", | |
320 | "ref = setInterval(", | |
321 | " function(){", | |
322 | " clearInterval(ref);", | |
323 | " }, 10);" | |
324 | ].join("\n"), | |
325 | [ | |
326 | "var _timer;", | |
327 | "function f() {", | |
328 | " _timer = setTimeout(function () {}, _timer ? 100 : 0);", | |
329 | "}", | |
330 | "f();" | |
331 | ].join("\n"), | |
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();", | |
336 | ||
337 | // https://github.com/eslint/eslint/issues/6646 | |
338 | [ | |
339 | "function someFunction() {", | |
340 | " var a = 0, i;", | |
341 | " for (i = 0; i < 2; i++) {", | |
342 | " a = myFunction(a);", | |
343 | " }", | |
344 | "}", | |
345 | "someFunction();" | |
346 | ].join("\n"), | |
347 | ||
348 | // https://github.com/eslint/eslint/issues/7124 | |
349 | { | |
350 | code: "(function(a, b, {c, d}) { d })", | |
351 | options: [{ argsIgnorePattern: "c" }], | |
352 | parserOptions: { ecmaVersion: 6 } | |
353 | }, | |
354 | { | |
355 | code: "(function(a, b, {c, d}) { c })", | |
356 | options: [{ argsIgnorePattern: "d" }], | |
357 | parserOptions: { ecmaVersion: 6 } | |
358 | }, | |
359 | ||
360 | // https://github.com/eslint/eslint/issues/7250 | |
361 | { | |
362 | code: "(function(a, b, c) { c })", | |
363 | options: [{ argsIgnorePattern: "c" }] | |
364 | }, | |
365 | { | |
366 | code: "(function(a, b, {c, d}) { c })", | |
367 | options: [{ argsIgnorePattern: "[cd]" }], | |
368 | parserOptions: { ecmaVersion: 6 } | |
369 | }, | |
370 | ||
371 | // https://github.com/eslint/eslint/issues/7351 | |
372 | { | |
373 | code: "(class { set foo(UNUSED) {} })", | |
374 | parserOptions: { ecmaVersion: 6 } | |
375 | }, | |
376 | { | |
377 | code: "class Foo { set bar(UNUSED) {} } console.log(Foo)", | |
378 | parserOptions: { ecmaVersion: 6 } | |
379 | }, | |
380 | ||
381 | // https://github.com/eslint/eslint/issues/8119 | |
382 | { | |
383 | code: "(({a, ...rest}) => rest)", | |
384 | options: [{ args: "all", ignoreRestSiblings: true }], | |
385 | parserOptions: { ecmaVersion: 2018 } | |
386 | }, | |
387 | ||
5422a9cc TL |
388 | // https://github.com/eslint/eslint/issues/14163 |
389 | { | |
390 | code: "let foo, rest;\n({ foo, ...rest } = something);\nconsole.log(rest);", | |
391 | options: [{ ignoreRestSiblings: true }], | |
392 | parserOptions: { ecmaVersion: 2020 } | |
393 | }, | |
394 | ||
eb39fafa DC |
395 | // https://github.com/eslint/eslint/issues/10952 |
396 | "/*eslint use-every-a:1*/ !function(b, a) { return 1 }", | |
397 | ||
398 | // https://github.com/eslint/eslint/issues/10982 | |
399 | "var a = function () { a(); }; a();", | |
400 | "var a = function(){ return function () { a(); } }; a();", | |
401 | { | |
402 | code: "const a = () => { a(); }; a();", | |
403 | parserOptions: { ecmaVersion: 2015 } | |
404 | }, | |
405 | { | |
406 | code: "const a = () => () => { a(); }; a();", | |
407 | parserOptions: { ecmaVersion: 2015 } | |
d3726936 TL |
408 | }, |
409 | ||
410 | // export * as ns from "source" | |
411 | { | |
412 | code: 'export * as ns from "source"', | |
413 | parserOptions: { ecmaVersion: 2020, sourceType: "module" } | |
414 | }, | |
415 | ||
416 | // import.meta | |
417 | { | |
418 | code: "import.meta", | |
419 | parserOptions: { ecmaVersion: 2020, sourceType: "module" } | |
eb39fafa DC |
420 | } |
421 | ], | |
422 | invalid: [ | |
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")] }, | |
438 | { | |
439 | code: "function foo() {function foo(x) {\nreturn x; }; return function() {return foo; }; }", | |
440 | errors: [{ | |
441 | messageId: "unusedVar", | |
442 | data: { varName: "foo", action: "defined", additional: "" }, | |
443 | line: 1, | |
444 | type: "Identifier" | |
445 | }] | |
446 | }, | |
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")] }, | |
463 | ||
464 | // exported | |
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")] }, | |
467 | ||
468 | // ignore pattern | |
469 | { | |
470 | code: "var _a; var b;", | |
471 | options: [{ vars: "all", varsIgnorePattern: "^_" }], | |
472 | errors: [{ | |
473 | line: 1, | |
474 | column: 13, | |
475 | messageId: "unusedVar", | |
476 | data: { | |
477 | varName: "b", | |
478 | action: "defined", | |
479 | additional: ". Allowed unused vars must match /^_/u" | |
480 | } | |
481 | }] | |
482 | }, | |
483 | { | |
484 | code: "var a; function foo() { var _b; var c_; } foo();", | |
485 | options: [{ vars: "local", varsIgnorePattern: "^_" }], | |
486 | errors: [{ | |
487 | line: 1, | |
488 | column: 37, | |
489 | messageId: "unusedVar", | |
490 | data: { | |
491 | varName: "c_", | |
492 | action: "defined", | |
493 | additional: ". Allowed unused vars must match /^_/u" | |
494 | } | |
495 | }] | |
496 | }, | |
497 | { | |
498 | code: "function foo(a, _b) { } foo();", | |
499 | options: [{ args: "all", argsIgnorePattern: "^_" }], | |
500 | errors: [{ | |
501 | line: 1, | |
502 | column: 14, | |
503 | messageId: "unusedVar", | |
504 | data: { | |
505 | varName: "a", | |
506 | action: "defined", | |
507 | additional: ". Allowed unused args must match /^_/u" | |
508 | } | |
509 | }] | |
510 | }, | |
511 | { | |
512 | code: "function foo(a, _b, c) { return a; } foo();", | |
513 | options: [{ args: "after-used", argsIgnorePattern: "^_" }], | |
514 | errors: [{ | |
515 | line: 1, | |
516 | column: 21, | |
517 | messageId: "unusedVar", | |
518 | data: { | |
519 | varName: "c", | |
520 | action: "defined", | |
521 | additional: ". Allowed unused args must match /^_/u" | |
522 | } | |
523 | }] | |
524 | }, | |
525 | { | |
526 | code: "function foo(_a) { } foo();", | |
527 | options: [{ args: "all", argsIgnorePattern: "[iI]gnored" }], | |
528 | errors: [{ | |
529 | line: 1, | |
530 | column: 14, | |
531 | messageId: "unusedVar", | |
532 | data: { | |
533 | varName: "_a", | |
534 | action: "defined", | |
535 | additional: ". Allowed unused args must match /[iI]gnored/u" | |
536 | } | |
537 | }] | |
538 | }, | |
539 | { | |
540 | code: "var [ firstItemIgnored, secondItem ] = items;", | |
541 | options: [{ vars: "all", varsIgnorePattern: "[iI]gnored" }], | |
542 | parserOptions: { ecmaVersion: 6 }, | |
543 | errors: [{ | |
544 | line: 1, | |
545 | column: 25, | |
546 | messageId: "unusedVar", | |
547 | data: { | |
548 | varName: "secondItem", | |
549 | action: "assigned a value", | |
550 | additional: ". Allowed unused vars must match /[iI]gnored/u" | |
551 | } | |
552 | }] | |
553 | }, | |
554 | ||
8f9d1d4d DC |
555 | // https://github.com/eslint/eslint/issues/15611 |
556 | { | |
557 | code: ` | |
558 | const array = ['a', 'b', 'c']; | |
559 | const [a, _b, c] = array; | |
560 | const newArray = [a, c]; | |
561 | `, | |
562 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
563 | parserOptions: { ecmaVersion: 2020 }, | |
564 | errors: [ | |
565 | ||
566 | // should report only `newArray` | |
567 | { ...assignedError("newArray"), line: 4, column: 19 } | |
568 | ] | |
569 | }, | |
570 | { | |
571 | code: ` | |
572 | const array = ['a', 'b', 'c', 'd', 'e']; | |
573 | const [a, _b, c] = array; | |
574 | `, | |
575 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
576 | parserOptions: { ecmaVersion: 2020 }, | |
577 | errors: [ | |
578 | { | |
579 | ...assignedError("a", ". Allowed unused elements of array destructuring patterns must match /^_/u"), | |
580 | line: 3, | |
581 | column: 20 | |
582 | }, | |
583 | { | |
584 | ...assignedError("c", ". Allowed unused elements of array destructuring patterns must match /^_/u"), | |
585 | line: 3, | |
586 | column: 27 | |
587 | } | |
588 | ] | |
589 | }, | |
590 | { | |
591 | code: ` | |
592 | const array = ['a', 'b', 'c']; | |
593 | const [a, _b, c] = array; | |
594 | const fooArray = ['foo']; | |
595 | const barArray = ['bar']; | |
596 | const ignoreArray = ['ignore']; | |
597 | `, | |
598 | options: [{ destructuredArrayIgnorePattern: "^_", varsIgnorePattern: "ignore" }], | |
599 | parserOptions: { ecmaVersion: 2020 }, | |
600 | errors: [ | |
601 | { | |
602 | ...assignedError("a", ". Allowed unused elements of array destructuring patterns must match /^_/u"), | |
603 | line: 3, | |
604 | column: 20 | |
605 | }, | |
606 | { | |
607 | ...assignedError("c", ". Allowed unused elements of array destructuring patterns must match /^_/u"), | |
608 | line: 3, | |
609 | column: 27 | |
610 | }, | |
611 | { | |
612 | ...assignedError("fooArray", ". Allowed unused vars must match /ignore/u"), | |
613 | line: 4, | |
614 | column: 19 | |
615 | }, | |
616 | { | |
617 | ...assignedError("barArray", ". Allowed unused vars must match /ignore/u"), | |
618 | line: 5, | |
619 | column: 19 | |
620 | } | |
621 | ] | |
622 | }, | |
623 | { | |
624 | code: ` | |
625 | const array = [obj]; | |
626 | const [{_a, foo}] = array; | |
627 | console.log(foo); | |
628 | `, | |
629 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
630 | parserOptions: { ecmaVersion: 2020 }, | |
631 | errors: [ | |
632 | { | |
633 | ...assignedError("_a"), | |
634 | line: 3, | |
635 | column: 21 | |
636 | } | |
637 | ] | |
638 | }, | |
639 | { | |
640 | code: ` | |
641 | function foo([{_a, bar}]) { | |
642 | bar; | |
643 | } | |
644 | foo(); | |
645 | `, | |
646 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
647 | parserOptions: { ecmaVersion: 2020 }, | |
648 | errors: [ | |
649 | { | |
650 | ...definedError("_a"), | |
651 | line: 2, | |
652 | column: 28 | |
653 | } | |
654 | ] | |
655 | }, | |
656 | { | |
657 | code: ` | |
658 | let _a, b; | |
659 | ||
660 | foo.forEach(item => { | |
661 | [a, b] = item; | |
662 | }); | |
663 | `, | |
664 | options: [{ destructuredArrayIgnorePattern: "^_" }], | |
665 | parserOptions: { ecmaVersion: 2020 }, | |
666 | errors: [ | |
667 | { | |
668 | ...definedError("_a"), | |
669 | line: 2, | |
670 | column: 17 | |
671 | }, | |
672 | { | |
673 | ...assignedError("b"), | |
674 | line: 2, | |
675 | column: 21 | |
676 | } | |
677 | ] | |
678 | }, | |
679 | ||
eb39fafa DC |
680 | // for-in loops (see #2342) |
681 | { | |
682 | code: "(function(obj) { var name; for ( name in obj ) { i(); return; } })({});", | |
683 | errors: [{ | |
684 | line: 1, | |
d3726936 | 685 | column: 34, |
eb39fafa DC |
686 | messageId: "unusedVar", |
687 | data: { | |
688 | varName: "name", | |
689 | action: "assigned a value", | |
690 | additional: "" | |
691 | } | |
692 | }] | |
693 | }, | |
694 | { | |
695 | code: "(function(obj) { var name; for ( name in obj ) { } })({});", | |
696 | errors: [{ | |
697 | line: 1, | |
d3726936 | 698 | column: 34, |
eb39fafa DC |
699 | messageId: "unusedVar", |
700 | data: { | |
701 | varName: "name", | |
702 | action: "assigned a value", | |
703 | additional: "" | |
704 | } | |
705 | }] | |
706 | }, | |
707 | { | |
708 | code: "(function(obj) { for ( var name in obj ) { } })({});", | |
709 | errors: [{ | |
710 | line: 1, | |
711 | column: 28, | |
712 | messageId: "unusedVar", | |
713 | data: { | |
714 | varName: "name", | |
715 | action: "assigned a value", | |
716 | additional: "" | |
717 | } | |
718 | }] | |
719 | }, | |
720 | ||
8f9d1d4d DC |
721 | // For-of loops |
722 | { | |
723 | code: "(function(iter) { var name; for ( name of iter ) { i(); return; } })({});", | |
724 | env: { es6: true }, | |
725 | errors: [{ | |
726 | line: 1, | |
727 | column: 35, | |
728 | messageId: "unusedVar", | |
729 | data: { | |
730 | varName: "name", | |
731 | action: "assigned a value", | |
732 | additional: "" | |
733 | } | |
734 | }] | |
735 | }, | |
736 | { | |
737 | code: "(function(iter) { var name; for ( name of iter ) { } })({});", | |
738 | env: { es6: true }, | |
739 | errors: [{ | |
740 | line: 1, | |
741 | column: 35, | |
742 | messageId: "unusedVar", | |
743 | data: { | |
744 | varName: "name", | |
745 | action: "assigned a value", | |
746 | additional: "" | |
747 | } | |
748 | }] | |
749 | }, | |
750 | { | |
751 | code: "(function(iter) { for ( var name of iter ) { } })({});", | |
752 | env: { es6: true }, | |
753 | errors: [{ | |
754 | line: 1, | |
755 | column: 29, | |
756 | messageId: "unusedVar", | |
757 | data: { | |
758 | varName: "name", | |
759 | action: "assigned a value", | |
760 | additional: "" | |
761 | } | |
762 | }] | |
763 | }, | |
764 | ||
eb39fafa DC |
765 | // https://github.com/eslint/eslint/issues/3617 |
766 | { | |
767 | code: "\n/* global foobar, foo, bar */\nfoobar;", | |
768 | errors: [ | |
769 | { | |
770 | line: 2, | |
771 | endLine: 2, | |
772 | column: 19, | |
773 | endColumn: 22, | |
774 | messageId: "unusedVar", | |
775 | data: { | |
776 | varName: "foo", | |
777 | action: "defined", | |
778 | additional: "" | |
779 | } | |
780 | }, | |
781 | { | |
782 | line: 2, | |
783 | endLine: 2, | |
784 | column: 24, | |
785 | endColumn: 27, | |
786 | messageId: "unusedVar", | |
787 | data: { | |
788 | varName: "bar", | |
789 | action: "defined", | |
790 | additional: "" | |
791 | } | |
792 | } | |
793 | ] | |
794 | }, | |
795 | { | |
796 | code: "\n/* global foobar,\n foo,\n bar\n */\nfoobar;", | |
797 | errors: [ | |
798 | { | |
799 | line: 3, | |
800 | column: 4, | |
801 | endLine: 3, | |
802 | endColumn: 7, | |
803 | messageId: "unusedVar", | |
804 | data: { | |
805 | varName: "foo", | |
806 | action: "defined", | |
807 | additional: "" | |
808 | } | |
809 | }, | |
810 | { | |
811 | line: 4, | |
812 | column: 4, | |
813 | endLine: 4, | |
814 | endColumn: 7, | |
815 | messageId: "unusedVar", | |
816 | data: { | |
817 | varName: "bar", | |
818 | action: "defined", | |
819 | additional: "" | |
820 | } | |
821 | } | |
822 | ] | |
823 | }, | |
824 | ||
825 | // Rest property sibling without ignoreRestSiblings | |
826 | { | |
827 | code: "const data = { type: 'coords', x: 1, y: 2 };\nconst { type, ...coords } = data;\n console.log(coords);", | |
828 | parserOptions: { ecmaVersion: 2018 }, | |
829 | errors: [ | |
830 | { | |
831 | line: 2, | |
832 | column: 9, | |
833 | messageId: "unusedVar", | |
834 | data: { | |
835 | varName: "type", | |
836 | action: "assigned a value", | |
837 | additional: "" | |
838 | } | |
839 | } | |
840 | ] | |
841 | }, | |
842 | ||
843 | // Unused rest property with ignoreRestSiblings | |
844 | { | |
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 }, | |
848 | errors: [ | |
849 | { | |
850 | line: 2, | |
851 | column: 18, | |
852 | messageId: "unusedVar", | |
853 | data: { | |
854 | varName: "coords", | |
855 | action: "assigned a value", | |
856 | additional: "" | |
857 | } | |
858 | } | |
859 | ] | |
860 | }, | |
5422a9cc TL |
861 | { |
862 | code: "let type, coords;\n({ type, ...coords } = data);\n console.log(type)", | |
863 | options: [{ ignoreRestSiblings: true }], | |
864 | parserOptions: { ecmaVersion: 2018 }, | |
865 | errors: [ | |
866 | { | |
867 | line: 2, | |
868 | column: 13, | |
869 | messageId: "unusedVar", | |
870 | data: { | |
871 | varName: "coords", | |
872 | action: "assigned a value", | |
873 | additional: "" | |
874 | } | |
875 | } | |
876 | ] | |
877 | }, | |
eb39fafa DC |
878 | |
879 | // Unused rest property without ignoreRestSiblings | |
880 | { | |
881 | code: "const data = { type: 'coords', x: 3, y: 2 };\nconst { type, ...coords } = data;\n console.log(type)", | |
882 | parserOptions: { ecmaVersion: 2018 }, | |
883 | errors: [ | |
884 | { | |
885 | line: 2, | |
886 | column: 18, | |
887 | messageId: "unusedVar", | |
888 | data: { | |
889 | varName: "coords", | |
890 | action: "assigned a value", | |
891 | additional: "" | |
892 | } | |
893 | } | |
894 | ] | |
895 | }, | |
896 | ||
897 | // Nested array destructuring with rest property | |
898 | { | |
899 | code: "const data = { vars: ['x','y'], x: 1, y: 2 };\nconst { vars: [x], ...coords } = data;\n console.log(coords)", | |
900 | parserOptions: { ecmaVersion: 2018 }, | |
901 | errors: [ | |
902 | { | |
903 | line: 2, | |
904 | column: 16, | |
905 | messageId: "unusedVar", | |
906 | data: { | |
907 | varName: "x", | |
908 | action: "assigned a value", | |
909 | additional: "" | |
910 | } | |
911 | } | |
912 | ] | |
913 | }, | |
914 | ||
915 | // Nested object destructuring with rest property | |
916 | { | |
917 | code: "const data = { defaults: { x: 0 }, x: 1, y: 2 };\nconst { defaults: { x }, ...coords } = data;\n console.log(coords)", | |
918 | parserOptions: { ecmaVersion: 2018 }, | |
919 | errors: [ | |
920 | { | |
921 | line: 2, | |
922 | column: 21, | |
923 | messageId: "unusedVar", | |
924 | data: { | |
925 | varName: "x", | |
926 | action: "assigned a value", | |
927 | additional: "" | |
928 | } | |
929 | } | |
930 | ] | |
931 | }, | |
932 | ||
933 | // https://github.com/eslint/eslint/issues/8119 | |
934 | { | |
935 | code: "(({a, ...rest}) => {})", | |
936 | options: [{ args: "all", ignoreRestSiblings: true }], | |
937 | parserOptions: { ecmaVersion: 2018 }, | |
938 | errors: [definedError("rest")] | |
939 | }, | |
940 | ||
941 | // https://github.com/eslint/eslint/issues/3714 | |
942 | { | |
943 | code: "/* global a$fooz,$foo */\na$fooz;", | |
944 | errors: [ | |
945 | { | |
946 | line: 1, | |
947 | column: 18, | |
948 | endLine: 1, | |
949 | endColumn: 22, | |
950 | messageId: "unusedVar", | |
951 | data: { | |
952 | varName: "$foo", | |
953 | action: "defined", | |
954 | additional: "" | |
955 | } | |
956 | } | |
957 | ] | |
958 | }, | |
959 | { | |
960 | code: "/* globals a$fooz, $ */\na$fooz;", | |
961 | errors: [ | |
962 | { | |
963 | line: 1, | |
964 | column: 20, | |
965 | endLine: 1, | |
966 | endColumn: 21, | |
967 | messageId: "unusedVar", | |
968 | data: { | |
969 | varName: "$", | |
970 | action: "defined", | |
971 | additional: "" | |
972 | } | |
973 | } | |
974 | ] | |
975 | }, | |
976 | { | |
977 | code: "/*globals $foo*/", | |
978 | errors: [ | |
979 | { | |
980 | line: 1, | |
981 | column: 11, | |
982 | endLine: 1, | |
983 | endColumn: 15, | |
984 | messageId: "unusedVar", | |
985 | data: { | |
986 | varName: "$foo", | |
987 | action: "defined", | |
988 | additional: "" | |
989 | } | |
990 | } | |
991 | ] | |
992 | }, | |
993 | { | |
994 | code: "/* global global*/", | |
995 | errors: [ | |
996 | { | |
997 | line: 1, | |
998 | column: 11, | |
999 | endLine: 1, | |
1000 | endColumn: 17, | |
1001 | messageId: "unusedVar", | |
1002 | data: { | |
1003 | varName: "global", | |
1004 | action: "defined", | |
1005 | additional: "" | |
1006 | } | |
1007 | } | |
1008 | ] | |
1009 | }, | |
1010 | { | |
1011 | code: "/*global foo:true*/", | |
1012 | errors: [ | |
1013 | { | |
1014 | line: 1, | |
1015 | column: 10, | |
1016 | endLine: 1, | |
1017 | endColumn: 13, | |
1018 | messageId: "unusedVar", | |
1019 | data: { | |
1020 | varName: "foo", | |
1021 | action: "defined", | |
1022 | additional: "" | |
1023 | } | |
1024 | } | |
1025 | ] | |
1026 | }, | |
1027 | ||
1028 | // non ascii. | |
1029 | { | |
1030 | code: "/*global 変数, 数*/\n変数;", | |
1031 | errors: [ | |
1032 | { | |
1033 | line: 1, | |
1034 | column: 14, | |
1035 | endLine: 1, | |
1036 | endColumn: 15, | |
1037 | messageId: "unusedVar", | |
1038 | data: { | |
1039 | varName: "æ•°", | |
1040 | action: "defined", | |
1041 | additional: "" | |
1042 | } | |
1043 | } | |
1044 | ] | |
1045 | }, | |
1046 | ||
1047 | // surrogate pair. | |
1048 | { | |
1049 | code: "/*global 𠮷𩸽, 𠮷*/\n\\u{20BB7}\\u{29E3D};", | |
1050 | env: { es6: true }, | |
1051 | errors: [ | |
1052 | { | |
1053 | line: 1, | |
1054 | column: 16, | |
1055 | endLine: 1, | |
1056 | endColumn: 18, | |
1057 | messageId: "unusedVar", | |
1058 | data: { | |
1059 | varName: "ð ®·", | |
1060 | action: "defined", | |
1061 | additional: "" | |
1062 | } | |
1063 | } | |
1064 | ] | |
1065 | }, | |
1066 | ||
1067 | // https://github.com/eslint/eslint/issues/4047 | |
1068 | { | |
1069 | code: "export default function(a) {}", | |
1070 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1071 | errors: [definedError("a")] | |
1072 | }, | |
1073 | { | |
1074 | code: "export default function(a, b) { console.log(a); }", | |
1075 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1076 | errors: [definedError("b")] | |
1077 | }, | |
1078 | { | |
1079 | code: "export default (function(a) {});", | |
1080 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1081 | errors: [definedError("a")] | |
1082 | }, | |
1083 | { | |
1084 | code: "export default (function(a, b) { console.log(a); });", | |
1085 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1086 | errors: [definedError("b")] | |
1087 | }, | |
1088 | { | |
1089 | code: "export default (a) => {};", | |
1090 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1091 | errors: [definedError("a")] | |
1092 | }, | |
1093 | { | |
1094 | code: "export default (a, b) => { console.log(a); };", | |
1095 | parserOptions: { ecmaVersion: 6, sourceType: "module" }, | |
1096 | errors: [definedError("b")] | |
1097 | }, | |
1098 | ||
1099 | // caughtErrors | |
1100 | { | |
1101 | code: "try{}catch(err){};", | |
1102 | options: [{ caughtErrors: "all" }], | |
1103 | errors: [definedError("err")] | |
1104 | }, | |
1105 | { | |
1106 | code: "try{}catch(err){};", | |
1107 | options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^ignore" }], | |
1108 | errors: [definedError("err", ". Allowed unused args must match /^ignore/u")] | |
1109 | }, | |
1110 | ||
1111 | // multiple try catch with one success | |
1112 | { | |
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")] | |
1116 | }, | |
1117 | ||
1118 | // multiple try catch both fail | |
1119 | { | |
1120 | code: "try{}catch(error){}try{}catch(err){};", | |
1121 | options: [{ caughtErrors: "all", caughtErrorsIgnorePattern: "^ignore" }], | |
1122 | errors: [ | |
1123 | definedError("error", ". Allowed unused args must match /^ignore/u"), | |
1124 | definedError("err", ". Allowed unused args must match /^ignore/u") | |
1125 | ] | |
1126 | }, | |
1127 | ||
1128 | // caughtErrors with other configs | |
1129 | { | |
1130 | code: "try{}catch(err){};", | |
1131 | options: [{ vars: "all", args: "all", caughtErrors: "all" }], | |
1132 | errors: [definedError("err")] | |
1133 | }, | |
1134 | ||
1135 | // no conflict in ignore patterns | |
1136 | { | |
1137 | code: "try{}catch(err){};", | |
1138 | options: [ | |
1139 | { | |
1140 | vars: "all", | |
1141 | args: "all", | |
1142 | caughtErrors: "all", | |
1143 | argsIgnorePattern: "^er" | |
1144 | } | |
1145 | ], | |
1146 | errors: [definedError("err")] | |
1147 | }, | |
1148 | ||
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")] }, | |
1159 | ||
1160 | // https://github.com/eslint/eslint/issues/6576 (For coverage) | |
1161 | { | |
1162 | code: "function foo(cb) { cb = function(a) { cb(1 + a); }; bar(not_cb); } foo();", | |
1163 | errors: [assignedError("cb")] | |
1164 | }, | |
1165 | { | |
1166 | code: "function foo(cb) { cb = function(a) { return cb(1 + a); }(); } foo();", | |
1167 | errors: [assignedError("cb")] | |
1168 | }, | |
1169 | { | |
1170 | code: "function foo(cb) { cb = (function(a) { cb(1 + a); }, cb); } foo();", | |
1171 | errors: [assignedError("cb")] | |
1172 | }, | |
1173 | { | |
1174 | code: "function foo(cb) { cb = (0, function(a) { cb(1 + a); }); } foo();", | |
1175 | errors: [assignedError("cb")] | |
1176 | }, | |
1177 | ||
1178 | // https://github.com/eslint/eslint/issues/6646 | |
1179 | { | |
1180 | code: [ | |
1181 | "while (a) {", | |
1182 | " function foo(b) {", | |
1183 | " b = b + 1;", | |
1184 | " }", | |
1185 | " foo()", | |
1186 | "}" | |
1187 | ].join("\n"), | |
1188 | errors: [assignedError("b")] | |
1189 | }, | |
1190 | ||
1191 | // https://github.com/eslint/eslint/issues/7124 | |
1192 | { | |
1193 | code: "(function(a, b, c) {})", | |
1194 | options: [{ argsIgnorePattern: "c" }], | |
1195 | errors: [ | |
1196 | definedError("a", ". Allowed unused args must match /c/u"), | |
1197 | definedError("b", ". Allowed unused args must match /c/u") | |
1198 | ] | |
1199 | }, | |
1200 | { | |
1201 | code: "(function(a, b, {c, d}) {})", | |
1202 | options: [{ argsIgnorePattern: "[cd]" }], | |
1203 | parserOptions: { ecmaVersion: 6 }, | |
1204 | errors: [ | |
1205 | definedError("a", ". Allowed unused args must match /[cd]/u"), | |
1206 | definedError("b", ". Allowed unused args must match /[cd]/u") | |
1207 | ] | |
1208 | }, | |
1209 | { | |
1210 | code: "(function(a, b, {c, d}) {})", | |
1211 | options: [{ argsIgnorePattern: "c" }], | |
1212 | parserOptions: { ecmaVersion: 6 }, | |
1213 | errors: [ | |
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") | |
1217 | ] | |
1218 | }, | |
1219 | { | |
1220 | code: "(function(a, b, {c, d}) {})", | |
1221 | options: [{ argsIgnorePattern: "d" }], | |
1222 | parserOptions: { ecmaVersion: 6 }, | |
1223 | errors: [ | |
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") | |
1227 | ] | |
1228 | }, | |
1229 | { | |
1230 | code: "/*global\rfoo*/", | |
1231 | errors: [{ | |
1232 | line: 2, | |
1233 | column: 1, | |
1234 | endLine: 2, | |
1235 | endColumn: 4, | |
1236 | messageId: "unusedVar", | |
1237 | data: { | |
1238 | varName: "foo", | |
1239 | action: "defined", | |
1240 | additional: "" | |
1241 | } | |
1242 | }] | |
1243 | }, | |
1244 | ||
1245 | // https://github.com/eslint/eslint/issues/8442 | |
1246 | { | |
1247 | code: "(function ({ a }, b ) { return b; })();", | |
1248 | parserOptions: { ecmaVersion: 2015 }, | |
1249 | errors: [ | |
1250 | definedError("a") | |
1251 | ] | |
1252 | }, | |
1253 | { | |
1254 | code: "(function ({ a }, { b, c } ) { return b; })();", | |
1255 | parserOptions: { ecmaVersion: 2015 }, | |
1256 | errors: [ | |
1257 | definedError("a"), | |
1258 | definedError("c") | |
1259 | ] | |
1260 | }, | |
5422a9cc TL |
1261 | |
1262 | // https://github.com/eslint/eslint/issues/14325 | |
1263 | { | |
1264 | code: `let x = 0; | |
1265 | x++, x = 0;`, | |
1266 | parserOptions: { ecmaVersion: 2015 }, | |
1267 | errors: [{ ...assignedError("x"), line: 2, column: 18 }] | |
1268 | }, | |
1269 | { | |
1270 | code: `let x = 0; | |
1271 | x++, x = 0; | |
1272 | x=3;`, | |
1273 | parserOptions: { ecmaVersion: 2015 }, | |
1274 | errors: [{ ...assignedError("x"), line: 3, column: 13 }] | |
1275 | }, | |
1276 | { | |
1277 | code: "let x = 0; x++, 0;", | |
1278 | parserOptions: { ecmaVersion: 2015 }, | |
1279 | errors: [{ ...assignedError("x"), line: 1, column: 12 }] | |
1280 | }, | |
1281 | { | |
1282 | code: "let x = 0; 0, x++;", | |
1283 | parserOptions: { ecmaVersion: 2015 }, | |
1284 | errors: [{ ...assignedError("x"), line: 1, column: 15 }] | |
1285 | }, | |
1286 | { | |
1287 | code: "let x = 0; 0, (1, x++);", | |
1288 | parserOptions: { ecmaVersion: 2015 }, | |
1289 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1290 | }, | |
1291 | { | |
1292 | code: "let x = 0; foo = (x++, 0);", | |
1293 | parserOptions: { ecmaVersion: 2015 }, | |
1294 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1295 | }, | |
1296 | { | |
1297 | code: "let x = 0; foo = ((0, x++), 0);", | |
1298 | parserOptions: { ecmaVersion: 2015 }, | |
1299 | errors: [{ ...assignedError("x"), line: 1, column: 23 }] | |
1300 | }, | |
1301 | { | |
1302 | code: "let x = 0; x += 1, 0;", | |
1303 | parserOptions: { ecmaVersion: 2015 }, | |
1304 | errors: [{ ...assignedError("x"), line: 1, column: 12 }] | |
1305 | }, | |
1306 | { | |
1307 | code: "let x = 0; 0, x += 1;", | |
1308 | parserOptions: { ecmaVersion: 2015 }, | |
1309 | errors: [{ ...assignedError("x"), line: 1, column: 15 }] | |
1310 | }, | |
1311 | { | |
1312 | code: "let x = 0; 0, (1, x += 1);", | |
1313 | parserOptions: { ecmaVersion: 2015 }, | |
1314 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1315 | }, | |
1316 | { | |
1317 | code: "let x = 0; foo = (x += 1, 0);", | |
1318 | parserOptions: { ecmaVersion: 2015 }, | |
1319 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1320 | }, | |
1321 | { | |
1322 | code: "let x = 0; foo = ((0, x += 1), 0);", | |
1323 | parserOptions: { ecmaVersion: 2015 }, | |
1324 | errors: [{ ...assignedError("x"), line: 1, column: 23 }] | |
1325 | }, | |
609c276f TL |
1326 | |
1327 | // https://github.com/eslint/eslint/issues/14866 | |
1328 | { | |
1329 | code: `let z = 0; | |
1330 | z = z + 1, z = 2; | |
1331 | `, | |
1332 | parserOptions: { ecmaVersion: 2020 }, | |
1333 | errors: [{ ...assignedError("z"), line: 2, column: 24 }] | |
1334 | }, | |
1335 | { | |
1336 | code: `let z = 0; | |
1337 | z = z+1, z = 2; | |
1338 | z = 3;`, | |
1339 | parserOptions: { ecmaVersion: 2020 }, | |
1340 | errors: [{ ...assignedError("z"), line: 3, column: 13 }] | |
1341 | }, | |
1342 | { | |
1343 | code: `let z = 0; | |
1344 | z = z+1, z = 2; | |
1345 | z = z+3; | |
1346 | `, | |
1347 | parserOptions: { ecmaVersion: 2020 }, | |
1348 | errors: [{ ...assignedError("z"), line: 3, column: 13 }] | |
1349 | }, | |
1350 | { | |
1351 | code: "let x = 0; 0, x = x+1;", | |
1352 | parserOptions: { ecmaVersion: 2020 }, | |
1353 | errors: [{ ...assignedError("x"), line: 1, column: 15 }] | |
1354 | }, | |
1355 | { | |
1356 | code: "let x = 0; x = x+1, 0;", | |
1357 | parserOptions: { ecmaVersion: 2020 }, | |
1358 | errors: [{ ...assignedError("x"), line: 1, column: 12 }] | |
1359 | }, | |
1360 | { | |
1361 | code: "let x = 0; foo = ((0, x = x + 1), 0);", | |
1362 | parserOptions: { ecmaVersion: 2020 }, | |
1363 | errors: [{ ...assignedError("x"), line: 1, column: 23 }] | |
1364 | }, | |
1365 | { | |
1366 | code: "let x = 0; foo = (x = x+1, 0);", | |
1367 | parserOptions: { ecmaVersion: 2020 }, | |
1368 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1369 | }, | |
1370 | { | |
1371 | code: "let x = 0; 0, (1, x=x+1);", | |
1372 | parserOptions: { ecmaVersion: 2020 }, | |
1373 | errors: [{ ...assignedError("x"), line: 1, column: 19 }] | |
1374 | }, | |
eb39fafa DC |
1375 | { |
1376 | code: "(function ({ a, b }, { c } ) { return b; })();", | |
1377 | parserOptions: { ecmaVersion: 2015 }, | |
1378 | errors: [ | |
1379 | definedError("a"), | |
1380 | definedError("c") | |
1381 | ] | |
1382 | }, | |
1383 | { | |
1384 | code: "(function ([ a ], b ) { return b; })();", | |
1385 | parserOptions: { ecmaVersion: 2015 }, | |
1386 | errors: [ | |
1387 | definedError("a") | |
1388 | ] | |
1389 | }, | |
1390 | { | |
1391 | code: "(function ([ a ], [ b, c ] ) { return b; })();", | |
1392 | parserOptions: { ecmaVersion: 2015 }, | |
1393 | errors: [ | |
1394 | definedError("a"), | |
1395 | definedError("c") | |
1396 | ] | |
1397 | }, | |
1398 | { | |
1399 | code: "(function ([ a, b ], [ c ] ) { return b; })();", | |
1400 | parserOptions: { ecmaVersion: 2015 }, | |
1401 | errors: [ | |
1402 | definedError("a"), | |
1403 | definedError("c") | |
1404 | ] | |
1405 | }, | |
1406 | ||
1407 | // https://github.com/eslint/eslint/issues/9774 | |
1408 | { | |
1409 | code: "(function(_a) {})();", | |
1410 | options: [{ args: "all", varsIgnorePattern: "^_" }], | |
1411 | errors: [definedError("_a")] | |
1412 | }, | |
1413 | { | |
1414 | code: "(function(_a) {})();", | |
1415 | options: [{ args: "all", caughtErrorsIgnorePattern: "^_" }], | |
1416 | errors: [definedError("_a")] | |
1417 | }, | |
1418 | ||
1419 | // https://github.com/eslint/eslint/issues/10982 | |
1420 | { | |
1421 | code: "var a = function() { a(); };", | |
1422 | errors: [assignedError("a")] | |
1423 | }, | |
1424 | { | |
1425 | code: "var a = function(){ return function() { a(); } };", | |
1426 | errors: [assignedError("a")] | |
1427 | }, | |
1428 | { | |
1429 | code: "const a = () => { a(); };", | |
1430 | parserOptions: { ecmaVersion: 2015 }, | |
1431 | errors: [assignedError("a")] | |
1432 | }, | |
1433 | { | |
1434 | code: "const a = () => () => { a(); };", | |
1435 | parserOptions: { ecmaVersion: 2015 }, | |
1436 | errors: [assignedError("a")] | |
d3726936 TL |
1437 | }, |
1438 | { | |
1439 | code: `let myArray = [1,2,3,4].filter((x) => x == 0); | |
1440 | myArray = myArray.filter((x) => x == 1);`, | |
1441 | parserOptions: { ecmaVersion: 2015 }, | |
5422a9cc | 1442 | errors: [{ ...assignedError("myArray"), line: 2, column: 5 }] |
d3726936 TL |
1443 | }, |
1444 | { | |
1445 | code: "const a = 1; a += 1;", | |
1446 | parserOptions: { ecmaVersion: 2015 }, | |
1447 | errors: [{ ...assignedError("a"), line: 1, column: 14 }] | |
1448 | }, | |
1449 | { | |
1450 | code: "var a = function() { a(); };", | |
5422a9cc | 1451 | errors: [{ ...assignedError("a"), line: 1, column: 5 }] |
d3726936 TL |
1452 | }, |
1453 | { | |
1454 | code: "var a = function(){ return function() { a(); } };", | |
5422a9cc | 1455 | errors: [{ ...assignedError("a"), line: 1, column: 5 }] |
d3726936 TL |
1456 | }, |
1457 | { | |
1458 | code: "const a = () => { a(); };", | |
1459 | parserOptions: { ecmaVersion: 2015 }, | |
5422a9cc | 1460 | errors: [{ ...assignedError("a"), line: 1, column: 7 }] |
d3726936 TL |
1461 | }, |
1462 | { | |
1463 | code: "const a = () => () => { a(); };", | |
1464 | parserOptions: { ecmaVersion: 2015 }, | |
5422a9cc TL |
1465 | errors: [{ ...assignedError("a"), line: 1, column: 7 }] |
1466 | }, | |
1467 | ||
1468 | // https://github.com/eslint/eslint/issues/14324 | |
1469 | { | |
1470 | code: "let x = [];\nx = x.concat(x);", | |
1471 | parserOptions: { ecmaVersion: 2015 }, | |
1472 | errors: [{ ...assignedError("x"), line: 2, column: 1 }] | |
d3726936 TL |
1473 | }, |
1474 | { | |
1475 | ||
1476 | code: `let a = 'a'; | |
1477 | a = 10; | |
1478 | function foo(){ | |
1479 | a = 11; | |
1480 | a = () => { | |
1481 | a = 13 | |
1482 | } | |
1483 | }`, | |
1484 | parserOptions: { ecmaVersion: 2020 }, | |
5422a9cc TL |
1485 | errors: [{ ...assignedError("a"), line: 2, column: 13 }, { ...definedError("foo"), line: 3, column: 22 }] |
1486 | }, | |
1487 | { | |
1488 | code: `let foo; | |
1489 | init(); | |
1490 | foo = foo + 2; | |
1491 | function init() { | |
1492 | foo = 1; | |
1493 | }`, | |
1494 | parserOptions: { ecmaVersion: 2020 }, | |
1495 | errors: [{ ...assignedError("foo"), line: 3, column: 13 }] | |
1496 | }, | |
1497 | { | |
1498 | code: `function foo(n) { | |
1499 | if (n < 2) return 1; | |
1500 | return n * foo(n - 1); | |
1501 | }`, | |
1502 | parserOptions: { ecmaVersion: 2020 }, | |
1503 | errors: [{ ...definedError("foo"), line: 1, column: 10 }] | |
d3726936 TL |
1504 | }, |
1505 | { | |
1506 | code: `let c = 'c' | |
1507 | c = 10 | |
1508 | function foo1() { | |
1509 | c = 11 | |
1510 | c = () => { | |
1511 | c = 13 | |
1512 | } | |
1513 | } | |
1514 | ||
1515 | c = foo1`, | |
1516 | parserOptions: { ecmaVersion: 2020 }, | |
1517 | errors: [{ ...assignedError("c"), line: 10, column: 1 }] | |
eb39fafa DC |
1518 | } |
1519 | ] | |
1520 | }); |