UNPKG

1.97 kBJavaScriptView Raw
1/**
2 * Requires the file to be at most the number of lines specified
3 *
4 * Types: `Integer` or `Object`
5 *
6 * Values:
7 * - `Integer`: file should be at most the number of lines specified
8 * - `Object`:
9 * - `value`: (required) lines should be at most the number of characters specified
10 * - `allExcept`: (default: `[]`) an array of conditions that will exempt a line
11 * - `comments`: allows comments to break the rule
12*
13 * #### Example
14 *
15 * ```js
16 * "maximumNumberOfLines": 100
17 * ```
18 */
19
20var assert = require('assert');
21
22module.exports = function() {};
23
24module.exports.prototype = {
25
26 configure: function(options) {
27 this._allowComments = true;
28
29 if (typeof options === 'number') {
30 assert(
31 typeof options === 'number',
32 this.getOptionName() + ' option requires number value or options object'
33 );
34 this._maximumNumberOfLines = options;
35 } else {
36 assert(
37 typeof options.value === 'number',
38 this.getOptionName() + ' option requires the "value" property to be defined'
39 );
40 this._maximumNumberOfLines = options.value;
41
42 var exceptions = options.allExcept || [];
43 this._allowComments = (exceptions.indexOf('comments') === -1);
44 }
45 },
46
47 getOptionName: function() {
48 return 'maximumNumberOfLines';
49 },
50
51 check: function(file, errors) {
52 var firstToken = file.getFirstToken({includeComments: true});
53 var lines = this._allowComments ?
54 file.getLines() : file.getLinesWithCommentsRemoved();
55
56 lines = lines.filter(function(line) {return line !== '';});
57
58 if (lines.length > this._maximumNumberOfLines) {
59 errors.add(
60 'File must be at most ' + this._maximumNumberOfLines + ' lines long',
61 firstToken,
62 firstToken.value.length
63 );
64 }
65 }
66
67};