Validate Indentation (indent)

This option validates a specific tab width for your code in block statements.

There are several common guidelines which require specific indentation of nested blocks and statements, like:

function hello(indentSize, type) {
    if (indentSize === 4 && type !== 'tab') {
        console.log('Each next indentation will increase on 4 spaces');
    }
}

This is the most common scenarios recommended in different style guides:

Fixable: This rule is automatically fixable using the --fix flag on the command line.

Rule Details

This rule is aimed to enforce consistent indentation style. The default style is 4 spaces.

It takes an option as the second parameter which can be "tab" for tab-based indentation or a positive number for space indentations.

Options

The indent rule has two options:

Level of indentation denotes the multiple of the indent specified. Example:

2 space indentation with enabled switch cases indentation

 "indent": [2, 2, {"SwitchCase": 1}]

4 space indention

"indent": 2

2 space indentation

"indent": [2, 2]

tabbed indentation

"indent": [2, "tab"]

The following patterns are considered problems:

/*eslint indent: [2, 2]*/

if (a) {
   b=c;            /*error Expected indentation of 2 space characters but found 3.*/
function foo(d) {  /*error Expected indentation of 2 space characters but found 0.*/
       e=f;        /*error Expected indentation of 2 space characters but found 7.*/
}                  /*error Expected indentation of 6 space characters but found 0.*/
}
/*eslint indent: [2, "tab"]*/

if (a) {
     b=c;          /*error Expected indentation of 1 tab character but found 0.*/
function foo(d) {  /*error Expected indentation of 1 tab character but found 0.*/
           e=f;    /*error Expected indentation of 1 tab character but found 0.*/
 }
}
/*eslint indent: [2, 2, {"VariableDeclarator": 1}]*/
/*eslint-env es6*/

var a,
    b,             /*error Expected indentation of 2 space characters but found 4.*/
    c;             /*error Expected indentation of 2 space characters but found 4.*/
let a,
    b,             /*error Expected indentation of 2 space characters but found 4.*/
    c;             /*error Expected indentation of 2 space characters but found 4.*/
const a = 1,
    b = 2,         /*error Expected indentation of 2 space characters but found 4.*/
    c = 3;         /*error Expected indentation of 2 space characters but found 4.*/
/*eslint indent: [2, 2, {"SwitchCase": 1}]*/

switch(a){
case "a":          /*error Expected indentation of 2 space characters but found 0.*/
    break;
case "b":          /*error Expected indentation of 2 space characters but found 0.*/
    break;
}

The following patterns are not considered problems:

/*eslint indent: [2, 2]*/

if (a) {
  b=c;
  function foo(d) {
    e=f;
  }
}
/*indent: [2, "tab"]*/

if (a) {
/*tab*/b=c;
/*tab*/function foo(d) {
/*tab*//*tab*/e=f;
/*tab*/}
}
/*eslint indent: [2, 2, {"VariableDeclarator": 2}]*/
/*eslint-env es6*/

var a,
    b,
    c;
let a,
    b,
    c;
const a = 1,
    b = 2,
    c = 3;
/*eslint indent: [2, 2, {"VariableDeclarator": { "var": 2, "let": 2, "const": 3}}]*/
/*eslint-env es6*/

var a,
    b,
    c;
let a,
    b,
    c;
const a = 1,
      b = 2,
      c = 3;
/*eslint indent: [2, 4, {"SwitchCase": 1}]*/

switch(a){
    case "a":
        break;
    case "b":
        break;
}

Compatibility

Version

This rule was introduced in ESLint 0.14.0.

Resources