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: Unused recursive function expressions should be flagged (fixes #… #11032

Merged
merged 1 commit into from Nov 9, 2018
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
28 changes: 27 additions & 1 deletion lib/rules/no-unused-vars.js
Expand Up @@ -223,6 +223,32 @@ module.exports = {
return false;
}

/**
* Gets a list of function definitions for a specified variable.
* @param {Variable} variable - eslint-scope variable object.
* @returns {ASTNode[]} Function nodes.
* @private
*/
function getFunctionDefinitions(variable) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest 'map' usage instead of 'forEach':

const getFunctionDefinitions = variable =>
    variable.defs.map(def => {
        const { type, node } = def;
        // FunctionDeclarations
        if (type === "FunctionName") {
            return node;
        }
        // FunctionExpressions
        if (type === "Variable" && node.init &&
            (node.init.type === "FunctionExpression" || node.init.type === "ArrowFunctionExpression")) {
            return node.init;
        }
    }).filter(a => a);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't like to use both map and filter here, but let's wait maintainers' feedback

const functionDefinitions = [];

variable.defs.forEach(def => {
const { type, node } = def;

// FunctionDeclarations
if (type === "FunctionName") {
functionDefinitions.push(node);
}

// FunctionExpressions
if (type === "Variable" && node.init &&
(node.init.type === "FunctionExpression" || node.init.type === "ArrowFunctionExpression")) {
functionDefinitions.push(node.init);
}
});
return functionDefinitions;
}

/**
* Checks the position of given nodes.
*
Expand Down Expand Up @@ -435,7 +461,7 @@ module.exports = {
* @private
*/
function isUsedVariable(variable) {
const functionNodes = variable.defs.filter(def => def.type === "FunctionName").map(def => def.node),
const functionNodes = getFunctionDefinitions(variable),
isFunctionDefinition = functionNodes.length > 0;
let rhsNode = null;

Expand Down
35 changes: 34 additions & 1 deletion tests/lib/rules/no-unused-vars.js
Expand Up @@ -274,8 +274,19 @@ ruleTester.run("no-unused-vars", rule, {
},

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

// https://github.com/eslint/eslint/issues/10982
"var a = function () { a(); }; a();",
"var a = function(){ return function () { a(); } }; a();",
{
code: "const a = () => { a(); }; a();",
parserOptions: { ecmaVersion: 2015 }
},
{
code: "const a = () => () => { a(); }; a();",
parserOptions: { ecmaVersion: 2015 }
}
],
invalid: [
{ code: "function foox() { return foox(); }", errors: [definedError("foox")] },
Expand Down Expand Up @@ -717,6 +728,28 @@ ruleTester.run("no-unused-vars", rule, {
code: "(function(_a) {})();",
options: [{ args: "all", caughtErrorsIgnorePattern: "^_" }],
errors: [{ message: "'_a' is defined but never used." }]
},

// https://github.com/eslint/eslint/issues/10982
{
code: "var a = function() { a(); };",
errors: [{ message: "'a' is assigned a value but never used." }]
},
{
code: "var a = function(){ return function() { a(); } };",
errors: [
{ message: "'a' is assigned a value but never used." }
]
},
{
code: "const a = () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ message: "'a' is assigned a value but never used." }]
},
{
code: "const a = () => () => { a(); };",
parserOptions: { ecmaVersion: 2015 },
errors: [{ message: "'a' is assigned a value but never used." }]
}
]
});