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 1 commit into from Dec 30, 2020


Copy link

@mdjermanovic mdjermanovic commented Dec 27, 2020

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?


Please show your full 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"] */

    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"] */

    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).

Copy link

@yeonjuan yeonjuan left a comment

LGTM! Thanks 👍


Copy link

@aladdin-add aladdin-add left a comment



@mdjermanovic mdjermanovic merged commit 19c69c0 into master Dec 30, 2020
13 checks passed
@mdjermanovic mdjermanovic deleted the onevar-blockposition branch Dec 30, 2020
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants