Skip to content

Commit

Permalink
Fix: ignoreRestSiblings option didn't cover arguments (fixes #8119) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and alberto committed Feb 23, 2017
1 parent 589ab67 commit 8f91e32
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/rules/no-unused-vars.js
Expand Up @@ -66,6 +66,7 @@ module.exports = {

const DEFINED_MESSAGE = "'{{name}}' is defined but never used.";
const ASSIGNED_MESSAGE = "'{{name}}' is assigned a value but never used.";
const REST_PROPERTY_TYPE = /^(?:Experimental)?RestProperty$/;

const config = {
vars: "all",
Expand Down Expand Up @@ -139,17 +140,16 @@ module.exports = {
*/
function hasRestSpreadSibling(variable) {
if (config.ignoreRestSiblings) {
const restProperties = new Set(["ExperimentalRestProperty", "RestProperty"]);

return variable.defs
.filter(def => def.name.type === "Identifier")
.some(def => (
def.node.id &&
def.node.id.type === "ObjectPattern" &&
def.node.id.properties.length &&
restProperties.has(def.node.id.properties[def.node.id.properties.length - 1].type) && // last property is a rest property
!restProperties.has(def.name.parent.type) // variable is sibling of the rest property
));
return variable.defs.some(def => {
const propertyNode = def.name.parent;
const patternNode = propertyNode.parent;

return (
propertyNode.type === "Property" &&
patternNode.type === "ObjectPattern" &&
REST_PROPERTY_TYPE.test(patternNode.properties[patternNode.properties.length - 1].type)
);
});
}

return false;
Expand Down
15 changes: 14 additions & 1 deletion tests/lib/rules/no-unused-vars.js
Expand Up @@ -287,7 +287,13 @@ ruleTester.run("no-unused-vars", rule, {
{
code: "class Foo { set bar(UNUSED) {} } console.log(Foo)",
parserOptions: { ecmaVersion: 6 }
}
},

// https://github.com/eslint/eslint/issues/8119
includeRestPropertyParser({
code: "(({a, ...rest}) => rest)",
options: [{ args: "all", ignoreRestSiblings: true }]
})
],
invalid: [
{ code: "function foox() { return foox(); }", errors: [definedError("foox")] },
Expand Down Expand Up @@ -396,6 +402,13 @@ ruleTester.run("no-unused-vars", rule, {
]
}),

// https://github.com/eslint/eslint/issues/8119
includeRestPropertyParser({
code: "(({a, ...rest}) => {})",
options: [{ args: "all", ignoreRestSiblings: true }],
errors: ["'rest' is defined but never used."]
}),

// https://github.com/eslint/eslint/issues/3714
{
code: "/* global a$fooz,$foo */\na$fooz;",
Expand Down

0 comments on commit 8f91e32

Please sign in to comment.