Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
disallowMultipleVarDecl: improve {"allExcept": ["require"]} logic
Browse files Browse the repository at this point in the history
When using `{"allExcept": ["require"]}`,
allow any declarion that sourced from a require call

Fixes #1848
Closes gh-1849
  • Loading branch information
ValYouW authored and markelog committed Oct 12, 2015
1 parent 9fd8706 commit 56a32f7
Show file tree
Hide file tree
Showing 2 changed files with 41 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);
});
}

};
19 changes: 17 additions & 2 deletions test/specs/rules/disallow-multiple-var-decl.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,28 @@ describe('rules/disallow-multiple-var-decl', function() {
it('should report multiple var decls with require mixed with normal', function() {
expect(checker.checkString('var first = require("first"), second = 1;'))
.to.have.one.validation.error.from('disallowMultipleVarDecl');

var test = 'var first = require("first"), second = 1, third = require("foo").Foo';
expect(checker.checkString(test)).to.have.one.validation.error.from('disallowMultipleVarDecl');
});
it('should report multiple var decls with require mixed with undefined', function() {
var test = 'var first = require("first"), second = require("foo").Foo, third;';
expect(checker.checkString(test)).to.have.one.validation.error.from('disallowMultipleVarDecl');
});
it('should report multiple var decls', function() {
expect(checker.checkString('var x, y;')).to.have.one.validation.error.from('disallowMultipleVarDecl');
});
it('should not report consecutive var decls', function() {
expect(checker.checkString('var x; var y;')).to.have.no.errors();
});
it('should not report multiple var decls sourced from required', function() {
expect(checker.checkString('var x = require("fs"), y = require("fs").File;')).to.have.no.errors();
expect(checker.checkString('var x = require("fs").File, y = require("fs").foo();')).to.have.no.errors();
expect(checker.checkString('var x = require("fs"), y = require("fs").some.long().chain.test();'))
.to.have.no.errors();
expect(checker.checkString('var x = require("fs"), y = require("fs").some().long().chain.test;'))
.to.have.no.errors();
});
});

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

expect(checker.checkString('var a = require("a"), b = require("b"), x, y, z;')).to.have.no.errors();
expect(checker.checkString('var a = require("a"), b = require("b"), x, y, c = 1;'))
expect(checker.checkString('var a = require("a"), b = require("b").getMe(), x, y, z;')).to.have.no.errors();
expect(checker.checkString('var a = require("a").Instance, b = require("b"), x, y, c = 1;'))
.to.have.one.validation.error.from('disallowMultipleVarDecl');
});
it('should accept strict as option', function() {
Expand Down

0 comments on commit 56a32f7

Please sign in to comment.