UNPKG

1.66 kBJavaScriptView Raw
1/**
2 * Requires parentheses around arrow function expressions with a single parameter.
3 *
4 * Type: `Boolean`
5 *
6 * Value: `true`
7 *
8 * Version: `ES6`
9 *
10 * #### Example
11 *
12 * ```js
13 * "requireParenthesesAroundArrowParam": true
14 * ```
15 *
16 * ##### Valid
17 *
18 * ```js
19 * [1, 2, 3].map((x) => x * x);
20 * // params are always required for multiple parameters
21 * [1, 2, 3].map((x, y, z) => x * x);
22 * ```
23 *
24 * ##### Invalid
25 *
26 * ```js
27 * [1, 2, 3].map(x => x * x);
28 * ```
29 */
30
31var assert = require('assert');
32
33module.exports = function() {};
34
35module.exports.prototype = {
36
37 configure: function(options) {
38 assert(
39 options === true,
40 this.getOptionName() + ' option requires a true value or should be removed'
41 );
42 },
43
44 getOptionName: function() {
45 return 'requireParenthesesAroundArrowParam';
46 },
47
48 check: function(file, errors) {
49 function isWrapped(node) {
50 var openParensToken = file.getPrevToken(file.getFirstNodeToken(node));
51 var closingParensToken = file.getNextToken(file.getLastNodeToken(node));
52 var closingTokenValue = closingParensToken ? closingParensToken.value : '';
53
54 return openParensToken.value + closingTokenValue === '()';
55 }
56
57 file.iterateNodesByType('ArrowFunctionExpression', function(node) {
58 var params = node.params;
59 var firstParam = params[0];
60
61 if (params.length === 1 && !isWrapped(firstParam)) {
62 errors.add(
63 'Wrap arrow function expressions in parentheses',
64 firstParam
65 );
66 }
67 });
68 }
69
70};