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

Conversation

sergei-startsev
Copy link
Contributor

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 for FunctionExpression and ArrowFunctionExpression, see corresponding unit tests.

@jsf-clabot
Copy link

jsf-clabot commented Oct 30, 2018

CLA assistant check
All committers have signed the CLA.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Oct 30, 2018
* @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

@platinumazure platinumazure added bug ESLint is working incorrectly rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Oct 30, 2018
Copy link
Member

@platinumazure platinumazure left a 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!

@sergei-startsev
Copy link
Contributor Author

@platinumazure Thanks for the review, I've added a few more tests to be sure that the rule doesn't have false positive issues.

Copy link
Member

@btmills btmills left a 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!

@nzakas nzakas merged commit 9436712 into eslint:master Nov 9, 2018
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators May 9, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label May 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants