]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-tabs.js
1f3921a9a70fd620e614c409335b0071ec244301
[pve-eslint.git] / eslint / lib / rules / no-tabs.js
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
12 const tabRegex = /\t+/gu;
13 const anyNonWhitespaceRegex = /\S/u;
14
15 //------------------------------------------------------------------------------
16 // Public Interface
17 //------------------------------------------------------------------------------
18
19 module.exports = {
20 meta: {
21 type: "layout",
22
23 docs: {
24 description: "disallow all tabs",
25 recommended: false,
26 url: "https://eslint.org/docs/rules/no-tabs"
27 },
28 schema: [{
29 type: "object",
30 properties: {
31 allowIndentationTabs: {
32 type: "boolean",
33 default: false
34 }
35 },
36 additionalProperties: false
37 }],
38
39 messages: {
40 unexpectedTab: "Unexpected tab character."
41 }
42 },
43
44 create(context) {
45 const sourceCode = context.getSourceCode();
46 const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs;
47
48 return {
49 Program(node) {
50 sourceCode.getLines().forEach((line, index) => {
51 let match;
52
53 while ((match = tabRegex.exec(line)) !== null) {
54 if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) {
55 continue;
56 }
57
58 context.report({
59 node,
60 loc: {
61 start: {
62 line: index + 1,
63 column: match.index
64 },
65 end: {
66 line: index + 1,
67 column: match.index + match[0].length
68 }
69 },
70 messageId: "unexpectedTab"
71 });
72 }
73 });
74 }
75 };
76 }
77 };