This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/
require-var-decl-first.js
185 lines (169 loc) · 3.47 KB
/
require-var-decl-first.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
184
185
/**
* Requires `var` declaration to be on the top of an enclosing scope
*
* Types: `Boolean`
*
* Values:
*
* - `true` specifies that `var` declarations must occur the top of a function scope.
*
* #### Example
*
* ```js
* "requireVarDeclFirst": true
* ```
*
* ##### Valid for mode `true`
*
* ```js
* var x = 1,
* y = 2;
* ```
* ```js
* 'use strict;'
* var x = 1,
* y = 2;
* ```
* ```js
* var x = 1;
* var y = 2;
* ```
* ```js
* var x = 1;
* // comments
* var y = 2;
* ```
* ```js
* var x = 1;
* // comments
* // comments 2
* var y = 2;
* ```
* ```js
* const a = 1;
* const b = 2;
* ```
* ```js
* var x = 1;
* function y() {var z;};
* ```
* ```js
* var x = 1;
* var y = function () {var z;};
* ```
* ```js
* var w = 1;
* function x() {
* var y;
* // comments
* // comments 2
* var z;
* };
* ```
* ```js
* var w = 1;
* function x() {
* "use strict";
* var y;
* };
* ```
* ```js
* var x = 1;
* var y;
* for (y = 0; y < 10; y++) {};
* ```
*
* ##### Invalid
*
* ```js
* var x;
* x = 1;
* var y = 2;
* ```
* ```js
* var w = 1;
* function x() {var y;};
* var z = 2;
* ```
* ```js
* var w = 1;
* function x() {
* var y;
* y = 2;
* var z;
* };
* ```
* ```js
* var a;
* for(var count=0;count < 10;count++){}
* ```
* ```js
* var x;
* for(var count=0;count < 10;count++){
* var y;
* }
* ```
*
*/
var assert = require('assert');
function isScopeElement(elem) {
return elem.type === 'Program' ||
elem.type === 'BlockStatement' && (
elem.parentElement.type === 'FunctionExpression' ||
elem.parentElement.type === 'FunctionDeclaration' ||
elem.parentElement.type === 'ArrowFunctionExpression'
);
}
/**
* Checks for allowed elements before variable declaration
* @param {Object} elem
* @returns {Boolean}
*/
function isRelevantElement(elem) {
// Allow comments and whitespaces.
var itIs = elem.isComment || elem.isWhitespace;
// Variable declaration itself.
itIs = itIs || elem.type === 'VariableDeclaration';
// For '{' in `function() { var a; }`.
itIs = itIs || elem.type === 'Punctuator';
// Skip 'use strict' statements at the beginning.
itIs = itIs || elem.type === 'ExpressionStatement' && elem.expression.value === 'use strict';
return itIs;
}
function isVarDeclFirst(varDecl) {
var elem = varDecl;
// All elements should be relevant.
while (elem && (elem.isComment || elem.isWhitespace || isRelevantElement(elem))) {
elem = elem.previousSibling;
}
if (elem === null) {
return true;
}
return false;
}
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value'
);
},
getOptionName: function() {
return 'requireVarDeclFirst';
},
check: function(file, errors) {
file.iterateNodesByType(['VariableDeclaration'], function(varDecl) {
// Ignore let and const for now #1783
if (varDecl.kind !== 'var') {
return;
}
// Checking scope to not allow vars inside block statements.
if (isScopeElement(varDecl.parentElement) && isVarDeclFirst(varDecl)) {
return;
}
errors.add('Variable declarations must be the first statements of a function scope.',
varDecl);
});
}
};