Blame view

node_modules/eslint/lib/rules/no-extra-semi.js 2.56 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
72
73
74
75
76
77
78
79
80
81
82
83
  /**
   * @fileoverview Rule to flag use of unnecessary semicolons
   * @author Nicholas C. Zakas
   */
  
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      /**
       * Reports an unnecessary semicolon error.
       * @param {Node|Token} nodeOrToken - A node or a token to be reported.
       * @returns {void}
       */
      function report(nodeOrToken) {
          context.report({
              node: nodeOrToken,
              message: "Unnecessary semicolon.",
              fix: function(fixer) {
                  return fixer.remove(nodeOrToken);
              }
          });
      }
  
      /**
       * Checks for a part of a class body.
       * This checks tokens from a specified token to a next MethodDefinition or the end of class body.
       *
       * @param {Token} firstToken - The first token to check.
       * @returns {void}
       */
      function checkForPartOfClassBody(firstToken) {
          for (var token = firstToken;
              token.type === "Punctuator" && token.value !== "}";
              token = context.getTokenAfter(token)
          ) {
              if (token.value === ";") {
                  report(token);
              }
          }
      }
  
      return {
          /**
           * Reports this empty statement, except if the parent node is a loop.
           * @param {Node} node - A EmptyStatement node to be reported.
           * @returns {void}
           */
          "EmptyStatement": function(node) {
              var parent = node.parent,
                  allowedParentTypes = ["ForStatement", "ForInStatement", "ForOfStatement", "WhileStatement", "DoWhileStatement"];
  
              if (allowedParentTypes.indexOf(parent.type) === -1) {
                  report(node);
              }
          },
  
          /**
           * Checks tokens from the head of this class body to the first MethodDefinition or the end of this class body.
           * @param {Node} node - A ClassBody node to check.
           * @returns {void}
           */
          "ClassBody": function(node) {
              checkForPartOfClassBody(context.getFirstToken(node, 1)); // 0 is `{`.
          },
  
          /**
           * Checks tokens from this MethodDefinition to the next MethodDefinition or the end of this class body.
           * @param {Node} node - A MethodDefinition node of the start point.
           * @returns {void}
           */
          "MethodDefinition": function(node) {
              checkForPartOfClassBody(context.getTokenAfter(node));
          }
      };
  
  };
  
  module.exports.schema = [];