Skip to content

Commit

Permalink
In disallowMultipleVarDecl, when using allExcept require, allow any d…
Browse files Browse the repository at this point in the history
…eclarion that sourced from a require call [fixes jscs-dev#1848]
  • Loading branch information
ValYouW committed Oct 7, 2015
1 parent 72d6d2e commit 16eac43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
29 changes: 24 additions & 5 deletions lib/rules/disallow-multiple-var-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ module.exports.prototype = {
},

check: function(file, errors) {

function isSourcedFromRequire(node) {
// If this node is a CallExpression it has a callee,
// check if this is the `require` function
if (node.callee && node.callee.name === 'require') {
return true;
}

// If this CallExpression is not a `require` we keep looking for
// the `require` method up in the tree
if (node.callee && node.callee.object) {
return isSourcedFromRequire(node.callee.object);
}

// If there is no `callee` this might be a MemberExpression, keep
// look for the `require` method up in the tree.
if (node.object) {
return isSourcedFromRequire(node.object);
}

return false;
}

var inStrictMode = this._strictMode;
var exceptUndefined = this._exceptUndefined;
var exceptRequire = this._exceptRequire;
Expand All @@ -119,10 +142,7 @@ module.exports.prototype = {

var requireStatements = node.declarations.filter(function(declaration) {
var init = declaration.init;

return init &&
init.callee &&
init.callee.name === 'require';
return init && isSourcedFromRequire(init);
});
var allRequireStatements = requireStatements.length === node.declarations.length;

Expand Down Expand Up @@ -160,5 +180,4 @@ module.exports.prototype = {
errors.add('Multiple var declaration', node.loc.start);
});
}

};
13 changes: 11 additions & 2 deletions test/specs/rules/disallow-multiple-var-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,23 @@ describe('rules/disallow-multiple-var-decl', function() {
assert(checker.checkString('var first = require("first"), second = require("second");').isEmpty());
});
it('should report multiple var decls with require mixed with normal', function() {
assert.equal(1, checker.checkString('var first = require("first"), second = 1;').getErrorCount());
assert.equal(1, checker.checkString('var first = require("first"), second = 1, third = require("foo").Foo').getErrorCount());
});
it('should report multiple var decls with require mixed with undefined', function() {
assert.equal(1, checker.checkString('var first = require("first"), second = require("foo").Foo, third;').getErrorCount());
});
it('should report multiple var decls', function() {
assert.equal(1, checker.checkString('var x, y;').getErrorCount());
});
it('should not report consecutive var decls', function() {
assert(checker.checkString('var x; var y;').isEmpty());
});
it('should not report multiple var decls sourced from required', function() {
assert(checker.checkString('var x = require("fs"), y = require("fs").File; ').isEmpty());
assert(checker.checkString('var x = require("fs").File, y = require("fs").foo();').isEmpty());
assert(checker.checkString('var x = require("fs"), y = require("fs").some.long().chain.test();').isEmpty());
assert(checker.checkString('var x = require("fs"), y = require("fs").some().long().chain.test;').isEmpty());
});
});

describe('options as object', function() {
Expand All @@ -107,7 +116,7 @@ describe('rules/disallow-multiple-var-decl', function() {
it('should accept require and undefined as allExcept value', function() {
checker.configure({ disallowMultipleVarDecl: { allExcept: ['undefined', 'require'] } });

assert(checker.checkString('var a = require("a"), b = require("b"), x, y, z;').isEmpty());
assert(checker.checkString('var a = require("a"), b = require("b").useDefault(), x, y, z;').isEmpty());
assert.equal(
1,
checker.checkString('var a = require("a"), b = require("b"), x, y, c = 1;').getErrorCount()
Expand Down

0 comments on commit 16eac43

Please sign in to comment.