Blame view

node_modules/eslint/lib/rules/semi.js 5 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  /**
   * @fileoverview Rule to flag missing semicolons.
   * @author Nicholas C. Zakas
   * @copyright 2013 Nicholas C. Zakas. All rights reserved.
   * See LICENSE file in root directory for full license.
   */
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      var OPT_OUT_PATTERN = /[\[\(\/\+\-]/; // One of [(/+-
  
      var always = context.options[0] !== "never",
          sourceCode = context.getSourceCode();
  
      //--------------------------------------------------------------------------
      // Helpers
      //--------------------------------------------------------------------------
  
      /**
       * Reports a semicolon error with appropriate location and message.
       * @param {ASTNode} node The node with an extra or missing semicolon.
       * @returns {void}
       */
      function report(node) {
          var message,
              fix,
              lastToken = sourceCode.getLastToken(node),
              loc = lastToken.loc;
  
          if (always) {
              message = "Missing semicolon.";
              loc = loc.end;
              fix = function(fixer) {
                  return fixer.insertTextAfter(lastToken, ";");
              };
          } else {
              message = "Extra semicolon.";
              loc = loc.start;
              fix = function(fixer) {
                  return fixer.remove(lastToken);
              };
          }
  
          context.report({
              node: node,
              loc: loc,
              message: message,
              fix: fix
          });
  
      }
  
      /**
       * Checks whether a token is a semicolon punctuator.
       * @param {Token} token The token.
       * @returns {boolean} True if token is a semicolon punctuator.
       */
      function isSemicolon(token) {
          return (token.type === "Punctuator" && token.value === ";");
      }
  
      /**
       * Check if a semicolon is unnecessary, only true if:
       *   - next token is on a new line and is not one of the opt-out tokens
       *   - next token is a valid statement divider
       * @param {Token} lastToken last token of current node.
       * @returns {boolean} whether the semicolon is unnecessary.
       */
      function isUnnecessarySemicolon(lastToken) {
          var isDivider, isOptOutToken, lastTokenLine, nextToken, nextTokenLine;
  
          if (!isSemicolon(lastToken)) {
              return false;
          }
  
          nextToken = context.getTokenAfter(lastToken);
  
          if (!nextToken) {
              return true;
          }
  
          lastTokenLine = lastToken.loc.end.line;
          nextTokenLine = nextToken.loc.start.line;
          isOptOutToken = OPT_OUT_PATTERN.test(nextToken.value);
          isDivider = (nextToken.value === "}" || nextToken.value === ";");
  
          return (lastTokenLine !== nextTokenLine && !isOptOutToken) || isDivider;
      }
  
      /**
       * Checks a node to see if it's followed by a semicolon.
       * @param {ASTNode} node The node to check.
       * @returns {void}
       */
      function checkForSemicolon(node) {
          var lastToken = context.getLastToken(node);
  
          if (always) {
              if (!isSemicolon(lastToken)) {
                  report(node);
              }
          } else {
              if (isUnnecessarySemicolon(lastToken)) {
                  report(node);
              }
          }
      }
  
      /**
       * Checks to see if there's a semicolon after a variable declaration.
       * @param {ASTNode} node The node to check.
       * @returns {void}
       */
      function checkForSemicolonForVariableDeclaration(node) {
          var ancestors = context.getAncestors(),
              parentIndex = ancestors.length - 1,
              parent = ancestors[parentIndex];
  
          if ((parent.type !== "ForStatement" || parent.init !== node) &&
              (!/^For(?:In|Of)Statement/.test(parent.type) || parent.left !== node)
          ) {
              checkForSemicolon(node);
          }
      }
  
      //--------------------------------------------------------------------------
      // Public API
      //--------------------------------------------------------------------------
  
      return {
  
          "VariableDeclaration": checkForSemicolonForVariableDeclaration,
          "ExpressionStatement": checkForSemicolon,
          "ReturnStatement": checkForSemicolon,
          "ThrowStatement": checkForSemicolon,
          "DoWhileStatement": checkForSemicolon,
          "DebuggerStatement": checkForSemicolon,
          "BreakStatement": checkForSemicolon,
          "ContinueStatement": checkForSemicolon,
          "ImportDeclaration": checkForSemicolon,
          "ExportAllDeclaration": checkForSemicolon,
          "ExportNamedDeclaration": function(node) {
              if (!node.declaration) {
                  checkForSemicolon(node);
              }
          },
          "ExportDefaultDeclaration": function(node) {
              if (!/(?:Class|Function)Declaration/.test(node.declaration.type)) {
                  checkForSemicolon(node);
              }
          }
      };
  
  };
  
  module.exports.schema = [
      {
          "enum": ["always", "never"]
      }
  ];