From 2c561839caa0cafd65da449bdf9fd2fa851ae855 Mon Sep 17 00:00:00 2001 From: Sstern6 Date: Sun, 14 Oct 2018 21:01:46 -0700 Subject: [PATCH] Fix: adds conditional for separateRequires in one-var (fixes #10179) --- lib/rules/one-var.js | 8 ++++++-- tests/lib/rules/one-var.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/rules/one-var.js b/lib/rules/one-var.js index 44f05fb700a..dc5ecade0a4 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) { diff --git a/tests/lib/rules/one-var.js b/tests/lib/rules/one-var.js index e5470d2e9ca..435b336baec 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;",