Skip to content

Commit

Permalink
Update: no-tabs allowIndentationTabs option (fixes #10256)
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure committed Oct 5, 2018
1 parent 066f7e0 commit e972c19
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
28 changes: 24 additions & 4 deletions docs/rules/no-tabs.md
Expand Up @@ -9,14 +9,14 @@ This rule looks for tabs anywhere inside a file: code, comments or anything else
Examples of **incorrect** code for this rule:

```js
var a /t= 2;
var a \t= 2;

/**
* /t/t it's a test function
* \t\t it's a test function
*/
function test(){}

var x = 1; // /t test
var x = 1; // \t test
```

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

### Options

This rule has an optional object option with the following properties:

* `allowIndentationTabs` (default: false): If this is set to true, then the rule will not report tabs used for indentation.

#### allowIndentationTabs

Examples of **correct** code for this rule with the `allowIndentationTabs: true` option:

```js
/* eslint no-tabs: ["error", { allowIndentationTabs: true }] */

function test() {
\tdoSomething();
}

\t// comment with leading indentation tab
```

## When Not To Use It

If you have established a standard where having tabs is fine.
If you have established a standard where having tabs is fine, then you can disable this rule.

## Compatibility

Expand Down
29 changes: 23 additions & 6 deletions lib/rules/no-tabs.js
Expand Up @@ -8,7 +8,9 @@
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const regex = /\t/;

const tabRegex = /\t+/g;
const anyNonWhitespaceRegex = /\S/;

//------------------------------------------------------------------------------
// Public Interface
Expand All @@ -22,21 +24,36 @@ module.exports = {
recommended: false,
url: "https://eslint.org/docs/rules/no-tabs"
},
schema: []
schema: [{
type: "object",
properties: {
allowIndentationTabs: {
type: "boolean"
}
},
additionalProperties: false
}]
},

create(context) {
const sourceCode = context.getSourceCode();
const allowIndentationTabs = context.options && context.options[0] && context.options[0].allowIndentationTabs;

return {
Program(node) {
context.getSourceCode().getLines().forEach((line, index) => {
const match = regex.exec(line);
sourceCode.getLines().forEach((line, index) => {
let match;

while ((match = tabRegex.exec(line)) !== null) {
if (allowIndentationTabs && !anyNonWhitespaceRegex.test(line.slice(0, match.index))) {
continue;
}

if (match) {
context.report({
node,
loc: {
line: index + 1,
column: match.index + 1
column: match.index
},
message: "Unexpected tab character."
});
Expand Down
32 changes: 25 additions & 7 deletions tests/lib/rules/no-tabs.js
Expand Up @@ -23,23 +23,32 @@ ruleTester.run("no-tabs", rule, {
"function test(){\n}",
"function test(){\n" +
" // sdfdsf \n" +
"}"
"}",

{
code: "\tdoSomething();",
options: [{ allowIndentationTabs: true }]
},
{
code: "\t// comment",
options: [{ allowIndentationTabs: true }]
}
],
invalid: [
{
code: "function test(){\t}",
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 18
column: 17
}]
},
{
code: "/** \t comment test */",
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 6
column: 5
}]
},
{
Expand All @@ -50,7 +59,7 @@ ruleTester.run("no-tabs", rule, {
errors: [{
message: ERROR_MESSAGE,
line: 2,
column: 6
column: 5
}]
},
{
Expand All @@ -61,7 +70,7 @@ ruleTester.run("no-tabs", rule, {
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 10
column: 9
}]
},
{
Expand All @@ -73,14 +82,23 @@ ruleTester.run("no-tabs", rule, {
{
message: ERROR_MESSAGE,
line: 2,
column: 6
column: 5
},
{
message: ERROR_MESSAGE,
line: 3,
column: 2
column: 1
}
]
},
{
code: "\t// Comment with leading tab \t and inline tab",
options: [{ allowIndentationTabs: true }],
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 30
}]
}
]
});

0 comments on commit e972c19

Please sign in to comment.