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

mdjermanovic commented Oct 30, 2019

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.

Copy link
Member

kaicataldo left a comment

LGTM, thanks!

Copy link
Member

mysticatea left a comment

LGTM, thank you very much!

@kaicataldo kaicataldo merged commit bb556d5 into master Nov 4, 2019
18 checks passed
18 checks passed
Verify Files
Details
Test (ubuntu-latest, 13.x)
Details
Test (ubuntu-latest, 12.x)
Details
Test (ubuntu-latest, 10.x)
Details
Test (ubuntu-latest, 8.x)
Details
Test (ubuntu-latest, 8.10.0)
Details
Test (windows-latest, 12.x)
Details
Test (macOS-latest, 12.x)
Details
Browser Test
Details
commit-message Commit message follows guidelines
Details
continuous-integration Build #20191030.3 succeeded
Details
continuous-integration (Test on Node.js 10 (Linux)) Test on Node.js 10 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Linux)) Test on Node.js 12 (Linux) succeeded
Details
continuous-integration (Test on Node.js 12 (Windows)) Test on Node.js 12 (Windows) succeeded
Details
continuous-integration (Test on Node.js 12 (macOS)) Test on Node.js 12 (macOS) succeeded
Details
continuous-integration (Test on Node.js 8 (Linux)) Test on Node.js 8 (Linux) succeeded
Details
licence/cla Contributor License Agreement is signed.
Details
release-monitor No patch release is pending
Details
@kaicataldo kaicataldo deleted the issue11908 branch Nov 4, 2019
@kaicataldo
Copy link
Member

kaicataldo commented Nov 4, 2019

Thanks for contributing!

@eslint eslint bot locked and limited conversation to collaborators May 4, 2020
@eslint eslint bot added the archived due to age label May 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.