8 This rule was **deprecated** in ESLint v4.0.0.
10 ESLint 4.0.0 introduced a rewrite of the [`indent`](indent) rule, which now reports more errors than it did in previous versions. To ease the process of migrating to 4.0.0, the `indent-legacy` rule was introduced as a snapshot of the `indent` rule from ESLint 3.x. If your build is failing after the upgrade to 4.0.0, you can disable `indent` and enable `indent-legacy` as a quick fix. Eventually, you should switch back to the `indent` rule to get bugfixes and improvements in future versions.
14 There are several common guidelines which require specific indentation of nested blocks and statements, like:
17 function hello(indentSize, type) {
18 if (indentSize === 4 && type !== 'tab') {
19 console.log('Each next indentation will increase on 4 spaces');
24 These are the most common scenarios recommended in different style guides:
26 * Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
28 * Four spaces: Crockford
32 This rule enforces a consistent indentation style. The default style is `4 spaces`.
36 This rule has a mixed option:
38 For example, for 2-space indentation:
42 "indent": ["error", 2]
46 Or for tabbed indentation:
50 "indent": ["error", "tab"]
54 Examples of **incorrect** code for this rule with the default options:
59 /*eslint indent: "error"*/
71 Examples of **correct** code for this rule with the default options:
76 /*eslint indent: "error"*/
88 This rule has an object option:
90 * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements
91 * `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations.
92 * `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs.
93 * `"MemberExpression"` (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)
94 * `"FunctionDeclaration"` takes an object to define rules for function declarations.
95 * `parameters` (off by default) enforces indentation level for parameters in a function declaration. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the declaration must be aligned with the first parameter.
96 * `body` (default: 1) enforces indentation level for the body of a function declaration.
97 * `"FunctionExpression"` takes an object to define rules for function expressions.
98 * `parameters` (off by default) enforces indentation level for parameters in a function expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all parameters of the expression must be aligned with the first parameter.
99 * `body` (default: 1) enforces indentation level for the body of a function expression.
100 * `"CallExpression"` takes an object to define rules for function call expressions.
101 * `arguments` (off by default) enforces indentation level for arguments in a call expression. This can either be a number indicating indentation level, or the string `"first"` indicating that all arguments of the expression must be aligned with the first argument.
102 * `"ArrayExpression"` (default: 1) enforces indentation level for elements in arrays. It can also be set to the string `"first"`, indicating that all the elements in the array should be aligned with the first element.
103 * `"ObjectExpression"` (default: 1) enforces indentation level for properties in objects. It can be set to the string `"first"`, indicating that all properties in the object should be aligned with the first property.
105 Level of indentation denotes the multiple of the indent specified. Example:
107 * Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces.
108 * Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces.
109 * Indent of 2 spaces with `VariableDeclarator` set to `{"var": 2, "let": 2, "const": 3}` will indent the multi-line variable declarations with 4 spaces for `var` and `let`, 6 spaces for `const` statements.
110 * Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs.
111 * Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements.
112 * Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements.
113 * Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements.
114 * Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements.
115 * Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
116 * Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces.
117 * Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces.
118 * Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
119 * Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces.
120 * Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces.
124 Examples of **incorrect** code for this rule with the `"tab"` option:
129 /*eslint indent: ["error", "tab"]*/
141 Examples of **correct** code for this rule with the `"tab"` option:
146 /*eslint indent: ["error", "tab"]*/
150 /*tab*/function foo(d) {
160 Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
165 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
177 Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
182 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
194 ### VariableDeclarator
196 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
201 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
217 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
222 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
238 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
243 /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
259 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
264 /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
282 Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
287 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
304 Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`:
309 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
328 Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
333 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
342 Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
347 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
353 // Any indentation is permitted in variable declarations and assignments.
354 var bip = aardvark.badger
360 ### FunctionDeclaration
362 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
367 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
378 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
383 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
394 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
399 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
401 function foo(bar, baz,
409 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
414 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
416 function foo(bar, baz,
424 ### FunctionExpression
426 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
431 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
433 var foo = function(bar,
442 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
447 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
449 var foo = function(bar,
458 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
463 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
465 var foo = function(bar, baz,
473 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
478 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
480 var foo = function(bar, baz,
490 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
495 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
505 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
510 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
520 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
525 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
533 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
538 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
548 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
553 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
564 Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
569 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
580 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
585 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
595 Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
600 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
612 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
617 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
628 Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
633 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
644 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
649 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
657 Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
662 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
672 * **JSHint**: `indent`
673 * **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)