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-number-of-lines.js
67 lines (56 loc) · 1.92 KB
/
maximum-number-of-lines.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
/**
* Requires the file to be at most the number of lines specified
*
* Types: `Integer` or `Object`
*
* Values:
* - `Integer`: file should be at most the number of lines specified
* - `Object`:
* - `value`: (required) lines should be at most the number of characters specified
* - `allExcept`: (default: `[]`) an array of conditions that will exempt a line
* - `comments`: allows comments to break the rule
*
* #### Example
*
* ```js
* "maximumNumberOfLines": 100
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
this._allowComments = true;
if (typeof options === 'number') {
assert(
typeof options === 'number',
this.getOptionName() + ' option requires number value or options object'
);
this._maximumNumberOfLines = options;
} else {
assert(
typeof options.value === 'number',
this.getOptionName() + ' option requires the "value" property to be defined'
);
this._maximumNumberOfLines = options.value;
var exceptions = options.allExcept || [];
this._allowComments = (exceptions.indexOf('comments') === -1);
}
},
getOptionName: function() {
return 'maximumNumberOfLines';
},
check: function(file, errors) {
var firstToken = file.getFirstToken({includeComments: true});
var lines = this._allowComments ?
file.getLines() : file.getLinesWithCommentsRemoved();
lines = lines.filter(function(line) {return line !== '';});
if (lines.length > this._maximumNumberOfLines) {
errors.add(
'File must be at most ' + this._maximumNumberOfLines + ' lines long',
firstToken,
firstToken.value.length
);
}
}
};