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

"no-unused-vars" doesn't work with recursive function correctly, when it is declared with function expression #10982

Closed
aliaksandr-yermalayeu opened this issue Oct 15, 2018 · 5 comments

Comments

@aliaksandr-yermalayeu
Copy link

commented Oct 15, 2018

Tell us about your environment

  • ESLint Version: 5.7.0
  • Node Version: 8.11.1
  • npm Version: 5.6.0

What parser (default, Babel-ESLint, etc.) are you using?
default

Please show your full configuration:

{
    "rules": {
        "no-unused-vars": "error"
    }
}
Configuration
{
    "rules": {
        "no-unused-vars": "error"
    }
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

function declaration

// considered error
function a () {
    a();
}

function expression

// error is not happened
var a = function () {
    a();
}

What did you expect to happen?
Second case should cause error the same way as the first one.

@eslint eslint bot added the triage label Oct 15, 2018

@platinumazure platinumazure added bug rule evaluating and removed triage labels Oct 15, 2018

@platinumazure

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

Hi @aliaksandr-yermalayeu, thanks for the issue.

It seems to me the recursive declaration case should be flagged as used (i.e., neither example should produce a warning). But I'm not 100% sure.

@not-an-aardvark @mysticatea Any thoughts?

@not-an-aardvark

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

The firat example should produce a warning because the function can never be initially called, since it's only referenced from inside itself.

The second example could be made to produce a warning for the same reason, although this would require modifying the rule to be able to infer that the function can only be accessed through the a variable.

@mysticatea

This comment has been minimized.

Copy link
Member

commented Oct 15, 2018

It has been introduced in #2095; the rule is addressing function declarations to detect recursive calls, but not variable declarations. We can enhance the rule. Maybe...

  • the rule should ignore references in the VariableDeclarator#init and AssignmentPattern#right if duplicate variables don't exist.
@kaicataldo

This comment has been minimized.

Copy link
Member

commented Oct 28, 2018

@mysticatea Just to clarify, are you saying this is a bug?

@mysticatea

This comment has been minimized.

Copy link
Member

commented Oct 29, 2018

Looks like an enhancement to me.

Re-looked, #2095 was handled as a bug, so this is a bug.

@mysticatea mysticatea added accepted and removed evaluating labels Oct 29, 2018

sergei-startsev added a commit to sergei-startsev/eslint that referenced this issue Oct 30, 2018

sergei-startsev added a commit to sergei-startsev/eslint that referenced this issue Oct 30, 2018

@nzakas nzakas closed this in 9436712 Nov 9, 2018

@eslint eslint bot locked and limited conversation to collaborators May 9, 2019

@eslint eslint bot added the archived due to age label May 9, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.