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 pathmaximum-line-length.js
205 lines (176 loc) · 6.65 KB
/
maximum-line-length.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
/**
* Requires all lines to be at most the number of characters specified
*
* Types: `Integer` or `Object`
*
* Values:
* - `Integer`: lines should be at most the number of characters specified
* - `Object`:
* - `value`: (required) lines should be at most the number of characters specified
* - `tabSize`: (default: `1`) considered the tab character as number of specified spaces
* - `allExcept`: (default: `[]`) an array of conditions that will exempt a line
* - `regex`: allows regular expression literals to break the rule
* - `comments`: allows comments to break the rule
* - `urlComments`: allows comments with long urls to break the rule
* - `functionSignature`: allows function definitions to break the rule
* - `require`: allows require expressions to break the rule
* - `allowRegex`: *deprecated* use `allExcept: ["regex"]` instead
* - `allowComments`: *deprecated* use `allExcept: ["comments"]` instead
* - `allowUrlComments`: *deprecated* use `allExcept: ["urlComments"]` instead
*
* JSHint: [`maxlen`](http://jshint.com/docs/options/#maxlen)
*
* #### Example
*
* ```js
* "maximumLineLength": 40
* ```
*
* ##### Valid
*
* ```js
* var aLineOf40Chars = 123456789012345678;
* ```
*
* ##### Invalid
*
* ```js
* var aLineOf41Chars = 1234567890123456789;
* ```
*
* #### Example for allExcept functionSignature
*
* ```js
* "maximumLineLength": { "value": 40, "allExcept": [ "functionSignature" ] }
* ```
*
* ##### Valid
*
* ```js
* var f = function(with, many, _many_, arguments) { .... };
* let f = x => x * x * x * x * x * x * x * x;
* (function(foo, bar, baz, quux, cuttlefish) {
* function namesNaamesNaaamesNaaaames() {
* ...
* }
* })();
* const longNameIgnoredAsWell = (a, b) => a * b;
* class X { myLongMethodName(withPossiblyManyArgs) { ... } };
* ```
*
* ##### Invalid
*
* ```js
* function x() { // valid
* return "function_bodies_are_not_protected";
* }
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(maximumLineLength) {
this._tabSize = '';
this._allowRegex = false;
this._allowComments = false;
this._allowUrlComments = false;
this._allowRequire = false;
if (typeof maximumLineLength === 'object') {
assert(
typeof maximumLineLength.value === 'number',
this.getOptionName() + ' option requires the "value" property to be defined'
);
this._maximumLineLength = maximumLineLength.value;
var tabSize = maximumLineLength.tabSize || 0;
while (tabSize--) {
this._tabSize += ' ';
}
var exceptions = maximumLineLength.allExcept || [];
this._allowRegex = (exceptions.indexOf('regex') !== -1);
this._allowComments = (exceptions.indexOf('comments') !== -1);
this._allowUrlComments = (exceptions.indexOf('urlComments') !== -1);
this._allowFunctionSignature = (exceptions.indexOf('functionSignature') !== -1);
this._allowRequire = (exceptions.indexOf('require') !== -1);
if (maximumLineLength.hasOwnProperty('allowRegex')) {
this._allowRegex = (maximumLineLength.allowRegex === true);
}
if (maximumLineLength.hasOwnProperty('allowComments')) {
this._allowComments = (maximumLineLength.allowComments === true);
}
if (maximumLineLength.hasOwnProperty('allowUrlComments')) {
this._allowUrlComments = (maximumLineLength.allowUrlComments === true);
}
} else {
assert(
typeof maximumLineLength === 'number',
this.getOptionName() + ' option requires number value or options object'
);
this._maximumLineLength = maximumLineLength;
}
},
getOptionName: function() {
return 'maximumLineLength';
},
check: function(file, errors) {
var maximumLineLength = this._maximumLineLength;
var line;
var lines = this._allowComments ?
file.getLinesWithCommentsRemoved() : file.getLines();
// This check should not be destructive
lines = lines.slice();
var removeLoc = function(tokenOrNode) {
// Just in case (See #2107 for example)
if (!tokenOrNode) {
return;
}
for (var i = tokenOrNode.getLoc().start.line; i <= tokenOrNode.getLoc().end.line; i++) {
lines[i - 1] = '';
}
};
if (this._allowRegex) {
file.iterateTokensByType('RegularExpression', function(token) {
removeLoc(token);
});
}
if (this._allowUrlComments) {
file.iterateTokensByType(['CommentLine', 'CommentBlock'], function(comment) {
for (var i = comment.getLoc().start.line; i <= comment.getLoc().end.line; i++) {
lines[i - 1] = lines[i - 1].replace(/(http|https|ftp):\/\/[^\s$]+/, '');
}
});
}
if (this._allowFunctionSignature) {
file.iterateNodesByType('FunctionDeclaration', function(node) {
// Need to remove the first line, because we can't be sure there's any id or params
lines[node.getLoc().start.line - 1] = '';
removeLoc(node.id);
node.params.forEach(removeLoc);
});
file.iterateNodesByType('ClassMethod', function(node) {
removeLoc(node.key);
});
file.iterateNodesByType(['ArrowFunctionExpression', 'FunctionExpression'], function(node) {
// Need to remove the first line, because we can't be sure there's any id or params
lines[node.getLoc().start.line - 1] = '';
removeLoc(node.id);
node.params.forEach(removeLoc);
});
}
if (this._allowRequire) {
file.iterateNodesByType('CallExpression', function(node) {
if (node.callee.name === 'require') {
removeLoc(node);
}
});
}
for (var i = 0, l = lines.length; i < l; i++) {
line = this._tabSize ? lines[i].replace(/\t/g, this._tabSize) : lines[i];
if (line.length > maximumLineLength) {
errors.add(
'Line must be at most ' + maximumLineLength + ' characters',
file.getLastTokenOnLine(i + 1, { includeComments: true })
);
}
}
}
};