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: make separateRequires work in consecutive mode (fixes #10784) #10886

Merged
merged 2 commits into from Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rules/one-var.js
Expand Up @@ -384,8 +384,13 @@ module.exports = {
if (nodeIndex > 0) {
const previousNode = parent.body[nodeIndex - 1];
const isPreviousNodeDeclaration = previousNode.type === "VariableDeclaration";
const declarationsWithPrevious = declarations.concat(isPreviousNodeDeclaration && previousNode.declarations);
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: if isPreviousNodeDeclaration being false, it could be pushed to declarations. can we change it to something like:

const declarationsWithPrevious = declarations.concat(previousNode.declarations || []);


if (isPreviousNodeDeclaration && previousNode.kind === type) {
if (
isPreviousNodeDeclaration &&
previousNode.kind === type &&
!(declarationsWithPrevious.some(isRequire) && !declarationsWithPrevious.every(isRequire))
) {
const previousDeclCounts = countDeclarations(previousNode.declarations);

if (options[type].initialized === MODE_CONSECUTIVE && options[type].uninitialized === MODE_CONSECUTIVE) {
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/one-var.js
Expand Up @@ -263,6 +263,13 @@ ruleTester.run("one-var", rule, {
options: ["consecutive"],
parserOptions: { ecmaVersion: 6 }
},

// https://github.com/eslint/eslint/issues/10784
{
code: "const foo = require('foo'); const bar = 'bar';",
options: [{ const: "consecutive", separateRequires: true }],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var a = 0, b = 1; var c, d;",
options: [{ initialized: "consecutive", uninitialized: "always" }]
Expand Down