Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make no-unused-var function the same way in for..of loops as for..in loops #15868

Merged
merged 1 commit into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/rules/no-unused-vars.js
Expand Up @@ -484,12 +484,12 @@ module.exports = {
}

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


Expand All @@ -498,7 +498,7 @@ module.exports = {
target = target.parent.parent;
}

if (target.type !== "ForInStatement") {
if (target.type !== "ForInStatement" && target.type !== "ForOfStatement") {
return false;
}

Expand Down Expand Up @@ -531,7 +531,7 @@ module.exports = {
let rhsNode = null;

return variable.references.some(ref => {
if (isForInRef(ref)) {
if (isForInOfRef(ref)) {
return true;
}

Expand Down
53 changes: 53 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -252,6 +252,15 @@ ruleTester.run("no-unused-vars", rule, {
{ code: "(function(obj) { for ( const name in obj ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
{ code: "(function(obj) { for ( const name in obj ) return true })({})", parserOptions: { ecmaVersion: 6 } },

// For-of loops
{ code: "(function(iter) { let name; for ( name of iter ) return; })({});", parserOptions: { ecmaVersion: 6 } },
{ code: "(function(iter) { let name; for ( name of iter ) { return; } })({});", parserOptions: { ecmaVersion: 6 } },
{ code: "(function(iter) { for ( let name of iter ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
{ code: "(function(iter) { for ( let name of iter ) return true })({})", parserOptions: { ecmaVersion: 6 } },

{ code: "(function(iter) { for ( const name of iter ) { return true } })({})", parserOptions: { ecmaVersion: 6 } },
{ code: "(function(iter) { for ( const name of iter ) return true })({})", parserOptions: { ecmaVersion: 6 } },

// Sequence Expressions (See https://github.com/eslint/eslint/issues/14325)
{ code: "let x = 0; foo = (0, x++);", parserOptions: { ecmaVersion: 6 } },
{ code: "let x = 0; foo = (0, x += 1);", parserOptions: { ecmaVersion: 6 } },
Expand Down Expand Up @@ -704,6 +713,50 @@ ruleTester.run("no-unused-vars", rule, {
}]
},

// For-of loops
{
code: "(function(iter) { var name; for ( name of iter ) { i(); return; } })({});",
env: { es6: true },
errors: [{
line: 1,
column: 35,
messageId: "unusedVar",
data: {
varName: "name",
action: "assigned a value",
additional: ""
}
}]
},
{
code: "(function(iter) { var name; for ( name of iter ) { } })({});",
env: { es6: true },
errors: [{
line: 1,
column: 35,
messageId: "unusedVar",
data: {
varName: "name",
action: "assigned a value",
additional: ""
}
}]
},
{
code: "(function(iter) { for ( var name of iter ) { } })({});",
env: { es6: true },
errors: [{
line: 1,
column: 29,
messageId: "unusedVar",
data: {
varName: "name",
action: "assigned a value",
additional: ""
}
}]
},

// https://github.com/eslint/eslint/issues/3617
{
code: "\n/* global foobar, foo, bar */\nfoobar;",
Expand Down