Skip to content

Commit 5fd1bda

Browse files
platinumazurenot-an-aardvark
authored andcommitted
Update: no-tabs allowIndentationTabs option (fixes #10256) (#10925)
1 parent d12be69 commit 5fd1bda

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

docs/rules/no-tabs.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ This rule looks for tabs anywhere inside a file: code, comments or anything else
99
Examples of **incorrect** code for this rule:
1010

1111
```js
12-
var a /t= 2;
12+
var a \t= 2;
1313

1414
/**
15-
* /t/t it's a test function
15+
* \t\t it's a test function
1616
*/
1717
function test(){}
1818

19-
var x = 1; // /t test
19+
var x = 1; // \t test
2020
```
2121

2222
Examples of **correct** code for this rule:
@@ -32,9 +32,29 @@ function test(){}
3232
var x = 1; // test
3333
```
3434

35+
### Options
36+
37+
This rule has an optional object option with the following properties:
38+
39+
* `allowIndentationTabs` (default: false): If this is set to true, then the rule will not report tabs used for indentation.
40+
41+
#### allowIndentationTabs
42+
43+
Examples of **correct** code for this rule with the `allowIndentationTabs: true` option:
44+
45+
```js
46+
/* eslint no-tabs: ["error", { allowIndentationTabs: true }] */
47+
48+
function test() {
49+
\tdoSomething();
50+
}
51+
52+
\t// comment with leading indentation tab
53+
```
54+
3555
## When Not To Use It
3656

37-
If you have established a standard where having tabs is fine.
57+
If you have established a standard where having tabs is fine, then you can disable this rule.
3858

3959
## Compatibility
4060

lib/rules/no-tabs.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
//------------------------------------------------------------------------------
99
// Helpers
1010
//------------------------------------------------------------------------------
11-
const regex = /\t/;
11+
12+
const tabRegex = /\t+/g;
13+
const anyNonWhitespaceRegex = /\S/;
1214

1315
//------------------------------------------------------------------------------
1416
// Public Interface
@@ -22,21 +24,36 @@ module.exports = {
2224
recommended: false,
2325
url: "https://eslint.org/docs/rules/no-tabs"
2426
},
25-
schema: []
27+
schema: [{
28+
type: "object",
29+
properties: {
30+
allowIndentationTabs: {
31+
type: "boolean"
32+
}
33+
},
34+
additionalProperties: false
35+
}]
2636
},
2737

2838
create(context) {
39+
const sourceCode = context.getSourceCode();
40+
const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs;
41+
2942
return {
3043
Program(node) {
31-
context.getSourceCode().getLines().forEach((line, index) => {
32-
const match = regex.exec(line);
44+
sourceCode.getLines().forEach((line, index) => {
45+
let match;
46+
47+
while ((match = tabRegex.exec(line)) !== null) {
48+
if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) {
49+
continue;
50+
}
3351

34-
if (match) {
3552
context.report({
3653
node,
3754
loc: {
3855
line: index + 1,
39-
column: match.index + 1
56+
column: match.index
4057
},
4158
message: "Unexpected tab character."
4259
});

tests/lib/rules/no-tabs.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,32 @@ ruleTester.run("no-tabs", rule, {
2323
"function test(){\n}",
2424
"function test(){\n" +
2525
" // sdfdsf \n" +
26-
"}"
26+
"}",
27+
28+
{
29+
code: "\tdoSomething();",
30+
options: [{ allowIndentationTabs: true }]
31+
},
32+
{
33+
code: "\t// comment",
34+
options: [{ allowIndentationTabs: true }]
35+
}
2736
],
2837
invalid: [
2938
{
3039
code: "function test(){\t}",
3140
errors: [{
3241
message: ERROR_MESSAGE,
3342
line: 1,
34-
column: 18
43+
column: 17
3544
}]
3645
},
3746
{
3847
code: "/** \t comment test */",
3948
errors: [{
4049
message: ERROR_MESSAGE,
4150
line: 1,
42-
column: 6
51+
column: 5
4352
}]
4453
},
4554
{
@@ -50,7 +59,7 @@ ruleTester.run("no-tabs", rule, {
5059
errors: [{
5160
message: ERROR_MESSAGE,
5261
line: 2,
53-
column: 6
62+
column: 5
5463
}]
5564
},
5665
{
@@ -61,7 +70,7 @@ ruleTester.run("no-tabs", rule, {
6170
errors: [{
6271
message: ERROR_MESSAGE,
6372
line: 1,
64-
column: 10
73+
column: 9
6574
}]
6675
},
6776
{
@@ -73,14 +82,23 @@ ruleTester.run("no-tabs", rule, {
7382
{
7483
message: ERROR_MESSAGE,
7584
line: 2,
76-
column: 6
85+
column: 5
7786
},
7887
{
7988
message: ERROR_MESSAGE,
8089
line: 3,
81-
column: 2
90+
column: 1
8291
}
8392
]
93+
},
94+
{
95+
code: "\t// Comment with leading tab \t and inline tab",
96+
options: [{ allowIndentationTabs: true }],
97+
errors: [{
98+
message: ERROR_MESSAGE,
99+
line: 1,
100+
column: 30
101+
}]
84102
}
85103
]
86104
});

0 commit comments

Comments
 (0)