UNPKG

2.05 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to check for tabs inside a file
3 * @author Gyandeep Singh
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Helpers
10//------------------------------------------------------------------------------
11
12const tabRegex = /\t+/gu;
13const anyNonWhitespaceRegex = /\S/u;
14
15//------------------------------------------------------------------------------
16// Public Interface
17//------------------------------------------------------------------------------
18
19module.exports = {
20 meta: {
21 type: "layout",
22
23 docs: {
24 description: "disallow all tabs",
25 category: "Stylistic Issues",
26 recommended: false,
27 url: "https://eslint.org/docs/rules/no-tabs"
28 },
29 schema: [{
30 type: "object",
31 properties: {
32 allowIndentationTabs: {
33 type: "boolean",
34 default: false
35 }
36 },
37 additionalProperties: false
38 }]
39 },
40
41 create(context) {
42 const sourceCode = context.getSourceCode();
43 const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs;
44
45 return {
46 Program(node) {
47 sourceCode.getLines().forEach((line, index) => {
48 let match;
49
50 while ((match = tabRegex.exec(line)) !== null) {
51 if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) {
52 continue;
53 }
54
55 context.report({
56 node,
57 loc: {
58 line: index + 1,
59 column: match.index
60 },
61 message: "Unexpected tab character."
62 });
63 }
64 });
65 }
66 };
67 }
68};