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