7b76d76db99d7dc67e1a49fad3a7bbfdcb5596a4
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Disallow mixed spaces and tabs for indentation
3  * @author Jary Niebur
4  * @copyright 2014 Nicholas C. Zakas. All rights reserved.
5  * @copyright 2014 Jary Niebur. All rights reserved.
6  */
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function(context) {
14
15     var smartTabs;
16
17     switch (context.options[0]) {
18         case true: // Support old syntax, maybe add deprecation warning here
19         case "smart-tabs":
20             smartTabs = true;
21             break;
22         default:
23             smartTabs = false;
24     }
25
26     var COMMENT_START = /^\s*\/\*/,
27         MAYBE_COMMENT = /^\s*\*/;
28
29     //--------------------------------------------------------------------------
30     // Public
31     //--------------------------------------------------------------------------
32
33     return {
34
35         "Program": function(node) {
36             /*
37              * At least one space followed by a tab
38              * or the reverse before non-tab/-space
39              * characters begin.
40              */
41             var regex = /^(?=[\t ]*(\t | \t))/,
42                 match,
43                 lines = context.getSourceLines();
44
45             if (smartTabs) {
46                 /*
47                  * At least one space followed by a tab
48                  * before non-tab/-space characters begin.
49                  */
50                 regex = /^(?=[\t ]* \t)/;
51             }
52
53             lines.forEach(function(line, i) {
54                 match = regex.exec(line);
55
56                 if (match) {
57
58                     if (!MAYBE_COMMENT.test(line) && !COMMENT_START.test(lines[i - 1])) {
59                         context.report(node, { line: i + 1, column: match.index + 1 }, "Mixed spaces and tabs.");
60                     }
61
62                 }
63             });
64         }
65
66     };
67
68 };