Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New: no-tabs rule (fixes #6079) (#6772)
  • Loading branch information
gyandeeps authored and nzakas committed Jul 29, 2016
1 parent ddea63a commit b22eb5c
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/eslint.json
Expand Up @@ -99,6 +99,7 @@
"no-spaced-func": "off",
"no-sparse-arrays": "error",
"no-sync": "off",
"no-tabs": "off",
"no-ternary": "off",
"no-trailing-spaces": "off",
"no-this-before-super": "error",
Expand Down
41 changes: 41 additions & 0 deletions docs/rules/no-tabs.md
@@ -0,0 +1,41 @@
# Disallow tabs in file (no-tabs)

some style guides don't allow the use of tab characters anywhere. So they would want to disallow tab anywhere inside a file including comments.

## Rule Details

This rule looks for tabs inside the file. It can abe anywhere inside code or comments or anything.

Examples of **incorrect** code for this rule:

```js
var a /t= 2;

/**
* /t/t its a test function
*/
function test(){}

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

Examples of **correct** code for this rule:

```js
var a = 2;

/**
* its a test function
*/
function test(){}

var x = 1; // test
```

## When Not To Use It

If you have established a standard where having tabs is fine.

## Compatibility

* **JSCS**: [disallowTabs](http://jscs.info/rule/disallowTabs)
47 changes: 47 additions & 0 deletions lib/rules/no-tabs.js
@@ -0,0 +1,47 @@
/**
* @fileoverview Rule to check for tabs inside a file
* @author Gyandeep Singh
*/

"use strict";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const regex = /\t/;

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: "Disallow tabs in file",
category: "Stylistic Issues",
recommended: false
},
schema: []
},

create(context) {
return {
Program(node) {
context.getSourceLines().forEach((line, index) => {
const match = regex.exec(line);

if (match) {
context.report(
node,
{
line: index + 1,
column: match.index + 1
},
"Unexpected tab character."
);
}
});
}
};
}
};
1 change: 1 addition & 0 deletions packages/eslint-config-eslint/default.yml
Expand Up @@ -72,6 +72,7 @@ rules:
no-shadow: "error"
no-shadow-restricted-names: "error"
no-spaced-func: "error"
no-tabs: "error"
no-trailing-spaces: "error"
no-undef: "error"
no-undef-init: "error"
Expand Down
86 changes: 86 additions & 0 deletions tests/lib/rules/no-tabs.js
@@ -0,0 +1,86 @@
/**
* @fileoverview Tests for no-tabs rule
* @author Gyandeep Singh
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/no-tabs");
const RuleTester = require("../../../lib/testers/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const ERROR_MESSAGE = "Unexpected tab character.";

ruleTester.run("no-tabs", rule, {
valid: [
"function test(){\n}",
"function test(){\n" +
" // sdfdsf \n" +
"}",
],
invalid: [
{
code: "function test(){\t}",
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 18
}]
},
{
code: "/** \t comment test */",
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 6
}]
},
{
code:
"function test(){\n" +
" //\tsdfdsf \n" +
"}",
errors: [{
message: ERROR_MESSAGE,
line: 2,
column: 6
}]
},
{
code:
"function\ttest(){\n" +
" //sdfdsf \n" +
"}",
errors: [{
message: ERROR_MESSAGE,
line: 1,
column: 10
}]
},
{
code:
"function test(){\n" +
" //\tsdfdsf \n" +
"\t}",
errors: [
{
message: ERROR_MESSAGE,
line: 2,
column: 6
},
{
message: ERROR_MESSAGE,
line: 3,
column: 2
}
]
}
]
});
2 changes: 1 addition & 1 deletion tests/lib/rules/no-underscore-dangle.js
Expand Up @@ -43,6 +43,6 @@ ruleTester.run("no-underscore-dangle", rule, {
{ code: "foo._bar;", errors: [{ message: "Unexpected dangling '_' in '_bar'.", type: "MemberExpression"}] },
{ code: "this._prop;", errors: [{ message: "Unexpected dangling '_' in '_prop'.", type: "MemberExpression"}] },
{ code: "class foo { constructor() { super._prop; } }", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected dangling '_' in '_prop'.", type: "MemberExpression"}] },
{ code: "class foo { constructor() { this._prop; } }", options: [{allowAfterSuper: true}], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected dangling '_' in '_prop'.", type: "MemberExpression"}] }
{ code: "class foo { constructor() { this._prop; } }", options: [{allowAfterSuper: true}], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected dangling '_' in '_prop'.", type: "MemberExpression"}] }
]
});

0 comments on commit b22eb5c

Please sign in to comment.