9cfea63daab2fc45d3a77ca3c957833e5f74ffa0
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Disallows multiple blank lines.
3  * implementation adapted from the no-trailing-spaces rule.
4  * @author Greg Cochard
5  * @copyright 2014 Greg Cochard. All rights reserved.
6  */
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function(context) {
14
15     // Use options.max or 2 as default
16     var numLines = 2;
17
18     if (context.options.length) {
19         numLines = context.options[0].max;
20     }
21
22     //--------------------------------------------------------------------------
23     // Public
24     //--------------------------------------------------------------------------
25
26     return {
27
28         "Program": function checkBlankLines(node) {
29             var lines = context.getSourceLines(),
30                 currentLocation = -1,
31                 lastLocation,
32                 blankCounter = 0,
33                 location,
34                 trimmedLines = lines.map(function(str) {
35                     return str.trim();
36                 });
37
38             // Aggregate and count blank lines
39             do {
40                 lastLocation = currentLocation;
41                 currentLocation = trimmedLines.indexOf("", currentLocation + 1);
42                 if (lastLocation === currentLocation - 1) {
43                     blankCounter++;
44                 } else {
45                     if (blankCounter >= numLines) {
46                         location = {
47                             line: lastLocation + 1,
48                             column: lines[lastLocation].length
49                         };
50                         context.report(node, location, "Multiple blank lines not allowed.");
51                     }
52
53                     // Finally, reset the blank counter
54                     blankCounter = 0;
55                 }
56             } while (currentLocation !== -1);
57         }
58     };
59
60 };