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: one-var shouldn't split declaration if it isn't in a statement list #13959

Merged
merged 1 commit into from Dec 30, 2020

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

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

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Tell us about your environment

  • ESLint Version: v7.16.0
  • Node Version: v12.18.4
  • npm Version: v6.14.6

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, 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

/* eslint one-var: ["error", "never"] */

do 
    var x = f(), y = b();
while (x < y);

What did you expect to happen?

Given that this rule already reports errors that require non-trivial manual fixes, I'd expect an error without the autofix. This code can be fixed by adding a block (or in some other way), but that seems out of scope for this rule.

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

Error with autofix that produces a syntax error:

/* eslint one-var: ["error", "never"] */

do 
    var x = f(); var y = b();
while (x < y);
  4:18  error  Parsing error: Unexpected token var

What changes did you make? (Give an overview)

Fixed splitDeclarations() to produce a fix only if the given VariableDeclaration is in a statement list, i.e. if it is a child of Program, BlockStatement, or SwitchCase node.

Autofix will not be provided if the reported declaration is a direct child of if, while, do-while, for, for-in, for-of or with, or if it is labeled.

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

There's also a false negative in SwitchCase nodes when the option is "consecutive" (I'll fix that in another PR), and inconsistent behavior with export-ed declarations (I'll open an issue for that).

@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 Dec 27, 2020
Copy link
Member

@yeonjuan yeonjuan 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

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

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

LGTM! 💯

@mdjermanovic mdjermanovic merged commit 19c69c0 into master Dec 30, 2020
@mdjermanovic mdjermanovic deleted the onevar-blockposition branch December 30, 2020 23:42
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 29, 2021
@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 29, 2021
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.

None yet

3 participants