From e972c1939fb36b55bb33d799d2a9dbe7bab3f42d Mon Sep 17 00:00:00 2001 From: Kevin Partington Date: Fri, 5 Oct 2018 14:43:06 -0500 Subject: [PATCH] Update: no-tabs allowIndentationTabs option (fixes #10256) --- docs/rules/no-tabs.md | 28 ++++++++++++++++++++++++---- lib/rules/no-tabs.js | 29 +++++++++++++++++++++++------ tests/lib/rules/no-tabs.js | 32 +++++++++++++++++++++++++------- 3 files changed, 72 insertions(+), 17 deletions(-) diff --git a/docs/rules/no-tabs.md b/docs/rules/no-tabs.md index 691b2f2af2a..4e02d9a04c6 100644 --- a/docs/rules/no-tabs.md +++ b/docs/rules/no-tabs.md @@ -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: @@ -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 diff --git a/lib/rules/no-tabs.js b/lib/rules/no-tabs.js index 08a8fa5b758..c22a94da38b 100644 --- a/lib/rules/no-tabs.js +++ b/lib/rules/no-tabs.js @@ -8,7 +8,9 @@ //------------------------------------------------------------------------------ // Helpers //------------------------------------------------------------------------------ -const regex = /\t/; + +const tabRegex = /\t+/g; +const anyNonWhitespaceRegex = /\S/; //------------------------------------------------------------------------------ // Public Interface @@ -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." }); diff --git a/tests/lib/rules/no-tabs.js b/tests/lib/rules/no-tabs.js index a01d9dc1661..b4989037d30 100644 --- a/tests/lib/rules/no-tabs.js +++ b/tests/lib/rules/no-tabs.js @@ -23,7 +23,16 @@ 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: [ { @@ -31,7 +40,7 @@ ruleTester.run("no-tabs", rule, { errors: [{ message: ERROR_MESSAGE, line: 1, - column: 18 + column: 17 }] }, { @@ -39,7 +48,7 @@ ruleTester.run("no-tabs", rule, { errors: [{ message: ERROR_MESSAGE, line: 1, - column: 6 + column: 5 }] }, { @@ -50,7 +59,7 @@ ruleTester.run("no-tabs", rule, { errors: [{ message: ERROR_MESSAGE, line: 2, - column: 6 + column: 5 }] }, { @@ -61,7 +70,7 @@ ruleTester.run("no-tabs", rule, { errors: [{ message: ERROR_MESSAGE, line: 1, - column: 10 + column: 9 }] }, { @@ -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 + }] } ] });