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

Bug: no-var autofix is not working #16610

Closed
mvorisek opened this issue Dec 4, 2022 · 3 comments · Fixed by #16611
Closed

Bug: no-var autofix is not working #16610

mvorisek opened this issue Dec 4, 2022 · 3 comments · Fixed by #16611
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects

Comments

@mvorisek
Copy link

mvorisek commented Dec 4, 2022

How to reproduce?

config:

'no-var': ['error']

code:

var fx = function (i = 0) {
  if (i < 5) {
    return fx(i + 1);
  }
  console.log(i);
};

fx();

What did you expect to happen?

var fx autofixed to let fx

I tried the code in a sandpit and it behaves the same.

What actually happened?

not autofixed, Unexpected var, use let or const instead no-var error is generated

(autofix of var -> let works in other cases, the problem is present only when the variable is referred in anonymous function in the same declaration)

@mvorisek mvorisek added bug ESLint is working incorrectly repro:needed labels Dec 4, 2022
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Dec 4, 2022
@mdjermanovic mdjermanovic moved this from Needs Triage to Triaging in Triage Dec 4, 2022
@mdjermanovic mdjermanovic added repro:yes rule Relates to ESLint's core rules autofix This change is related to ESLint's autofixing capabilities and removed repro:needed labels Dec 4, 2022
@mdjermanovic
Copy link
Member

Hi @mvorisek, thank for the issue!

I can reproduce this. The variable is referenced in its initializer, so the rule considers the fix unsafe because the reference might be evaluated in TDZ. I think we can make a small change to ignore the reference if the initializer is a function definition, as in that case the reference certainly cannot be evaluated in TDZ. I'm working on the fix.

@mdjermanovic mdjermanovic self-assigned this Dec 4, 2022
@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Dec 4, 2022
@amareshsm
Copy link
Member

@mdjermanovic I would like to give it a try. Can I work on this?

ignore the reference if the initializer is a function definition

Like this?

    if (!declarator.init || declarator.init.type === 'FunctionExpression') {
                return false;
    }

mdjermanovic added a commit that referenced this issue Dec 4, 2022
@mdjermanovic mdjermanovic moved this from Triaging to Pull Request Opened in Triage Dec 4, 2022
@mdjermanovic
Copy link
Member

Ah, I've already started working on this and prepared PR #16611.

Triage automation moved this from Pull Request Opened to Complete Dec 8, 2022
nzakas pushed a commit that referenced this issue Dec 8, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 7, 2023
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion autofix This change is related to ESLint's autofixing capabilities bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

3 participants