UNPKG

1.51 kBJavaScriptView Raw
1/**
2 * Require arrow functions to use a block statement (explicit return).
3 *
4 * Why enable this rule? Arrow functions' syntax can cause maintenance issues:
5 *
6 * - When you add additional lines to an arrow function's expression body, the
7 * function will now return `undefined`, unless you remember to add an
8 * explicit `return`.
9 * - The shorthand syntax is ambiguous in terms of returning objects.
10 * `(name) => {id: name}` is interpreted as a longhand arrow function with the
11 * label `id:`.
12 *
13 * Type: `Boolean`
14 *
15 * Value: `true`
16 *
17 * Version: `ES6`
18 *
19 * #### Example
20 *
21 * ```js
22 * "disallowShorthandArrowFunctions": true
23 * ```
24 *
25 * ##### Valid
26 *
27 * ```js
28 * // block statement
29 * evens.map(v => {
30 * return v + 1;
31 * });
32 * ```
33 *
34 * ##### Invalid
35 *
36 * ```js
37 * // single expression
38 * evens.map(v => v + 1);
39 * ```
40 */
41
42var assert = require('assert');
43
44module.exports = function() {};
45
46module.exports.prototype = {
47
48 configure: function(options) {
49 assert(
50 options === true,
51 this.getOptionName() + ' option requires a true value or should be removed'
52 );
53 },
54
55 getOptionName: function() {
56 return 'disallowShorthandArrowFunctions';
57 },
58
59 check: function(file, errors) {
60 file.iterateNodesByType('ArrowFunctionExpression', function(node) {
61 if (node.expression) {
62 errors.add('Use arrow function with explicit block and explicit return', node.body);
63 }
64 });
65 }
66
67};