Blame view

node_modules/eslint/lib/rules/no-multiple-empty-lines.js 4.17 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
  /**
   * @fileoverview Disallows multiple blank lines.
   * implementation adapted from the no-trailing-spaces rule.
   * @author Greg Cochard
   * @copyright 2014 Greg Cochard. All rights reserved.
   */
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = function(context) {
  
      // Use options.max or 2 as default
      var max = 2,
          maxEOF;
  
      // store lines that appear empty but really aren't
      var notEmpty = [];
  
      if (context.options.length) {
          max = context.options[0].max;
          maxEOF = context.options[0].maxEOF;
      }
  
      //--------------------------------------------------------------------------
      // Public
      //--------------------------------------------------------------------------
  
      return {
  
          "TemplateLiteral": function(node) {
              var start = node.loc.start.line;
              var end = node.loc.end.line;
              while (start <= end) {
                  notEmpty.push(start);
                  start++;
              }
          },
  
          "Program:exit": function checkBlankLines(node) {
              var lines = context.getSourceLines(),
                  currentLocation = -1,
                  lastLocation,
                  blankCounter = 0,
                  location,
                  trimmedLines = lines.map(function(str) {
                      return str.trim();
                  }),
                  firstOfEndingBlankLines;
  
              // add the notEmpty lines in there with a placeholder
              notEmpty.forEach(function(x, i) {
                  trimmedLines[i] = x;
              });
  
              if (typeof maxEOF === "undefined") {
                  // swallow the final newline, as some editors add it
                  // automatically and we don't want it to cause an issue
                  if (trimmedLines[trimmedLines.length - 1] === "") {
                      trimmedLines = trimmedLines.slice(0, -1);
                  }
                  firstOfEndingBlankLines = trimmedLines.length;
              } else {
                  // save the number of the first of the last blank lines
                  firstOfEndingBlankLines = trimmedLines.length;
                  while (trimmedLines[firstOfEndingBlankLines - 1] === ""
                          && firstOfEndingBlankLines > 0) {
                      firstOfEndingBlankLines--;
                  }
              }
  
              // Aggregate and count blank lines
              lastLocation = currentLocation;
              currentLocation = trimmedLines.indexOf("", currentLocation + 1);
              while (currentLocation !== -1) {
                  lastLocation = currentLocation;
                  currentLocation = trimmedLines.indexOf("", currentLocation + 1);
                  if (lastLocation === currentLocation - 1) {
                      blankCounter++;
                  } else {
                      location = {
                          line: lastLocation + 1,
                          column: 1
                      };
                      if (lastLocation < firstOfEndingBlankLines) {
                          // within the file, not at the end
                          if (blankCounter >= max) {
                              context.report(node, location,
                                      "More than " + max + " blank " + (max === 1 ? "line" : "lines") + " not allowed.");
                          }
                      } else {
                          // inside the last blank lines
                          if (blankCounter >= maxEOF) {
                              context.report(node, location,
                                      "Too many blank lines at the end of file. Max of " + maxEOF + " allowed.");
                          }
                      }
  
                      // Finally, reset the blank counter
                      blankCounter = 0;
                  }
              }
          }
      };
  
  };
  
  module.exports.schema = [
      {
          "type": "object",
          "properties": {
              "max": {
                  "type": "integer"
              },
              "maxEOF": {
                  "type": "integer"
              }
          },
          "required": ["max"],
          "additionalProperties": false
      }
  ];