Skip to content

Commit

Permalink
Fix: Unused recursive function expressions (fixes #10982) (#11032)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-startsev authored and nzakas committed Nov 9, 2018
1 parent c832cd5 commit 9436712
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/rules/no-unused-vars.js
Expand Up @@ -225,6 +225,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) {
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 @@ -433,7 +459,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." }]
}
]
});

0 comments on commit 9436712

Please sign in to comment.