Skip to content

Commit

Permalink
Update: support separate requires in one-var. (fixes eslint#6175)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Oct 14, 2017
1 parent 786cc73 commit 24797ee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/rules/one-var.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {
{
type: "object",
properties: {
separateRequires: {
type: "boolean"
},
var: {
enum: ["always", "never"]
},
Expand Down Expand Up @@ -70,13 +73,16 @@ module.exports = {
options.let = { uninitialized: mode, initialized: mode };
options.const = { uninitialized: mode, initialized: mode };
} else if (typeof mode === "object") { // options configuration is an object
if (mode.hasOwnProperty("var") && typeof mode.var === "string") {
if (mode.hasOwnProperty("separateRequires")) {
options.separateRequires = !!mode.separateRequires;
}
if (mode.hasOwnProperty("var")) {
options.var = { uninitialized: mode.var, initialized: mode.var };
}
if (mode.hasOwnProperty("let") && typeof mode.let === "string") {
if (mode.hasOwnProperty("let")) {
options.let = { uninitialized: mode.let, initialized: mode.let };
}
if (mode.hasOwnProperty("const") && typeof mode.const === "string") {
if (mode.hasOwnProperty("const")) {
options.const = { uninitialized: mode.const, initialized: mode.const };
}
if (mode.hasOwnProperty("uninitialized")) {
Expand Down Expand Up @@ -275,6 +281,16 @@ module.exports = {

const declarations = node.declarations;
const declarationCounts = countDeclarations(declarations);
const requires = declarations.filter(decl => decl.init && decl.init.type === "CallExpression" && decl.init.callee.name === "require");

if (options[type].initialized === MODE_ALWAYS || options[type].uninitialized === MODE_ALWAYS) {
if (options.separateRequires && declarationCounts.length !== requires.length) {
context.report({
node,
message: "Split requires to be separated into a single block."
});
}
}

// always
if (!hasOnlyOneStatement(type, declarations)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/one-var.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,17 @@ ruleTester.run("one-var", rule, {
line: 1,
column: 53
}]
},
{
code: "var foo = require('foo'), bar;",
options: [{ separateRequires: true, var: "always" }],
parserOptions: { env: { node: true } },
errors: [{
message: "Split requires to be separated into a single block.",
type: "VariableDeclaration",
line: 1,
column: 1
}]
}
]
});

0 comments on commit 24797ee

Please sign in to comment.