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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-unused-vars ignoreRestSiblings check assignments (fixes #14163) #14264

Merged
merged 3 commits into from Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 15 additions & 10 deletions lib/rules/no-unused-vars.js
Expand Up @@ -196,6 +196,17 @@ module.exports = {

}

/**
* Checks whether a node is a sibling of the rest property or not.
* @param {ASTNode} node a node to check
* @returns {boolean} True if the node is a sibling of the rest property, otherwise false.
*/
function hasRestSibling(node) {
return node.type === "Property" &&
node.parent.type === "ObjectPattern" &&
REST_PROPERTY_TYPE.test(node.parent.properties[node.parent.properties.length - 1].type);
}

/**
* Determines if a variable has a sibling rest property
* @param {Variable} variable eslint-scope variable object.
Expand All @@ -204,16 +215,10 @@ module.exports = {
*/
function hasRestSpreadSibling(variable) {
if (config.ignoreRestSiblings) {
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)
);
});
const hasRestSiblingDefinition = variable.defs.some(def => hasRestSibling(def.name.parent));
const hasRestSiblingReference = variable.references.some(ref => hasRestSibling(ref.identifier.parent));

return hasRestSiblingDefinition || hasRestSiblingReference;
}

return false;
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Expand Up @@ -291,6 +291,13 @@ ruleTester.run("no-unused-vars", rule, {
parserOptions: { ecmaVersion: 2018 }
},

// https://github.com/eslint/eslint/issues/14163
{
code: "let foo, rest;\n({ foo, ...rest } = something);\nconsole.log(rest);",
options: [{ ignoreRestSiblings: true }],
parserOptions: { ecmaVersion: 2020 }
},

// https://github.com/eslint/eslint/issues/10952
"/*eslint use-every-a:1*/ !function(b, a) { return 1 }",

Expand Down Expand Up @@ -588,6 +595,23 @@ ruleTester.run("no-unused-vars", rule, {
}
]
},
{
code: "let type, coords;\n({ type, ...coords } = data);\n console.log(type)",
options: [{ ignoreRestSiblings: true }],
parserOptions: { ecmaVersion: 2018 },
errors: [
{
line: 2,
column: 13,
messageId: "unusedVar",
data: {
varName: "coords",
action: "assigned a value",
additional: ""
}
}
]
},

// Unused rest property without ignoreRestSiblings
{
Expand Down