2 * @fileoverview Disallow mixed spaces and tabs for indentation
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "disallow mixed spaces and tabs for indentation",
17 category: "Stylistic Issues",
19 url: "https://eslint.org/docs/rules/no-mixed-spaces-and-tabs"
24 enum: ["smart-tabs", true, false]
30 const sourceCode = context.getSourceCode();
33 const ignoredLocs = [];
35 switch (context.options[0]) {
36 case true: // Support old syntax, maybe add deprecation warning here
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.
52 function beforeLoc(loc, line, column) {
53 if (line < loc.start.line) {
56 return line === loc.start.line && column < loc.start.column;
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.
67 function afterLoc(loc, line, column) {
68 if (line > loc.end.line) {
71 return line === loc.end.line && column > loc.end.column;
74 //--------------------------------------------------------------------------
76 //--------------------------------------------------------------------------
80 TemplateElement(node) {
81 ignoredLocs.push(node.loc);
84 "Program:exit"(node) {
87 * At least one space followed by a tab
88 * or the reverse before non-tab/-space
91 let regex = /^(?=[\t ]*(\t | \t))/u;
92 const lines = sourceCode.lines,
93 comments = sourceCode.getAllComments();
95 comments.forEach(comment => {
96 ignoredLocs.push(comment.loc);
99 ignoredLocs.sort((first, second) => {
100 if (beforeLoc(first, second.start.line, second.start.column)) {
104 if (beforeLoc(second, first.start.line, second.start.column)) {
114 * At least one space followed by a tab
115 * before non-tab/-space characters begin.
117 regex = /^(?=[\t ]* \t)/u;
120 lines.forEach((line, i) => {
121 const match = regex.exec(line);
124 const lineNumber = i + 1,
125 column = match.index + 1;
127 for (let j = 0; j < ignoredLocs.length; j++) {
128 if (beforeLoc(ignoredLocs[j], lineNumber, column)) {
131 if (afterLoc(ignoredLocs[j], lineNumber, column)) {
138 context.report({ node, loc: { line: lineNumber, column }, message: "Mixed spaces and tabs." });