Repository: eslint/eslint (JavaScript)

Issue:
'one-var' doesn't respect 'separateRequires' option with 'always' or 'consecutive'
**Tell us about your environment**

* **ESLint Version:** 4.19.1
* **Node Version:** 8.11.1
* **npm Version:** 5.8.0

**What parser (default, Babel-ESLint, etc.) are you using?**
default

**Please show your full configuration:**

<details>
<summary>Configuration</summary>

<!-- Paste your configuration below: -->
```js
module.exports = {
	'root': true,
	'extends': [],
	'parserOptions': {
		'ecmaVersion': 8
	},
	'env': {
		'es6': true,
		'node': true
	},
	'rules': {
		'no-mixed-requires': [2, { 'allowCall': false }],
		'one-var': [2, { 'var': 'consecutive', 'separateRequires': true }],
	}
};
```

</details>

**What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.**
Took code from https://eslint.org/docs/rules/one-var#var-let-and-const under the `separateRequires` block.
<!-- Paste the source code below: -->
```js
var foo = require('foo');
var bar = 'bar';

var foo = require('foo'),
	bar = require('bar');
```

<!-- Paste the command you used to run ESLint: -->
Ran through Webstorm, which I presume just runs the configured package (which is the correct path) against the current file.

**What did you expect to happen?**
No errors are flagged.

**What actually happened? Please include the actual, raw output from ESLint.**
2 errors are flagged: The first `bar` variable wants to be joined with the first `foo`. The second foo/bar block wants to be joined with the first block. Interestingly if you make the `consecutive` option an `always`, the first error goes away.



Discussion:
Even paring it down further to just the following, it still doesn't seem to validate for the "correct" example.

**.eslintrc.js**
```js
module.exports = {
	'root': true,
	'extends': [],
	'rules': {
		'one-var': [2, { separateRequires: true, var: "always" }],
	}
};
```

**eslint.js**
```js
var foo = require('foo');
var bar = 'bar';

var foo = require('foo'),
	bar = require('bar');
```

------

```
$ eslint eslint.js 

/tmp/eslint/eslint.js
  4:1  error  Combine this with the previous 'var' statement  one-var

✖ 1 problem (1 error, 0 warnings)
```
Same issue. Quite annoying.

Anyone have a known working version that we can downgrade to?
Looks like this slipped through the cracks. Sorry about that.

@eslint/eslint-team Any thoughts on this issue? Seems like a potential bug to me.
Agree. This looks like a bug. Easier example to reproduce:
```js
/* eslint one-var: [2, { separateRequires: true, var: "always" }] */
var bar = 'bar';

var foo = require('foo');
```
Would like to work on this.
@sstern6 This bug report is accepted, so feel free to work on this whenever you get a chance! Thanks for the help
Currently workin on this, have some ideas with what is wrong but will keep looking and hopefully have a PR by Friday.
Just to clarify this issue, @ilyavolodin example for a failing case is correct.  Worked with @not-an-aardvark to determine the original example for the bug report is a little misleading as is the documentation and should be 2 separate test cases not 1, which currently does not throw any errors.

```
var foo = require('foo');
var bar = 'bar';
```

Should be run independent of:
```
var foo = require('foo'),
	bar = require('bar');
```

Looked into the issue last night and think I understand where the issue is being caused, should have a pr soon.

Thanks



Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that accepted issues failing to be implemented after 90 days tend to
never be implemented, and as such, we [close those issues](https://eslint.org/docs/maintainer-guide/issues#when-to-close-an-issue).
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

[//]: # (auto-close)

@kaicataldo theres a fix open for this, why did this get closed?
I‘ve self-assigned, to avoid the bot closing.
Hi, I can see it is done and added to eslint@5.14.0.  I've updated eslint to this version and tried to add rules: 
``` json
{
    "prefer-const": "error",
    "one-var": [
            "error", 
            {
                "separateRequires": true,
                "const": "consecutive"
            }
        ]
}
```
And then the fact that const is CommonJS require is ignored and eslint shows me an error in that case. What am I doing wrong?
I've written [the demo](https://gitlab.com/korneliakobiela/eslint-one-var-demo)



Patch:
diff --git a/lib/rules/one-var.js b/lib/rules/one-var.js
index 44f05fb700a0..dc5ecade0a4d 100644
--- a/lib/rules/one-var.js
+++ b/lib/rules/one-var.js
@@ -257,7 +257,9 @@ module.exports = {
 
             if (currentOptions.uninitialized === MODE_ALWAYS && currentOptions.initialized === MODE_ALWAYS) {
                 if (currentScope.uninitialized || currentScope.initialized) {
-                    return false;
+                    if (!hasRequires) {
+                        return false;
+                    }
                 }
             }
 
@@ -268,7 +270,9 @@ module.exports = {
             }
             if (declarationCounts.initialized > 0) {
                 if (currentOptions.initialized === MODE_ALWAYS && currentScope.initialized) {
-                    return false;
+                    if (!hasRequires) {
+                        return false;
+                    }
                 }
             }
             if (currentScope.required && hasRequires) {


Test patch:
diff --git a/tests/lib/rules/one-var.js b/tests/lib/rules/one-var.js
index e5470d2e9ca3..435b336baec2 100644
--- a/tests/lib/rules/one-var.js
+++ b/tests/lib/rules/one-var.js
@@ -209,6 +209,14 @@ ruleTester.run("one-var", rule, {
             options: [{ separateRequires: true, var: "always" }],
             parserOptions: { env: { node: true } }
         },
+        {
+            code: "var bar = 'bar'; var foo = require('foo');",
+            options: [{ separateRequires: true, var: "always" }]
+        },
+        {
+            code: "var foo = require('foo'); var bar = 'bar';",
+            options: [{ separateRequires: true, var: "always" }]
+        },
         {
             code: "var foo = require('foo'); var bar = 'bar';",
             options: [{ separateRequires: true, var: "always" }],
@@ -1092,6 +1100,17 @@ ruleTester.run("one-var", rule, {
                 column: 25
             }]
         },
+        {
+            code: "var a = true; var b = false;",
+            output: "var a = true,  b = false;",
+            options: [{ separateRequires: true, var: "always" }],
+            errors: [{
+                message: "Combine this with the previous 'var' statement.",
+                type: "VariableDeclaration",
+                line: 1,
+                column: 15
+            }]
+        },
         {
             code: "const a = 0; let b = 1; let c = 2; const d = 3;",
             output: "const a = 0; let b = 1,  c = 2; const d = 3;",