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 pathrequire-curly-braces.js
184 lines (159 loc) · 5.05 KB
/
require-curly-braces.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
/**
* Requires curly braces after statements.
*
* Types: `Array` or `Boolean` or `Object`
*
* Values:
* - Array of quoted keywords
* - `true` to require curly braces after the following keywords
* - `Object`
* - `'keywords'`
* - Array of quoted keywords
* - `'allExcept'`
* - Array of keywords inside of the block that would allow curly braces
* - Ex: ["return" , "continue", "break"]
*
* JSHint: [`curly`](http://jshint.com/docs/options/#curly)
*
* #### Example
*
* ```js
* "requireCurlyBraces": [
* "if",
* "else",
* "for",
* "while",
* "do",
* "try",
* "catch",
* "case",
* "default"
* ]
* ```
*
* ##### Valid
*
* ```js
* if (x) {
* x++;
* }
* ```
*
* ##### Invalid
*
* ```js
* if (x) x++;
* ```
*/
var assert = require('assert');
var defaultKeywords = require('../utils').curlyBracedKeywords;
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
Array.isArray(options) || options === true || typeof options === 'object',
this.getOptionName() + ' option requires array, true value, or object'
);
var keywordMap = {
'return': 'ReturnStatement',
'break': 'BreakStatement',
'continue': 'ContinueStatement'
};
if (options === true) {
options = defaultKeywords;
}
if (!Array.isArray(options)) {
assert(
Array.isArray(options.allExcept),
this.getOptionName() + '.allExcept ' +
'property requires an array value'
);
assert(
Array.isArray(options.keywords) || options.keywords === true,
this.getOptionName() + '.keywords ' +
'property requires an array value or a value of true'
);
if (options.keywords === true) {
options.keywords = defaultKeywords;
}
this._exceptions = options.allExcept.map(function(statementType) {
return keywordMap[statementType];
});
options = options.keywords;
}
this._typeIndex = {};
for (var i = 0, l = options.length; i < l; i++) {
this._typeIndex[options[i]] = true;
}
},
getOptionName: function() {
return 'requireCurlyBraces';
},
check: function(file, errors) {
var typeIndex = this._typeIndex;
var exceptions = this._exceptions;
function isNotABlockStatement(node) {
return node && node.type !== 'BlockStatement';
}
function addError(typeString, entity) {
errors.add(
typeString + ' statement without curly braces',
entity
);
}
function checkBody(type, typeString) {
file.iterateNodesByType(type, function(node) {
if (isNotABlockStatement(node.body)) {
addError(typeString, node);
}
});
}
if (typeIndex.if || typeIndex.else) {
file.iterateNodesByType('IfStatement', function(node) {
if (typeIndex.if && isNotABlockStatement(node.consequent) &&
// check exceptions for if and else
!(exceptions && exceptions.indexOf(node.consequent.type) !== -1)) {
// console.log(node.firstChild.getSourceCode());
addError('If', node.firstChild);
}
if (typeIndex.else && isNotABlockStatement(node.alternate) &&
node.alternate.type !== 'IfStatement' &&
// check exceptions for if and else
!(exceptions && exceptions.indexOf(node.consequent.type) !== -1)) {
addError('Else', node.alternate.getPreviousCodeToken());
}
});
}
if (typeIndex.case || typeIndex.default) {
file.iterateNodesByType('SwitchCase', function(node) {
// empty case statement
if (node.consequent.length === 0) {
return;
}
if (node.consequent.length === 1 && node.consequent[0].type === 'BlockStatement') {
return;
}
if (node.test === null && typeIndex.default) {
addError('Default', node);
}
if (node.test !== null && typeIndex.case) {
addError('Case', node);
}
});
}
if (typeIndex.while) {
checkBody('WhileStatement', 'While');
}
if (typeIndex.for) {
checkBody('ForStatement', 'For');
checkBody('ForInStatement', 'For in');
checkBody('ForOfStatement', 'For of');
}
if (typeIndex.do) {
checkBody('DoWhileStatement', 'Do while');
}
if (typeIndex.with) {
checkBody('WithStatement', 'With');
}
}
};