This repository was archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdisallow-multiple-var-decl.js
183 lines (158 loc) · 5.14 KB
/
disallow-multiple-var-decl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Disallows multiple `var` declaration (except for-loop).
*
* Types: `Boolean` or `Object`
*
* Values:
*
* - `true` disallows multiple variable declarations except within a for loop
* - `Object`:
* - `'strict'` disallows all multiple variable declarations
* - `'allExcept'` array of exceptions:
* - `'undefined'` allows declarations where all variables are not defined
* - `'require'` allows declarations where all variables are importing external modules with require
*
* #### Example
*
* ```js
* "disallowMultipleVarDecl": true
* ```
*
* ##### Valid for `true`
*
* ```js
* var x = 1;
* var y = 2;
*
* for (var i = 0, j = arr.length; i < j; i++) {}
* ```
*
* ##### Valid for `{ strict: true }`
*
* ```js
* var x = 1;
* var y = 2;
* ```
*
* ##### Valid for `{ allExcept: ['undefined'] }`
*
* ```js
* var a, b;
* var x = 1;
* var y = 2;
*
* for (var i = 0, j = arr.length; i < j; i++) {}
* ```
* ##### Valid for `{ allExcept: ['require'] }`
*
* ```js
* var a = require('a'),
* b = require('b');
*
* var x = 1;
* var y = 2;
*
* for (var i = 0, j = arr.length; i < j; i++) {}
* ```
*
* ##### Invalid
*
* ```js
* var x = 1,
* y = 2;
*
* var x, y = 2, z;
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
// support for legacy options
if (typeof options !== 'object') {
assert(
options === true ||
options === 'strict' ||
options === 'exceptUndefined',
this.getOptionName() +
' option requires a true value, "strict", "exceptUndefined", or an object'
);
var _options = {
strict: options === 'strict',
allExcept: []
};
if (options === 'exceptUndefined') {
_options.allExcept.push('undefined');
}
return this.configure(_options);
}
if (Array.isArray(options.allExcept)) {
this._exceptUndefined = options.allExcept.indexOf('undefined') > -1;
this._exceptRequire = options.allExcept.indexOf('require') > -1;
}
this._strictMode = options.strict === true;
},
getOptionName: function() {
return 'disallowMultipleVarDecl';
},
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;
file.iterateNodesByType('VariableDeclaration', function(node) {
var definedVariables = node.declarations.filter(function(declaration) {
return !!declaration.init;
});
var hasDefinedVariables = definedVariables.length > 0;
var requireStatements = node.declarations.filter(function(declaration) {
var init = declaration.init;
return init && isSourcedFromRequire(init);
});
var allRequireStatements = requireStatements.length === node.declarations.length;
var isForStatement = node.parentElement.type === 'ForStatement';
// allow single var declarations
if (node.declarations.length === 1) {
return;
}
// allow multiple var declarations in for statement unless we're in strict mode
// for (var i = 0, j = myArray.length; i < j; i++) {}
if (!inStrictMode && isForStatement) {
return;
}
// allow multiple var declarations with all undefined variables in exceptUndefined mode
// var a, b, c
if (exceptUndefined && !hasDefinedVariables) {
return;
}
// allow multiple var declaration with all require
// var a = require("a"), b = require("b")
if (exceptRequire && allRequireStatements) {
return;
}
// allow multiple var declarations only with require && undefined
// var a = require("a"), b = require("b"), x, y
if (exceptUndefined && exceptRequire && definedVariables.length === requireStatements.length) {
return;
}
errors.add('Multiple var declaration', node);
});
}
};