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

Rule "curly" with "multi" has false positive on nested if-for-if, fixer changes code behavior #12928

Closed
drakedevel opened this issue Feb 17, 2020 · 3 comments · Fixed by #12943
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 bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@drakedevel
Copy link

Tell us about your environment

  • ESLint Version: 6.8.0
  • Node Version: 12.16.0
  • npm Version: 6.13.4

This bug also reproduces on ESLint 7.0.0-alpha.1.

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

Please show your full configuration:

Configuration
{
  "rules": {
    "curly": ["warn", "multi"]
  }
}

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

var x = [''];
if (x)
  for (var i in x) {
    if (i > 0)
      console.log(i);
  }
else
  console.log('whoops');
./node_modules/.bin/eslint --fix index.js

What did you expect to happen?
ESLint should make no changes to the code, as the only curly braces present are required.

What actually happened? Please include the actual, raw output from ESLint.
ESLint changes the code to the following:

var x = [''];
if (x)
  for (var i in x) 
    if (i > 0)
      console.log(i);
  
else
  console.log('whoops');

This is incorrect -- the else is now associated with the inner if instead of the outer one. The execution behavior changes accordingly:

$ node index.js 
$ ./node_modules/.bin/eslint index.js

/tmp/eslint-repro/index.js
  3:3  warning  Unnecessary { after 'for-in'  curly

✖ 1 problem (0 errors, 1 warning)
  0 errors and 1 warning potentially fixable with the `--fix` option.

$ ./node_modules/.bin/eslint --fix index.js 
$ node index.js 
whoops

I did a little bit more experimentation: when I replace the for block with a while block, the same issue occurs. However, when I replace the for block with another if, ESLint correctly detects that the curly braces are mandatory.

Are you willing to submit a pull request to fix this bug?
I can if necessary, although the fix is probably much faster for somebody who's already familiar with the relevant code.

@drakedevel drakedevel added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Feb 17, 2020
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Feb 17, 2020
@mdjermanovic
Copy link
Member

Hi @drakedevel, thanks for the bug report!

Confirmed:

The outer if has alternate: null in AST for the fixed code.

@kaicataldo kaicataldo added the help wanted The team would welcome a contribution from the community for this issue label Feb 17, 2020
@mdjermanovic
Copy link
Member

I'm working on this.

@drakedevel
Copy link
Author

Confirmed fixed for me in 7.0.0-alpha.2, thanks!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 28, 2020
@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 Aug 28, 2020
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 bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants