Skip to content

Commit

Permalink
fix edge case.
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 15, 2017
1 parent 7d97579 commit c8fd8ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rules/one-var.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ module.exports = {
const declarationCounts = countDeclarations(declarations);
const currentOptions = options[statementType] || {};
const currentScope = getCurrentScope(statementType);
const hasRequires = declarations.some(isRequire);

if (currentOptions.uninitialized === MODE_ALWAYS && currentOptions.initialized === MODE_ALWAYS) {
if (currentScope.uninitialized || currentScope.initialized) {
Expand All @@ -264,6 +265,9 @@ module.exports = {
return false;
}
}
if (currentScope.required && hasRequires) {
return false;
}
recordTypes(statementType, declarations, currentScope);
return true;
}
Expand Down Expand Up @@ -294,10 +298,10 @@ module.exports = {

const declarations = node.declarations;
const declarationCounts = countDeclarations(declarations);
const requires = declarations.filter(isRequire);
const mixedRequires = declarations.some(isRequire) && !declarations.every(isRequire);

if (options[type].initialized === MODE_ALWAYS) {
if (options.separateRequires && requires.length > 0 && declarations.length !== requires.length) {
if (options.separateRequires && mixedRequires) {
context.report({
node,
message: "Split requires to be separated into a single block."
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 @@ -597,6 +597,17 @@ ruleTester.run("one-var", rule, {
line: 1,
column: 1
}]
},
{
code: "const foo = require('foo'); const bar = require('bar');",
options: [{ separateRequires: true, const: "always" }],
parserOptions: { env: { node: true } },
errors: [{
message: "Combine this with the previous 'const' statement.",
type: "VariableDeclaration",
line: 1,
column: 29
}]
}
]
});

0 comments on commit c8fd8ea

Please sign in to comment.