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-capitalized-comments.js
executable file
·288 lines (241 loc) · 6.56 KB
/
require-capitalized-comments.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Requires the first alphabetical character of a comment to be uppercase, unless it is part of a multi-line textblock.
*
* This rule automatically ignores jscs, jshint, eslint and istanbul specific comments.
*
* Types: `Boolean` or `Object`
*
* Values:
* - `true`
* - `Object`:
* - `allExcept`: array of quoted exceptions
* - `inlined`: Ignore comments in the middle of the code line
*
* #### Example
*
* ```js
* "requireCapitalizedComments": true
* ```
*
* ##### Valid:
*
* ```js
* // Valid
* //Valid
*
* /*
* Valid
* *\/
*
* /**
* * Valid
* *\/
*
* // A textblock is a set of lines
* // that starts with a capitalized letter
* // and has one or more non-capitalized lines
* // afterwards
*
* // A textblock may also have multiple lines.
* // Those lines can be uppercase as well to support
* // sentence breaks in textblocks
*
* // 123 or any non-alphabetical starting character
* // @are also valid anywhere
*
* // jscs: enable
* ```
*
* ##### Invalid:
*
* ```js
* // invalid
* //invalid
* /** invalid *\/
* /**
* * invalid
* *\/
* ```
*
* ```js
* "requireCapitalizedComments": { "allExcept": ["pragma"] }
* ```
*
* ##### Valid:
*
* ```js
* function sayHello() {
* /* pragma something *\/
*
* // I can now say hello in lots of statements, if I like.
* return "Hello";
* }
* ```
*
* ##### Valid:
*
* ```js
* function sayHello() {
* /* istanbul ignore next *\/
*
* // I'd like to ignore this statement in coverage reports.
* return "Hello";
* }
* ```
*
* ##### Invalid:
*
* ```js
* function sayHello() {
* /* otherPragma something *\/
*
* // i can now say hello in lots of statements, if I like.
* return "Hello";
* }
* ```
*
* ```js
* "requireCapitalizedComments": { "inlined": true }
* ```
* ##### Valid:
*
* ```js
* function sayHello( world /*internal*\/ ) {
* }
* ```
*/
var assert = require('assert');
var cst = require('cst');
var isPragma = require('../utils').isPragma;
var letterPattern = require('../../patterns/L');
var upperCasePattern = require('../../patterns/Lu');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
var exceptions;
this.inlined = false;
this._isPragma = null;
var optionName = this.getOptionName();
var isObject = typeof options === 'object';
var error = optionName + ' option requires a true value ' +
'or an object with String[] `allExcept` property or true with `inlined`';
assert(
options === true ||
isObject,
error
);
if (isObject && options.allExcept) {
exceptions = options.allExcept;
// verify items in `allExcept` property in object are string values
assert(
Array.isArray(exceptions) &&
exceptions.every(function(el) { return typeof el === 'string'; }),
'Property `allExcept` in ' + optionName + ' should be an array of strings'
);
this._isPragma = isPragma(exceptions);
}
if (!this._isPragma) {
this._isPragma = isPragma();
}
if (isObject && options.inlined) {
this.inlined = true;
}
if (isObject && !options.allExcept && !options.inlined) {
assert(false, error);
}
},
getOptionName: function() {
return 'requireCapitalizedComments';
},
_isUrl: function(comment) {
var protocolParts = comment.value.split('://');
if (protocolParts.length === 1) {
return false;
}
return comment.value.indexOf(protocolParts[0]) === 0;
},
_isException: function(comment) {
return this._isPragma(comment.value);
},
_isValid: function(comment) {
var first = this._getFirstChar(comment);
return first && upperCasePattern.test(first);
},
_isLetter: function(comment) {
var first = this._getFirstChar(comment);
return first && letterPattern.test(first);
},
_getFirstChar: function(comment) {
return comment.value.replace(/[\n\s\*]/g, '')[0];
},
_isTextBlock: function(file, comment) {
var prevComment = file.getPrevToken(comment, {includeComments: true});
if (prevComment) {
return prevComment.type === 'CommentLine' &&
!file.isOnTheSameLine(comment, prevComment) &&
prevComment.value.trim().length > 0;
}
return false;
},
_shouldIgnoreIfInTheMiddle: function(file, comment) {
if (!this.inlined) {
return false;
}
var firstToken = comment.getFirstToken();
var otherToken = firstToken.getPreviousNonWhitespaceToken();
return otherToken ? file.isOnTheSameLine(otherToken, firstToken) : false;
},
check: function(file, errors) {
var _this = this;
function add(comment) {
errors.cast({
message: 'Comments must start with an uppercase letter, unless it is part of a textblock',
element: comment,
additional: comment
});
}
file.iterateTokensByType('CommentLine', function(comment) {
if (_this._isException(comment)) {
return;
}
if (_this._isUrl(comment)) {
return;
}
if (!_this._isLetter(comment)) {
return;
}
if (_this._isTextBlock(file, comment)) {
return;
}
if (_this._isValid(comment)) {
return;
}
add(comment);
});
file.iterateTokensByType('CommentBlock', function(comment) {
if (_this._isException(comment)) {
return;
}
if (_this._isUrl(comment)) {
return;
}
if (!_this._isLetter(comment)) {
return;
}
if (_this._shouldIgnoreIfInTheMiddle(file, comment)) {
return;
}
if (_this._isValid(comment)) {
return;
}
add(comment);
});
},
_fix: function(file, error) {
var comment = error.additional;
var first = this._getFirstChar(comment);
var newValue = comment.value.replace(first, first.toUpperCase());
var newComment = new cst.Token(comment.type, newValue);
comment.parentElement.replaceChild(newComment, comment);
}
};