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: curly multi reports single lexical declarations (fixes #11908) #12513

Merged
merged 1 commit into from Nov 4, 2019

Conversation

mdjermanovic
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] Bug fix #11908

Tell us about your environment

  • ESLint Version: 6.6.0
  • Node Version: 10.16.0
  • npm Version: 6.9.0

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

default

Please show your full configuration:

Configuration
module.exports = {
    parserOptions: {
		ecmaVersion: 2015,
    }
};

What did you do? Please include the actual source code causing the issue.

Online Demo Link

/* eslint curly: ["error", "multi"] */

if (foo) {
    const a = 1;
}

What did you expect to happen?

0 errors. A block with only one statement which is a lexical declaration doesn't make much sense, but still these curly braces cannot be removed.

What actually happened? Please include the actual, raw output from ESLint.

1 error and syntax error in the fixed code.

3:1  error  Unnecessary { after 'if' condition  curly
/* eslint curly: ["error", "multi"] */

if (foo) 
    const a = 1;
4:2  error  Parsing error: Unexpected token const

What changes did you make? (Give an overview)

Changed the multi option to not report single-statement blocks if that statement is a let, const, function or class declaration.

Is there anything you'd like reviewers to focus on?

The following added valid test were invalid, and the autofix was producing syntax errors:

if (foo) { const bar = 'baz'; }
while (foo) { let bar = 'baz'; }
for (foo in bar) { class Baz {} }

The same applies to these valid tests with the consistent modifier:

if (foo) { let bar; } else { baz(); }
if (foo) { bar(); } else { const baz = 'quux'; }

The following valid test was also invalid. This code without braces is not a syntax error in non-strict mode, but it's a legacy syntax, so the original code shouldn't be reported and auto-fixed:

for(;;) { function foo() {} }

This invalid test was invalid before, it's a regression test:

if (foo) { var bar = 'baz'; }

These two invalid tests with the consistent modifier were invalid before, but in a different way. Autofix was removing braces, which was producing syntax errors. After this PR, autofix will do the opposite, put the braces around the statement that isn't in a block.

if (foo) { let bar; } else baz();
if (foo) bar(); else { const baz = 'quux' }

That's the only case where this PR can produce more warnings (e.g. 2 instead of 1), but that can happen only in chains that already have warnings.

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities labels Oct 30, 2019
Copy link
Member

@kaicataldo kaicataldo 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!

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you very much!

@kaicataldo kaicataldo merged commit bb556d5 into master Nov 4, 2019
@kaicataldo kaicataldo deleted the issue11908 branch November 4, 2019 18:25
@kaicataldo
Copy link
Member

Thanks for contributing!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators May 4, 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 May 4, 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 autofix This change is related to ESLint's autofixing capabilities 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.

curly 'multi' autofix produces syntax errors with lexical declarations
3 participants