Skip to content

Commit

Permalink
skip fix on referenced variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanujkanti4441 committed May 4, 2024
1 parent 947d0bd commit 74948f7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 60 deletions.
30 changes: 28 additions & 2 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module.exports = {
messages: {
unusedVar: "'{{varName}}' is {{action}} but never used{{additional}}.",
usedIgnoredVar: "'{{varName}}' is marked as ignored but is used{{additional}}.",
removeVar: "remove unused variable '{{varName}}'."
removeVar: "Remove unused variable '{{varName}}'."
}
},

Expand Down Expand Up @@ -926,6 +926,15 @@ module.exports = {
);
}

/**
* check if given node is forOf or forIn loop
* @param {ASTNode} node node to check
* @returns {boolean} true if node is forOf or forIn loop
*/
function isLoop(node) {
return (node.type === "ForOfStatement" || node.type === "ForInStatement");
}

/**
* Gets fixes in variable declarations and function parameters
* @param {ASTNode} node parent node of identifier
Expand Down Expand Up @@ -971,12 +980,17 @@ module.exports = {
return node.elements.filter(e => e !== null).length === 1;
}

// skip fix when variable is reassigned
if (writeReferences.length > 1 && id.parent.type !== "AssignmentPattern") {
return null;
}

// remove declared variables such as var a; or var a, b;
if (parentType === "VariableDeclarator") {
if (parent.parent.declarations.length === 1) {

// prevent fix of variable in forOf and forIn loops.
if (parent.parent.parent.type === "ForOfStatement" || parent.parent.parent.type === "ForInStatement") {
if (isLoop(parent.parent.parent)) {
return null;
}

Expand All @@ -996,6 +1010,12 @@ module.exports = {

// var {a} = foo;
if (parent.parent.parent.type === "VariableDeclarator") {

// skip variable in for (const { foo } of bar);
if (isLoop(parent.parent.parent.parent.parent)) {
return null;
}

if (parent.parent.parent.parent.declarations.length === 1) {
return fixer.removeRange(parent.parent.parent.parent.range);
}
Expand Down Expand Up @@ -1063,6 +1083,12 @@ module.exports = {

// fix var foo = [a];
if (parent.parent.type === "VariableDeclarator") {

// skip variable in for (const [ foo ] of bar);
if (isLoop(parent.parent.parent.parent)) {
return null;
}

if (parent.parent.parent.declarations.length === 1) {
return fixer.removeRange(parent.parent.parent.range);
}
Expand Down
28 changes: 14 additions & 14 deletions tests/lib/linter/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6363,7 +6363,7 @@ var a = "test2";
data: {
varName: "bbb"
},
desc: "remove unused variable 'bbb'.",
desc: "Remove unused variable 'bbb'.",
fix: {
range: [
99,
Expand Down Expand Up @@ -6406,7 +6406,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
92,
Expand Down Expand Up @@ -6479,7 +6479,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
160,
Expand Down Expand Up @@ -6520,7 +6520,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
21,
Expand Down Expand Up @@ -6576,7 +6576,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
21,
Expand Down Expand Up @@ -6634,7 +6634,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
99,
Expand Down Expand Up @@ -6692,7 +6692,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
102,
Expand Down Expand Up @@ -13045,7 +13045,7 @@ var a = "test2";
data: {
varName: "bbb"
},
desc: "remove unused variable 'bbb'.",
desc: "Remove unused variable 'bbb'.",
fix: {
range: [
99,
Expand Down Expand Up @@ -13088,7 +13088,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
92,
Expand Down Expand Up @@ -13161,7 +13161,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
160,
Expand Down Expand Up @@ -13202,7 +13202,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
21,
Expand Down Expand Up @@ -13258,7 +13258,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
21,
Expand Down Expand Up @@ -13316,7 +13316,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
99,
Expand Down Expand Up @@ -13374,7 +13374,7 @@ var a = "test2";
data: {
varName: "aaa"
},
desc: "remove unused variable 'aaa'.",
desc: "Remove unused variable 'aaa'.",
fix: {
range: [
102,
Expand Down

0 comments on commit 74948f7

Please sign in to comment.