]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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 | ||
34eeec05 | 19 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
20 | module.exports = { |
21 | meta: { | |
22 | type: "layout", | |
23 | ||
24 | docs: { | |
25 | description: "disallow all tabs", | |
eb39fafa DC |
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 | messages: { | |
41 | unexpectedTab: "Unexpected tab character." | |
42 | } | |
43 | }, | |
44 | ||
45 | create(context) { | |
46 | const sourceCode = context.getSourceCode(); | |
47 | const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs; | |
48 | ||
49 | return { | |
50 | Program(node) { | |
51 | sourceCode.getLines().forEach((line, index) => { | |
52 | let match; | |
53 | ||
54 | while ((match = tabRegex.exec(line)) !== null) { | |
55 | if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) { | |
56 | continue; | |
57 | } | |
58 | ||
59 | context.report({ | |
60 | node, | |
61 | loc: { | |
62 | start: { | |
63 | line: index + 1, | |
64 | column: match.index | |
65 | }, | |
66 | end: { | |
67 | line: index + 1, | |
68 | column: match.index + match[0].length | |
69 | } | |
70 | }, | |
71 | messageId: "unexpectedTab" | |
72 | }); | |
73 | } | |
74 | }); | |
75 | } | |
76 | }; | |
77 | } | |
78 | }; |