Skip to content

Commit f364d47

Browse files
authored
fix: Make no-unused-vars treat for..of loops same as for..in loops (#15868)
1 parent 1324f10 commit f364d47

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

lib/rules/no-unused-vars.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ module.exports = {
484484
}
485485

486486
/**
487-
* Determine if an identifier is used either in for-in loops.
487+
* Determine if an identifier is used either in for-in or for-of loops.
488488
* @param {Reference} ref The reference to check.
489489
* @returns {boolean} whether reference is used in the for-in loops
490490
* @private
491491
*/
492-
function isForInRef(ref) {
492+
function isForInOfRef(ref) {
493493
let target = ref.identifier.parent;
494494

495495

@@ -498,7 +498,7 @@ module.exports = {
498498
target = target.parent.parent;
499499
}
500500

501-
if (target.type !== "ForInStatement") {
501+
if (target.type !== "ForInStatement" && target.type !== "ForOfStatement") {
502502
return false;
503503
}
504504

@@ -531,7 +531,7 @@ module.exports = {
531531
let rhsNode = null;
532532

533533
return variable.references.some(ref => {
534-
if (isForInRef(ref)) {
534+
if (isForInOfRef(ref)) {
535535
return true;
536536
}
537537

tests/lib/rules/no-unused-vars.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ ruleTester.run("no-unused-vars", rule, {
252252
{ code: "(function(obj) { for ( const name in obj ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
253253
{ code: "(function(obj) { for ( const name in obj ) return true })({})", parserOptions: { ecmaVersion: 6 } },
254254

255+
// For-of loops
256+
{ code: "(function(iter) { let name; for ( name of iter ) return; })({});", parserOptions: { ecmaVersion: 6 } },
257+
{ code: "(function(iter) { let name; for ( name of iter ) { return; } })({});", parserOptions: { ecmaVersion: 6 } },
258+
{ code: "(function(iter) { for ( let name of iter ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
259+
{ code: "(function(iter) { for ( let name of iter ) return true })({})", parserOptions: { ecmaVersion: 6 } },
260+
261+
{ code: "(function(iter) { for ( const name of iter ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
262+
{ code: "(function(iter) { for ( const name of iter ) return true })({})", parserOptions: { ecmaVersion: 6 } },
263+
255264
// Sequence Expressions (See https://github.com/eslint/eslint/issues/14325)
256265
{ code: "let x = 0; foo = (0, x++);", parserOptions: { ecmaVersion: 6 } },
257266
{ code: "let x = 0; foo = (0, x += 1);", parserOptions: { ecmaVersion: 6 } },
@@ -704,6 +713,50 @@ ruleTester.run("no-unused-vars", rule, {
704713
}]
705714
},
706715

716+
// For-of loops
717+
{
718+
code: "(function(iter) { var name; for ( name of iter ) { i(); return; } })({});",
719+
env: { es6: true },
720+
errors: [{
721+
line: 1,
722+
column: 35,
723+
messageId: "unusedVar",
724+
data: {
725+
varName: "name",
726+
action: "assigned a value",
727+
additional: ""
728+
}
729+
}]
730+
},
731+
{
732+
code: "(function(iter) { var name; for ( name of iter ) { } })({});",
733+
env: { es6: true },
734+
errors: [{
735+
line: 1,
736+
column: 35,
737+
messageId: "unusedVar",
738+
data: {
739+
varName: "name",
740+
action: "assigned a value",
741+
additional: ""
742+
}
743+
}]
744+
},
745+
{
746+
code: "(function(iter) { for ( var name of iter ) { } })({});",
747+
env: { es6: true },
748+
errors: [{
749+
line: 1,
750+
column: 29,
751+
messageId: "unusedVar",
752+
data: {
753+
varName: "name",
754+
action: "assigned a value",
755+
additional: ""
756+
}
757+
}]
758+
},
759+
707760
// https://github.com/eslint/eslint/issues/3617
708761
{
709762
code: "\n/* global foobar, foo, bar */\nfoobar;",

0 commit comments

Comments
 (0)