UNPKG

4.54 kBJavaScriptView Raw
1/**
2 * @fileoverview Disallow mixed spaces and tabs for indentation
3 * @author Jary Niebur
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = {
12 meta: {
13 docs: {
14 description: "disallow mixed spaces and tabs for indentation",
15 category: "Stylistic Issues",
16 recommended: true
17 },
18
19 schema: [
20 {
21 enum: ["smart-tabs", true, false]
22 }
23 ]
24 },
25
26 create(context) {
27 const sourceCode = context.getSourceCode();
28
29 let smartTabs;
30 const ignoredLocs = [];
31
32 switch (context.options[0]) {
33 case true: // Support old syntax, maybe add deprecation warning here
34 case "smart-tabs":
35 smartTabs = true;
36 break;
37 default:
38 smartTabs = false;
39 }
40
41 /**
42 * Determines if a given line and column are before a location.
43 * @param {Location} loc The location object from an AST node.
44 * @param {int} line The line to check.
45 * @param {int} column The column to check.
46 * @returns {boolean} True if the line and column are before the location, false if not.
47 * @private
48 */
49 function beforeLoc(loc, line, column) {
50 if (line < loc.start.line) {
51 return true;
52 }
53 return line === loc.start.line && column < loc.start.column;
54 }
55
56 /**
57 * Determines if a given line and column are after a location.
58 * @param {Location} loc The location object from an AST node.
59 * @param {int} line The line to check.
60 * @param {int} column The column to check.
61 * @returns {boolean} True if the line and column are after the location, false if not.
62 * @private
63 */
64 function afterLoc(loc, line, column) {
65 if (line > loc.end.line) {
66 return true;
67 }
68 return line === loc.end.line && column > loc.end.column;
69 }
70
71 //--------------------------------------------------------------------------
72 // Public
73 //--------------------------------------------------------------------------
74
75 return {
76
77 TemplateElement(node) {
78 ignoredLocs.push(node.loc);
79 },
80
81 "Program:exit"(node) {
82
83 /*
84 * At least one space followed by a tab
85 * or the reverse before non-tab/-space
86 * characters begin.
87 */
88 let regex = /^(?=[\t ]*(\t | \t))/;
89 const lines = sourceCode.lines,
90 comments = sourceCode.getAllComments();
91
92 comments.forEach(comment => {
93 ignoredLocs.push(comment.loc);
94 });
95
96 ignoredLocs.sort((first, second) => {
97 if (beforeLoc(first, second.start.line, second.start.column)) {
98 return 1;
99 }
100
101 if (beforeLoc(second, first.start.line, second.start.column)) {
102 return -1;
103 }
104
105 return 0;
106 });
107
108 if (smartTabs) {
109
110 /*
111 * At least one space followed by a tab
112 * before non-tab/-space characters begin.
113 */
114 regex = /^(?=[\t ]* \t)/;
115 }
116
117 lines.forEach((line, i) => {
118 const match = regex.exec(line);
119
120 if (match) {
121 const lineNumber = i + 1,
122 column = match.index + 1;
123
124 for (let j = 0; j < ignoredLocs.length; j++) {
125 if (beforeLoc(ignoredLocs[j], lineNumber, column)) {
126 continue;
127 }
128 if (afterLoc(ignoredLocs[j], lineNumber, column)) {
129 continue;
130 }
131
132 return;
133 }
134
135 context.report({ node, loc: { line: lineNumber, column }, message: "Mixed spaces and tabs." });
136 }
137 });
138 }
139
140 };
141
142 }
143};