Skip to content

Commit

Permalink
Fix: fix no-unused-vars (fixes #44) (#45)
Browse files Browse the repository at this point in the history
* Fix: fix no-unused-vars (fixes #44)

* add more cases

* support array destructuring

* add more cases
  • Loading branch information
g-plane authored Jun 9, 2019
1 parent b716803 commit eb118f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
7 changes: 2 additions & 5 deletions lib/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

const anyFunctionPattern = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/u;
const sideEffectFree = new Set(["Literal", "Identifier", "ThisExpression"]);

/**
* Finds a function node from ancestors of a node.
Expand All @@ -28,7 +29,7 @@ function getUpperFunction(node) {
* @returns {boolean} result
*/
function hasSideEffect(node) {
if (["Literal", "Identifier", "ThisExpression"].includes(node.type)) {
if (sideEffectFree.has(node.type)) {
return false;
}

Expand All @@ -40,10 +41,6 @@ function hasSideEffect(node) {
return node.expressions.length !== 0;
}

if (node.type === "AssignmentExpression") {
return hasSideEffect(node.right);
}

return true;
}

Expand Down
36 changes: 30 additions & 6 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { hasSideEffect } = require("../ast-utils");

const rule = utils.getFixableRule("no-unused-vars", false);

const commaFilter = { filter: token => token.value === "," };

module.exports = ruleComposer.mapReports(
rule,
(problem, { sourceCode }) => {
Expand All @@ -35,20 +37,20 @@ module.exports = ruleComposer.mapReports(
}

if (parent !== grand.specifiers[grand.specifiers.length - 1]) {
const comma = sourceCode.getTokenAfter(parent, { filter: token => token.value === "," });
const comma = sourceCode.getTokenAfter(parent, commaFilter);

return [fixer.remove(parent), fixer.remove(comma)];
}

if (grand.specifiers.filter(specifier => specifier.type === "ImportSpecifier").length === 1) {
const start = sourceCode.getTokenBefore(parent, { filter: token => token.value === "," }),
const start = sourceCode.getTokenBefore(parent, commaFilter),
end = sourceCode.getTokenAfter(parent, { filter: token => token.value === "}" });

return fixer.removeRange([start.range[0], end.range[1]]);
}

return fixer.removeRange([
sourceCode.getTokenBefore(parent, { filter: token => token.value === "," }).range[0],
sourceCode.getTokenBefore(parent, commaFilter).range[0],
parent.range[1]
]);
case "VariableDeclarator":
Expand All @@ -65,13 +67,13 @@ module.exports = ruleComposer.mapReports(
}

if (parent !== grand.declarations[grand.declarations.length - 1]) {
const comma = sourceCode.getTokenAfter(parent, { filter: token => token.value === "," });
const comma = sourceCode.getTokenAfter(parent, commaFilter);

return [fixer.remove(parent), fixer.remove(comma)];
}

return [
fixer.remove(sourceCode.getTokenBefore(parent, { filter: token => token.value === "," })),
fixer.remove(sourceCode.getTokenBefore(parent, commaFilter)),
fixer.remove(parent)
];
case "AssignmentPattern":
Expand All @@ -81,7 +83,29 @@ module.exports = ruleComposer.mapReports(
return fixer.remove(parent);
case "RestElement":
case "Property":
return fixer.remove(parent);
if (!grand) {
return null;
}

if (grand.properties.length === 1) {
const identifierRemoval = fixer.remove(parent);
const comma = sourceCode.getLastToken(grand, commaFilter);

return comma ? [identifierRemoval, fixer.remove(comma)] : identifierRemoval;
}

if (parent === grand.properties[grand.properties.length - 1]) {
const comma = sourceCode.getTokenBefore(parent, commaFilter);

return [fixer.remove(parent), fixer.remove(comma)];
}

return [
fixer.remove(parent),
fixer.remove(sourceCode.getTokenAfter(parent, commaFilter))
];
case "ArrayPattern":
return fixer.remove(node);
default:
return null;
}
Expand Down
48 changes: 45 additions & 3 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ ruleTester.run("no-unused-vars", rule, {
},
{
code: "var a = b = c",
output: "",
output: null,
errors: [{ type: "Identifier" }]
},
{
code: "var a = b = c()",
output: null,
errors: [{ type: "Identifier" }]
},
{
Expand All @@ -142,8 +147,27 @@ ruleTester.run("no-unused-vars", rule, {
errors: [{ type: "Identifier" }]
},
{
code: "var a = b = c()",
output: null,
code: "var [a] = c",
output: "var [] = c",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "var [a, b] = c",
output: "var [, ] = c",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }, { type: "Identifier" }]
},
{
code: "var [a, b] = c; a;",
output: "var [a, ] = c; a;",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "var [a, b] = c; b;",
output: "var [, b] = c; b;",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
Expand All @@ -168,6 +192,12 @@ ruleTester.run("no-unused-vars", rule, {
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "let {a,} = b",
output: "let {} = b",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "let {a1: a2} = b",
output: "let {} = b",
Expand All @@ -185,6 +215,18 @@ ruleTester.run("no-unused-vars", rule, {
output: null,
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "let {a, b} = c; b;",
output: "let { b} = c; b;",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
},
{
code: "let {a, b} = c; a;",
output: "let {a } = c; a;",
parserOptions: { ecmaVersion: 6 },
errors: [{ type: "Identifier" }]
}
]
});

0 comments on commit eb118f1

Please sign in to comment.