This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/
require-aligned-multiline-params.js
127 lines (114 loc) · 3.19 KB
/
require-aligned-multiline-params.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
/**
* Enforces indentation of parameters in multiline functions
*
* Types: `Boolean`, `String`, `Number`
*
* Values:
* - `true` to require parameters are aligned with the body of the function
* - `'firstParam'` to require parameters to be aligned with the first parameter
* - `Number` for the number of columns the parameters should be indented past the function body
*
* #### Example
*
* ```js
* "disallowSpaceBeforeComma": true
* ```
*
* ##### Valid for `true`
*
* ```js
* var test = function(one, two,
* three, four, five,
* six, seven, eight) {
* console.log(a);
* };
* ```
*
* ##### Valid for `2`
*
* ```js
* var test = function(one, two,
* three, four, five,
* six, seven, eight) {
* console.log(a);
* };
* ```
*
* ##### Valid for `'firstParam'`
*
* ```js
* var test = function(one, two,
* three, four, five,
* six, seven, eight) {
* console.log(a);
* };
* ```
*
* ##### Invalid for `0`
*
* ```js
* var test = function(one, two,
* three, four, five,
* six, seven, eight) {
* console.log(a);
* };
* ```
*
*/
var assert = require('assert');
module.exports = function() {
};
module.exports.prototype = {
configure: function(option) {
if (typeof option === 'number') {
this._indentationLevel = option;
} else if (typeof option === 'string') {
assert(
option === 'firstParam',
this.getOptionName() + ' option requires string value to be "firstParam"'
);
this._alignWithFirstParam = true;
} else if (option === true) {
this._indentationLevel = 0;
} else {
assert(
false,
this.getOptionName() + ' option requires a valid option'
);
}
},
getOptionName: function() {
return 'requireAlignedMultilineParams';
},
check: function(file, errors) {
var _this = this;
file.iterateNodesByType(['FunctionDeclaration', 'FunctionExpression'], function(node) {
var params = node.params;
// We can pass the check if there's no params
if (params.length === 0) {
return;
}
var currentLine = params[0].loc.start.line;
var referenceColumn;
if (_this._alignWithFirstParam) {
referenceColumn = params[0].loc.start.column;
} else {
referenceColumn = node.body.body[0].loc.start.column + _this._indentationLevel;
}
params.forEach(function(param) {
if (param.loc.start.line !== currentLine) {
if (param.loc.start.column !== referenceColumn) {
errors.assert.indentation({
lineNumber: param.loc.start.line,
actual: param.loc.start.column,
expected: referenceColumn,
indentChar: ' ',
silent: false
});
}
currentLine = param.loc.start.line;
}
});
});
}
};