1 # enforce consistent indentation (indent-legacy)
3 ESLint 4.0.0 introduced a rewrite of the [`indent`](/docs/rules/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.
7 There are several common guidelines which require specific indentation of nested blocks and statements, like:
10 function hello(indentSize, type) {
11 if (indentSize === 4 && type !== 'tab') {
12 console.log('Each next indentation will increase on 4 spaces');
17 These are the most common scenarios recommended in different style guides:
19 * Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
21 * Four spaces: Crockford
25 This rule enforces a consistent indentation style. The default style is `4 spaces`.
29 This rule has a mixed option:
31 For example, for 2-space indentation:
35 "indent": ["error", 2]
39 Or for tabbed indentation:
43 "indent": ["error", "tab"]
47 Examples of **incorrect** code for this rule with the default options:
50 /*eslint indent: "error"*/
60 Examples of **correct** code for this rule with the default options:
63 /*eslint indent: "error"*/
73 This rule has an object option:
75 * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements
76 * `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations.
77 * `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs.
78 * `"MemberExpression"` (off by default) enforces indentation level for multi-line property chains (except in variable declarations and assignments)
79 * `"FunctionDeclaration"` takes an object to define rules for function declarations.
80 * `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.
81 * `body` (default: 1) enforces indentation level for the body of a function declaration.
82 * `"FunctionExpression"` takes an object to define rules for function expressions.
83 * `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.
84 * `body` (default: 1) enforces indentation level for the body of a function expression.
85 * `"CallExpression"` takes an object to define rules for function call expressions.
86 * `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.
87 * `"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.
88 * `"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.
90 Level of indentation denotes the multiple of the indent specified. Example:
92 * Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces.
93 * Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces.
94 * 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.
95 * Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs.
96 * Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements.
97 * Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements.
98 * Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements.
99 * Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements.
100 * Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
101 * Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces.
102 * Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces.
103 * Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
104 * Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces.
105 * Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces.
109 Examples of **incorrect** code for this rule with the `"tab"` option:
112 /*eslint indent: ["error", "tab"]*/
122 Examples of **correct** code for this rule with the `"tab"` option:
125 /*eslint indent: ["error", "tab"]*/
129 /*tab*/function foo(d) {
137 Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
140 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
150 Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
153 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
163 ### VariableDeclarator
165 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
168 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
182 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
185 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
199 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
202 /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
216 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
219 /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
235 Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
238 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
254 Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`:
257 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
275 Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
278 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
285 Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
288 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
294 // Any indentation is permitted in variable declarations and assignments.
295 var bip = aardvark.badger
299 ### FunctionDeclaration
301 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
304 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
313 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
316 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
325 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
328 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
330 function foo(bar, baz,
336 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
339 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
341 function foo(bar, baz,
347 ### FunctionExpression
349 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
352 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
354 var foo = function(bar,
361 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
364 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
366 var foo = function(bar,
373 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
376 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
378 var foo = function(bar, baz,
384 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
387 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
389 var foo = function(bar, baz,
397 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
400 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
408 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
411 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
419 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
422 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
428 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
431 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
439 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
442 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
451 Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
454 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
463 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
466 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
474 Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
477 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
487 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
490 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
499 Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
502 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
511 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
514 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
520 Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
523 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
531 * **JSHint**: `indent`
532 * **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)