Blame view

node_modules/eslint/lib/rules/arrow-body-style.js 2.13 KB
c39994410   Ryan Glover   wip converting to...
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
  /**
   * @fileoverview Rule to require braces in arrow function body.
   * @author Alberto Rodríguez
   * @copyright 2015 Alberto Rodríguez. All rights reserved.
   * See LICENSE file in root directory for full license.
   */
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
      var always = context.options[0] === "always";
      var asNeeded = !context.options[0] || context.options[0] === "as-needed";
  
      /**
       * Determines whether a arrow function body needs braces
       * @param {ASTNode} node The arrow function node.
       * @returns {void}
       */
      function validate(node) {
          var arrowBody = node.body;
          if (arrowBody.type === "BlockStatement") {
              var blockBody = arrowBody.body;
  
              if (blockBody.length > 1) {
                  return;
              }
  
              if (blockBody.length === 0) {
                  var hasComments = context.getComments(arrowBody).trailing.length > 0;
                  if (hasComments) {
                      return;
                  }
  
                  context.report({
                      node: node,
                      loc: arrowBody.loc.start,
                      message: "Unexpected empty block in arrow body."
                  });
              } else {
                  if (asNeeded && blockBody[0].type === "ReturnStatement") {
                      context.report({
                          node: node,
                          loc: arrowBody.loc.start,
                          message: "Unexpected block statement surrounding arrow body."
                      });
                  }
              }
          } else {
              if (always) {
                  context.report({
                      node: node,
                      loc: arrowBody.loc.start,
                      message: "Expected block statement surrounding arrow body."
                  });
              }
          }
      }
  
      return {
          "ArrowFunctionExpression": validate
      };
  };
  
  module.exports.schema = [
      {
          "enum": ["always", "as-needed"]
      }
  ];