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
Conversation
03e2981
to
8096f33
Compare
* @returns {ASTNode[]} Function nodes. | ||
* @private | ||
*/ | ||
function getFunctionDefinitions(variable) { |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sergei-startsev, thanks for the PR!
I don't have strong feelings on forEach vs map/filter (although I'll say I have no problem with map then filter).
Are there any test cases that show a recursive function not triggering this rule if it's initially called from the outside? Something like: function a() { a(); } a();
If any tests like that are missing, please add them. Thanks!
8096f33
to
2f1bfed
Compare
@platinumazure Thanks for the review, I've added a few more tests to be sure that the rule doesn't have false positive issues. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @sergei-startsev!
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
#10982
What changes did you make? (Give an overview)
Added
getFunctionDefinitions
to get a list of function definitions for a passed variable, added checks forFunctionExpression
andArrowFunctionExpression
, see corresponding unit tests.