Skip to content

Commit 9432b10

Browse files
g-planenot-an-aardvark
authored andcommitted
Fix: make separateRequires work in consecutive mode (fixes #10784) (#10886)
1 parent e51868d commit 9432b10

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/rules/one-var.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,13 @@ module.exports = {
384384
if (nodeIndex > 0) {
385385
const previousNode = parent.body[nodeIndex - 1];
386386
const isPreviousNodeDeclaration = previousNode.type === "VariableDeclaration";
387+
const declarationsWithPrevious = declarations.concat(previousNode.declarations || []);
387388

388-
if (isPreviousNodeDeclaration && previousNode.kind === type) {
389+
if (
390+
isPreviousNodeDeclaration &&
391+
previousNode.kind === type &&
392+
!(declarationsWithPrevious.some(isRequire) && !declarationsWithPrevious.every(isRequire))
393+
) {
389394
const previousDeclCounts = countDeclarations(previousNode.declarations);
390395

391396
if (options[type].initialized === MODE_CONSECUTIVE && options[type].uninitialized === MODE_CONSECUTIVE) {

tests/lib/rules/one-var.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ ruleTester.run("one-var", rule, {
263263
options: ["consecutive"],
264264
parserOptions: { ecmaVersion: 6 }
265265
},
266+
267+
// https://github.com/eslint/eslint/issues/10784
268+
{
269+
code: "const foo = require('foo'); const bar = 'bar';",
270+
options: [{ const: "consecutive", separateRequires: true }],
271+
parserOptions: { ecmaVersion: 6 }
272+
},
266273
{
267274
code: "var a = 0, b = 1; var c, d;",
268275
options: [{ initialized: "consecutive", uninitialized: "always" }]

0 commit comments

Comments
 (0)