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

Commit 56a32f7

Browse files
ValYouWmarkelog
authored andcommitted
disallowMultipleVarDecl: improve {"allExcept": ["require"]} logic
When using `{"allExcept": ["require"]}`, allow any declarion that sourced from a require call Fixes #1848 Closes gh-1849
1 parent 9fd8706 commit 56a32f7

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

lib/rules/disallow-multiple-var-decl.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ module.exports.prototype = {
107107
},
108108

109109
check: function(file, errors) {
110+
111+
function isSourcedFromRequire(node) {
112+
// If this node is a CallExpression it has a callee,
113+
// check if this is the `require` function
114+
if (node.callee && node.callee.name === 'require') {
115+
return true;
116+
}
117+
118+
// If this CallExpression is not a `require` we keep looking for
119+
// the `require` method up in the tree
120+
if (node.callee && node.callee.object) {
121+
return isSourcedFromRequire(node.callee.object);
122+
}
123+
124+
// If there is no `callee` this might be a MemberExpression, keep
125+
// look for the `require` method up in the tree.
126+
if (node.object) {
127+
return isSourcedFromRequire(node.object);
128+
}
129+
130+
return false;
131+
}
132+
110133
var inStrictMode = this._strictMode;
111134
var exceptUndefined = this._exceptUndefined;
112135
var exceptRequire = this._exceptRequire;
@@ -119,10 +142,7 @@ module.exports.prototype = {
119142

120143
var requireStatements = node.declarations.filter(function(declaration) {
121144
var init = declaration.init;
122-
123-
return init &&
124-
init.callee &&
125-
init.callee.name === 'require';
145+
return init && isSourcedFromRequire(init);
126146
});
127147
var allRequireStatements = requireStatements.length === node.declarations.length;
128148

@@ -160,5 +180,4 @@ module.exports.prototype = {
160180
errors.add('Multiple var declaration', node.loc.start);
161181
});
162182
}
163-
164183
};

test/specs/rules/disallow-multiple-var-decl.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,28 @@ describe('rules/disallow-multiple-var-decl', function() {
9696
it('should report multiple var decls with require mixed with normal', function() {
9797
expect(checker.checkString('var first = require("first"), second = 1;'))
9898
.to.have.one.validation.error.from('disallowMultipleVarDecl');
99+
100+
var test = 'var first = require("first"), second = 1, third = require("foo").Foo';
101+
expect(checker.checkString(test)).to.have.one.validation.error.from('disallowMultipleVarDecl');
102+
});
103+
it('should report multiple var decls with require mixed with undefined', function() {
104+
var test = 'var first = require("first"), second = require("foo").Foo, third;';
105+
expect(checker.checkString(test)).to.have.one.validation.error.from('disallowMultipleVarDecl');
99106
});
100107
it('should report multiple var decls', function() {
101108
expect(checker.checkString('var x, y;')).to.have.one.validation.error.from('disallowMultipleVarDecl');
102109
});
103110
it('should not report consecutive var decls', function() {
104111
expect(checker.checkString('var x; var y;')).to.have.no.errors();
105112
});
113+
it('should not report multiple var decls sourced from required', function() {
114+
expect(checker.checkString('var x = require("fs"), y = require("fs").File;')).to.have.no.errors();
115+
expect(checker.checkString('var x = require("fs").File, y = require("fs").foo();')).to.have.no.errors();
116+
expect(checker.checkString('var x = require("fs"), y = require("fs").some.long().chain.test();'))
117+
.to.have.no.errors();
118+
expect(checker.checkString('var x = require("fs"), y = require("fs").some().long().chain.test;'))
119+
.to.have.no.errors();
120+
});
106121
});
107122

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

117-
expect(checker.checkString('var a = require("a"), b = require("b"), x, y, z;')).to.have.no.errors();
118-
expect(checker.checkString('var a = require("a"), b = require("b"), x, y, c = 1;'))
132+
expect(checker.checkString('var a = require("a"), b = require("b").getMe(), x, y, z;')).to.have.no.errors();
133+
expect(checker.checkString('var a = require("a").Instance, b = require("b"), x, y, c = 1;'))
119134
.to.have.one.validation.error.from('disallowMultipleVarDecl');
120135
});
121136
it('should accept strict as option', function() {

0 commit comments

Comments
 (0)