1 # enforce consistent indentation (indent)
3 There are several common guidelines which require specific indentation of nested blocks and statements, like:
6 function hello(indentSize, type) {
7 if (indentSize === 4 && type !== 'tab') {
8 console.log('Each next indentation will increase on 4 spaces');
13 These are the most common scenarios recommended in different style guides:
15 * Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
17 * Four spaces: Crockford
21 This rule enforces a consistent indentation style. The default style is `4 spaces`.
25 This rule has a mixed option:
27 For example, for 2-space indentation:
31 "indent": ["error", 2]
35 Or for tabbed indentation:
39 "indent": ["error", "tab"]
43 Examples of **incorrect** code for this rule with the default options:
46 /*eslint indent: "error"*/
56 Examples of **correct** code for this rule with the default options:
59 /*eslint indent: "error"*/
69 This rule has an object option:
71 * `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements
72 * `"VariableDeclarator"` (default: 1) enforces indentation level for `var` declarators; can also take an object to define separate rules for `var`, `let` and `const` declarations. It can also be `"first"`, indicating all the declarators should be aligned with the first declarator.
73 * `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. This can also be set to `"off"` to disable checking for file-level IIFEs.
74 * `"MemberExpression"` (default: 1) enforces indentation level for multi-line property chains. This can also be set to `"off"` to disable checking for MemberExpression indentation.
75 * `"FunctionDeclaration"` takes an object to define rules for function declarations.
76 * `parameters` (default: 1) 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. This can also be set to `"off"` to disable checking for FunctionDeclaration parameters.
77 * `body` (default: 1) enforces indentation level for the body of a function declaration.
78 * `"FunctionExpression"` takes an object to define rules for function expressions.
79 * `parameters` (default: 1) 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. This can also be set to `"off"` to disable checking for FunctionExpression parameters.
80 * `body` (default: 1) enforces indentation level for the body of a function expression.
81 * `"CallExpression"` takes an object to define rules for function call expressions.
82 * `arguments` (default: 1) 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. This can also be set to `"off"` to disable checking for CallExpression arguments.
83 * `"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. This can also be set to `"off"` to disable checking for array elements.
84 * `"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. This can also be set to `"off"` to disable checking for object properties.
85 * `"ImportDeclaration"` (default: 1) enforces indentation level for import statements. It can be set to the string `"first"`, indicating that all imported members from a module should be aligned with the first member in the list. This can also be set to `"off"` to disable checking for imported module members.
86 * `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions.
87 * `"offsetTernaryExpressions": true` (`false` by default) requires indentation for values of ternary expressions.
88 * `"ignoredNodes"` accepts an array of [selectors](/docs/developer-guide/selectors.md). If an AST node is matched by any of the selectors, the indentation of tokens which are direct children of that node will be ignored. This can be used as an escape hatch to relax the rule if you disagree with the indentation that it enforces for a particular syntactic pattern.
89 * `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line.
91 Level of indentation denotes the multiple of the indent specified. Example:
93 * Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces.
94 * Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces.
95 * 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.
96 * Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs.
97 * Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements.
98 * Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements.
99 * Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements.
100 * Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements.
101 * Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
102 * Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces.
103 * Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces.
104 * Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
105 * Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces.
106 * Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces.
110 Examples of **incorrect** code for this rule with the `"tab"` option:
113 /*eslint indent: ["error", "tab"]*/
123 Examples of **correct** code for this rule with the `"tab"` option:
126 /*eslint indent: ["error", "tab"]*/
130 /*tab*/function foo(d) {
138 Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
141 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
151 Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
154 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
164 ### VariableDeclarator
166 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
169 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
183 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
186 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
200 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
203 /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
217 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": "first" }` options:
220 /*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/
234 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": "first" }` options:
237 /*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/
251 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
254 /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
270 Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
273 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
289 Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
292 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
308 Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": "off" }`:
311 /*eslint indent: ["error", 2, { "outerIIFEBody": "off" }]*/
336 Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
339 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
346 Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
349 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
356 ### FunctionDeclaration
358 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
361 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
370 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
373 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
382 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
385 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
387 function foo(bar, baz,
393 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
396 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
398 function foo(bar, baz,
404 ### FunctionExpression
406 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
409 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
411 var foo = function(bar,
418 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
421 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
423 var foo = function(bar,
430 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
433 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
435 var foo = function(bar, baz,
441 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
444 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
446 var foo = function(bar, baz,
454 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
457 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
465 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
468 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
476 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
479 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
485 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
488 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
496 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
499 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
508 Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
511 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
520 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
523 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
531 Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
534 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
544 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
547 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
556 Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
559 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
568 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
571 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
577 Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
580 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
586 ### ImportDeclaration
588 Examples of **correct** code for this rule with the `4, { "ImportDeclaration": 1 }` option (the default):
591 /*eslint indent: ["error", 4, { "ImportDeclaration": 1 }]*/
605 Examples of **incorrect** code for this rule with the `4, { "ImportDeclaration": "first" }` option:
608 /*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/
616 Examples of **correct** code for this rule with the `4, { "ImportDeclaration": "first" }` option:
619 /*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/
627 ### flatTernaryExpressions
629 Examples of **incorrect** code for this rule with the default `4, { "flatTernaryExpressions": false }` option:
632 /*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/
640 Examples of **correct** code for this rule with the default `4, { "flatTernaryExpressions": false }` option:
643 /*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/
651 Examples of **incorrect** code for this rule with the `4, { "flatTernaryExpressions": true }` option:
654 /*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/
662 Examples of **correct** code for this rule with the `4, { "flatTernaryExpressions": true }` option:
665 /*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/
673 ### offsetTernaryExpressions
675 Examples of **incorrect** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:
678 /*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/
689 Examples of **correct** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:
692 /*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/
707 Examples of **incorrect** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:
710 /*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/
725 Examples of **correct** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:
728 /*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/
745 The following configuration ignores the indentation of `ConditionalExpression` ("ternary expression") nodes:
747 Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["ConditionalExpression"] }` option:
750 /*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/
761 The following configuration ignores indentation in the body of IIFEs.
763 Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }` option:
766 /*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/
778 Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option:
781 /*eslint indent: ["error", 4, { "ignoreComments": true }] */
786 // comment intentionally de-indented
794 * **JSHint**: `indent`
795 * **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)