Blame view

node_modules/eslint/lib/rules/no-fallthrough.js 3.14 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
  /**
   * @fileoverview Rule to flag fall-through cases in switch statements.
   * @author Matt DuVall <http://mattduvall.com/>
   */
  "use strict";
  
  
  var FALLTHROUGH_COMMENT = /falls?\s?through/i;
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      var switches = [];
  
      return {
  
          "SwitchCase": function(node) {
  
              var consequent = node.consequent,
                  switchData = switches[switches.length - 1],
                  i,
                  comments,
                  comment;
  
              /*
               * Some developers wrap case bodies in blocks, so if there is just one
               * node and it's a block statement, check inside.
               */
              if (consequent.length === 1 && consequent[0].type === "BlockStatement") {
                  consequent = consequent[0];
              }
  
              // checking on previous case
              if (!switchData.lastCaseClosed) {
  
                  // a fall through comment will be the last trailing comment of the last case
                  comments = context.getComments(switchData.lastCase).trailing;
                  comment = comments[comments.length - 1];
  
                  // unless the user doesn't like semicolons, in which case it's first leading comment of this case
                  if (!comment) {
                      comments = context.getComments(node).leading;
                      comment = comments[comments.length - 1];
                  }
  
                  // check for comment
                  if (!comment || !FALLTHROUGH_COMMENT.test(comment.value)) {
                      context.report(node,
                          "Expected a \"break\" statement before \"{{code}}\".",
                          { code: node.test ? "case" : "default" });
                  }
              }
  
              // now dealing with the current case
              switchData.lastCaseClosed = false;
              switchData.lastCase = node;
  
              // try to verify using statements - go backwards as a fast path for the search
              if (consequent.length) {
                  for (i = consequent.length - 1; i >= 0; i--) {
                      if (/(?:Break|Continue|Return|Throw)Statement/.test(consequent[i].type)) {
                          switchData.lastCaseClosed = true;
                          break;
                      }
                  }
              } else {
                  // the case statement has no statements, so it must logically fall through
                  switchData.lastCaseClosed = true;
              }
  
              /*
               * Any warnings are triggered when the next SwitchCase occurs.
               * There is no need to warn on the last SwitchCase, since it can't
               * fall through to anything.
               */
          },
  
          "SwitchStatement": function(node) {
              switches.push({
                  node: node,
                  lastCaseClosed: true,
                  lastCase: null
              });
          },
  
          "SwitchStatement:exit": function() {
              switches.pop();
          }
      };
  
  };
  
  module.exports.schema = [];