This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/
validate-aligned-function-parameters.js
141 lines (122 loc) · 4.61 KB
/
validate-aligned-function-parameters.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
/**
* Validates proper alignment of function parameters.
*
* Types: `Boolean`, `Object`
*
* Values:
* - `true`: setting this is the same as validating the rule using
* `{lineBreakAfterOpeningBrace: true, lineBreakBeforeClosingBrace: true}`
* - `Object`:
* - `lineBreakAfterOpeningBrace`
* - `true` specifies that the first function parameter must not be on the same line as the opening brace `(`
* of the function parameters list
* - `lineBreakBeforeClosingBrace`
* - `true` specifies that the last function parameter must not be on the same line as the closing brace `)`
* of the function parameters list
*
* #### Example
*
* ```js
* "validateAlignedFunctionParameters": {
* "lineBreakAfterOpeningBrace": true,
* "lineBreakBeforeClosingBrace": true
* }
* ```
*
* ##### Valid for mode `{ "lineBreakAfterOpeningBrace": true, "lineBreakBeforeClosingBrace": true}`
* ```js
* function (
* thisIs,
* theLongestList,
* ofParametersEverWritten
* ) {}
* ```
* ##### Invalid for mode `{ "lineBreakAfterOpeningBrace": true, "lineBreakBeforeClosingBrace": true}`
* ```js
* function (thisIs,
* theLongestList,
* ofParametersEverWritten) {}
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
var validProperties = [
'lineBreakAfterOpeningBrace',
'lineBreakBeforeClosingBrace'
];
var optionName = this.getOptionName();
assert(
typeof options === 'object' || options === true,
optionName + ' option must be an object or boolean true'
);
if (typeof options === 'object') {
validProperties.forEach(function(key) {
var isPresent = key in options;
if (isPresent) {
assert(
options[key] === true,
optionName + '.' + key + ' property requires true value or should be removed'
);
}
});
validProperties.forEach(function(property) {
this['_' + property] = Boolean(options[property]);
}.bind(this));
}
},
getOptionName: function() {
return 'validateAlignedFunctionParameters';
},
check: function(file, errors) {
var lineBreakAfterOpeningBrace = this._lineBreakAfterOpeningBrace;
var lineBreakBeforeClosingBrace = this._lineBreakBeforeClosingBrace;
file.iterateNodesByType([
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression'
], function(node) {
// ignore this rule if there are no parameters
if (node.params.length === 0) {
return;
}
// ignore this rule if the parameters are not multi-line
var firstParameter = file.getFirstNodeToken(node.params[0]);
var lastParameter = node.params[node.params.length - 1];
if (file.isOnTheSameLine(firstParameter, lastParameter)) {
return;
}
// look for the furthest parameter start position
var maxParamStartPos = 0;
node.params.forEach(function(parameter) {
maxParamStartPos = Math.max(maxParamStartPos, parameter.getLoc().start.column);
});
// make sure all parameters are lined up
node.params.forEach(function(parameter) {
if (parameter.getLoc().start.column !== maxParamStartPos) {
errors.add('Multi-line parameters are not aligned.', parameter);
}
});
// make sure the first parameter is on a new line
if (lineBreakAfterOpeningBrace) {
var openingBrace = file.getPrevToken(firstParameter);
errors.assert.differentLine({
token: openingBrace,
nextToken: firstParameter,
message: 'There is no line break after the opening brace'
});
}
// make sure the closing brace is on a new line
if (lineBreakBeforeClosingBrace) {
var bodyToken = file.getFirstNodeToken(node.body);
var closingBrace = file.getPrevToken(bodyToken);
errors.assert.differentLine({
token: lastParameter,
nextToken: closingBrace,
message: 'There is no line break before the closing brace'
});
}
});
}
};