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 pathdisallow-parentheses-around-arrow-param.js
79 lines (67 loc) · 2.01 KB
/
disallow-parentheses-around-arrow-param.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
/**
* Disallows parentheses around arrow function expressions with a single parameter.
*
* Type: `Boolean`
*
* Value: `true`
*
* Version: `ES6`
*
* #### Example
*
* ```js
* "disallowParenthesesAroundArrowParam": true
* ```
*
* ##### Valid
*
* ```js
* [1, 2, 3].map(x => x * x);
* // parentheses are always required for multiple parameters
* [1, 2, 3].map((x, y, z) => x * x);
* ```
*
* ##### Invalid
*
* ```js
* [1, 2, 3].map((x) => x * x);
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true,
this.getOptionName() + ' option requires a true value or should be removed'
);
},
getOptionName: function() {
return 'disallowParenthesesAroundArrowParam';
},
check: function(file, errors) {
function isWrapped(node) {
var openParensToken = file.getPrevToken(file.getFirstNodeToken(node));
var closingParensToken = file.getNextToken(file.getLastNodeToken(node));
var closingTokenValue = closingParensToken ? closingParensToken.value : '';
return openParensToken.value + closingTokenValue === '()';
}
file.iterateNodesByType('ArrowFunctionExpression', function(node) {
if (node.params.length !== 1) {
return;
}
var firstParam = node.params[0];
var hasDefaultParameter = firstParam.type === 'AssignmentPattern';
var hasDestructuring = firstParam.type === 'ObjectPattern' || firstParam.type === 'ArrayPattern';
var hasRestElement = firstParam.type === 'RestElement';
if (hasDefaultParameter ||
hasDestructuring ||
hasRestElement) {
return;
}
if (isWrapped(firstParam)) {
errors.add('Illegal wrap of arrow function expressions in parentheses', firstParam);
}
});
}
};