]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/indent.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / rules / indent.md
CommitLineData
eb39fafa
DC
1# enforce consistent indentation (indent)
2
3There are several common guidelines which require specific indentation of nested blocks and statements, like:
4
5```js
6function hello(indentSize, type) {
7 if (indentSize === 4 && type !== 'tab') {
8 console.log('Each next indentation will increase on 4 spaces');
9 }
10}
11```
12
13These are the most common scenarios recommended in different style guides:
14
15* Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
16* Tabs: jQuery
17* Four spaces: Crockford
18
19## Rule Details
20
21This rule enforces a consistent indentation style. The default style is `4 spaces`.
22
23## Options
24
25This rule has a mixed option:
26
27For example, for 2-space indentation:
28
29```json
30{
31 "indent": ["error", 2]
32}
33```
34
35Or for tabbed indentation:
36
37```json
38{
39 "indent": ["error", "tab"]
40}
41```
42
43Examples of **incorrect** code for this rule with the default options:
44
45```js
46/*eslint indent: "error"*/
47
48if (a) {
49 b=c;
50 function foo(d) {
51 e=f;
52 }
53}
54```
55
56Examples of **correct** code for this rule with the default options:
57
58```js
59/*eslint indent: "error"*/
60
61if (a) {
62 b=c;
63 function foo(d) {
64 e=f;
65 }
66}
67```
68
69This rule has an object option:
70
5422a9cc 71* `"ignoredNodes"` can be used to disable indentation checking for any AST node. This 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.
eb39fafa
DC
72* `"SwitchCase"` (default: 0) enforces indentation level for `case` clauses in `switch` statements
73* `"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.
74* `"outerIIFEBody"` (default: 1) enforces indentation level for file-level IIFEs. This can also be set to `"off"` to disable checking for file-level IIFEs.
75* `"MemberExpression"` (default: 1) enforces indentation level for multi-line property chains. This can also be set to `"off"` to disable checking for MemberExpression indentation.
76* `"FunctionDeclaration"` takes an object to define rules for function declarations.
77 * `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.
78 * `body` (default: 1) enforces indentation level for the body of a function declaration.
79* `"FunctionExpression"` takes an object to define rules for function expressions.
80 * `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.
81 * `body` (default: 1) enforces indentation level for the body of a function expression.
609c276f
TL
82* `"StaticBlock"` takes an object to define rules for class static blocks.
83 * `body` (default: 1) enforces indentation level for the body of a class static block.
eb39fafa
DC
84* `"CallExpression"` takes an object to define rules for function call expressions.
85 * `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.
86* `"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.
87* `"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.
88* `"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.
89* `"flatTernaryExpressions": true` (`false` by default) requires no indentation for ternary expressions which are nested in other ternary expressions.
90* `"offsetTernaryExpressions": true` (`false` by default) requires indentation for values of ternary expressions.
eb39fafa
DC
91* `"ignoreComments"` (default: false) can be used when comments do not need to be aligned with nodes on the previous or next line.
92
93Level of indentation denotes the multiple of the indent specified. Example:
94
95* Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces.
96* Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces.
97* 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.
98* Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs.
99* Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements.
100* Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements.
101* Indent of 2 spaces with `SwitchCase` set to `2` will indent `case` clauses with 4 spaces with respect to `switch` statements.
102* Indent of tab with `SwitchCase` set to `2` will indent `case` clauses with 2 tabs with respect to `switch` statements.
103* Indent of 2 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
104* Indent of 2 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 2 spaces.
105* Indent of 2 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 4 spaces.
106* Indent of 4 spaces with `MemberExpression` set to `0` will indent the multi-line property chains with 0 spaces.
107* Indent of 4 spaces with `MemberExpression` set to `1` will indent the multi-line property chains with 4 spaces.
108* Indent of 4 spaces with `MemberExpression` set to `2` will indent the multi-line property chains with 8 spaces.
109
110### tab
111
112Examples of **incorrect** code for this rule with the `"tab"` option:
113
114```js
115/*eslint indent: ["error", "tab"]*/
116
117if (a) {
118 b=c;
119function foo(d) {
120 e=f;
121 }
122}
123```
124
125Examples of **correct** code for this rule with the `"tab"` option:
126
127```js
128/*eslint indent: ["error", "tab"]*/
129
130if (a) {
131/*tab*/b=c;
132/*tab*/function foo(d) {
133/*tab*//*tab*/e=f;
134/*tab*/}
135}
136```
137
5422a9cc
TL
138### ignoredNodes
139
140The following configuration ignores the indentation of `ConditionalExpression` ("ternary expression") nodes:
141
142Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["ConditionalExpression"] }` option:
143
144```js
145/*eslint indent: ["error", 4, { "ignoredNodes": ["ConditionalExpression"] }]*/
146
147var a = foo
148 ? bar
149 : baz;
150
151var a = foo
152 ? bar
153: baz;
154```
155
156The following configuration ignores indentation in the body of IIFEs.
157
158Examples of **correct** code for this rule with the `4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }` option:
159
160```js
161/*eslint indent: ["error", 4, { "ignoredNodes": ["CallExpression > FunctionExpression.callee > BlockStatement.body"] }]*/
162
163(function() {
164
165foo();
166bar();
167
168})
169```
170
171All AST node types can be found at [ESTree](https://github.com/estree/estree) specification. You can use [AST Explorer](https://astexplorer.net/) with the espree parser to examine AST tree of a code snippet.
172
eb39fafa
DC
173### SwitchCase
174
175Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
176
177```js
178/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
179
180switch(a){
181case "a":
182 break;
183case "b":
184 break;
185}
186```
187
188Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
189
190```js
191/*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
192
193switch(a){
194 case "a":
195 break;
196 case "b":
197 break;
198}
199```
200
201### VariableDeclarator
202
203Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
204
205```js
206/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
207/*eslint-env es6*/
208
209var a,
210 b,
211 c;
212let a,
213 b,
214 c;
215const a = 1,
216 b = 2,
217 c = 3;
218```
219
220Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
221
222```js
223/*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
224/*eslint-env es6*/
225
226var a,
227 b,
228 c;
229let a,
230 b,
231 c;
232const a = 1,
233 b = 2,
234 c = 3;
235```
236
237Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
238
239```js
240/*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
241/*eslint-env es6*/
242
243var a,
244 b,
245 c;
246let a,
247 b,
248 c;
249const a = 1,
250 b = 2,
251 c = 3;
252```
253
254Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": "first" }` options:
255
256```js
257/*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/
258/*eslint-env es6*/
259
260var a,
261 b,
262 c;
263let a,
264 b,
265 c;
266const a = 1,
267 b = 2,
268 c = 3;
269```
270
271Examples of **correct** code for this rule with the `2, { "VariableDeclarator": "first" }` options:
272
273```js
274/*eslint indent: ["error", 2, { "VariableDeclarator": "first" }]*/
275/*eslint-env es6*/
276
277var a,
278 b,
279 c;
280let a,
281 b,
282 c;
283const a = 1,
284 b = 2,
285 c = 3;
286```
287
288Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
289
290```js
291/*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
292/*eslint-env es6*/
293
294var a,
295 b,
296 c;
297let a,
298 b,
299 c;
300const a = 1,
301 b = 2,
302 c = 3;
303```
304
305### outerIIFEBody
306
307Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
308
309```js
310/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
311
312(function() {
313
314 function foo(x) {
315 return x + 1;
316 }
317
318})();
319
320
321if (y) {
322console.log('foo');
323}
324```
325
326Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
327
328```js
329/*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
330
331(function() {
332
333function foo(x) {
334 return x + 1;
335}
336
337})();
338
339
340if (y) {
341 console.log('foo');
342}
343```
344
345Examples of **correct** code for this rule with the options `2, { "outerIIFEBody": "off" }`:
346
347```js
348/*eslint indent: ["error", 2, { "outerIIFEBody": "off" }]*/
349
350(function() {
351
352function foo(x) {
353 return x + 1;
354}
355
356})();
357
358(function() {
359
360 function foo(x) {
361 return x + 1;
362 }
363
364})();
365
366if (y) {
367 console.log('foo');
368}
369```
370
371### MemberExpression
372
373Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
374
375```js
376/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
377
378foo
379.bar
380.baz()
381```
382
383Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
384
385```js
386/*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
387
388foo
389 .bar
390 .baz();
391```
392
393### FunctionDeclaration
394
395Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
396
397```js
398/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
399
400function foo(bar,
401 baz,
402 qux) {
403 qux();
404}
405```
406
407Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
408
409```js
410/*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
411
412function foo(bar,
413 baz,
414 qux) {
415 qux();
416}
417```
418
419Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
420
421```js
422/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
423
424function foo(bar, baz,
425 qux, boop) {
426 qux();
427}
428```
429
430Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
431
432```js
433/*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
434
435function foo(bar, baz,
436 qux, boop) {
437 qux();
438}
439```
440
441### FunctionExpression
442
443Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
444
445```js
446/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
447
448var foo = function(bar,
449 baz,
450 qux) {
451 qux();
452}
453```
454
455Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
456
457```js
458/*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
459
460var foo = function(bar,
461 baz,
462 qux) {
463 qux();
464}
465```
466
467Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
468
469```js
470/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
471
472var foo = function(bar, baz,
473 qux, boop) {
474 qux();
475}
476```
477
478Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
479
480```js
481/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
482
483var foo = function(bar, baz,
484 qux, boop) {
485 qux();
486}
487```
488
609c276f
TL
489### StaticBlock
490
491Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:
492
493```js
494/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/
495
496class C {
497 static {
498 foo();
499 }
500}
501```
502
503Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 1} }` option:
504
505```js
506/*eslint indent: ["error", 2, { "StaticBlock": {"body": 1} }]*/
507
508class C {
509 static {
510 foo();
511 }
512}
513```
514
515Examples of **incorrect** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:
516
517```js
518/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/
519
520class C {
521 static {
522 foo();
523 }
524}
525```
526
527Examples of **correct** code for this rule with the `2, { "StaticBlock": {"body": 2} }` option:
528
529```js
530/*eslint indent: ["error", 2, { "StaticBlock": {"body": 2} }]*/
531
532class C {
533 static {
534 foo();
535 }
536}
537```
538
eb39fafa
DC
539### CallExpression
540
541Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
542
543```js
544/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
545
546foo(bar,
547 baz,
548 qux
549);
550```
551
552Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
553
554```js
555/*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
556
557foo(bar,
558 baz,
559 qux
560);
561```
562
563Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
564
565```js
566/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
567
568foo(bar, baz,
569 baz, boop, beep);
570```
571
572Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
573
574```js
575/*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
576
577foo(bar, baz,
578 baz, boop, beep);
579```
580
581### ArrayExpression
582
583Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
584
585```js
586/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
587
588var foo = [
589 bar,
590baz,
591 qux
592];
593```
594
595Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
596
597```js
598/*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
599
600var foo = [
601 bar,
602 baz,
603 qux
604];
605```
606
607Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
608
609```js
610/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
611
612var foo = [bar,
613 baz,
614 qux
615];
616```
617
618Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
619
620```js
621/*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
622
623var foo = [bar,
624 baz,
625 qux
626];
627```
628
629### ObjectExpression
630
631Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
632
633```js
634/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
635
636var foo = {
637 bar: 1,
638baz: 2,
639 qux: 3
640};
641```
642
643Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
644
645```js
646/*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
647
648var foo = {
649 bar: 1,
650 baz: 2,
651 qux: 3
652};
653```
654
655Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
656
657```js
658/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
659
660var foo = { bar: 1,
661 baz: 2 };
662```
663
664Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
665
666```js
667/*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
668
669var foo = { bar: 1,
670 baz: 2 };
671```
672
673### ImportDeclaration
674
675Examples of **correct** code for this rule with the `4, { "ImportDeclaration": 1 }` option (the default):
676
677```js
678/*eslint indent: ["error", 4, { "ImportDeclaration": 1 }]*/
679
680import { foo,
681 bar,
682 baz,
683} from 'qux';
684
685import {
686 foo,
687 bar,
688 baz,
689} from 'qux';
690```
691
692Examples of **incorrect** code for this rule with the `4, { "ImportDeclaration": "first" }` option:
693
694```js
695/*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/
696
697import { foo,
698 bar,
699 baz,
700} from 'qux';
701```
702
703Examples of **correct** code for this rule with the `4, { "ImportDeclaration": "first" }` option:
704
705```js
706/*eslint indent: ["error", 4, { "ImportDeclaration": "first" }]*/
707
708import { foo,
709 bar,
710 baz,
711} from 'qux';
712```
713
714### flatTernaryExpressions
715
716Examples of **incorrect** code for this rule with the default `4, { "flatTernaryExpressions": false }` option:
717
718```js
719/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/
720
721var a =
722 foo ? bar :
723 baz ? qux :
724 boop;
725```
726
727Examples of **correct** code for this rule with the default `4, { "flatTernaryExpressions": false }` option:
728
729```js
730/*eslint indent: ["error", 4, { "flatTernaryExpressions": false }]*/
731
732var a =
733 foo ? bar :
734 baz ? qux :
735 boop;
736```
737
738Examples of **incorrect** code for this rule with the `4, { "flatTernaryExpressions": true }` option:
739
740```js
741/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/
742
743var a =
744 foo ? bar :
745 baz ? qux :
746 boop;
747```
748
749Examples of **correct** code for this rule with the `4, { "flatTernaryExpressions": true }` option:
750
751```js
752/*eslint indent: ["error", 4, { "flatTernaryExpressions": true }]*/
753
754var a =
755 foo ? bar :
756 baz ? qux :
757 boop;
758```
759
760### offsetTernaryExpressions
761
762Examples of **incorrect** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:
763
764```js
765/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/
766
767condition
768 ? () => {
769 return true
770 }
771 : () => {
772 false
773 }
774```
775
776Examples of **correct** code for this rule with the default `2, { "offsetTernaryExpressions": false }` option:
777
778```js
779/*eslint indent: ["error", 2, { "offsetTernaryExpressions": false }]*/
780
781condition
782 ? () => {
783 return true
784 }
785 : condition2
786 ? () => {
787 return true
788 }
789 : () => {
790 return false
791 }
792```
793
794Examples of **incorrect** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:
795
796```js
797/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/
798
799condition
800 ? () => {
801 return true
802 }
803 : condition2
804 ? () => {
805 return true
806 }
807 : () => {
808 return false
809 }
810```
811
812Examples of **correct** code for this rule with the `2, { "offsetTernaryExpressions": true }` option:
813
814```js
815/*eslint indent: ["error", 2, { "offsetTernaryExpressions": true }]*/
816
817condition
818 ? () => {
819 return true
820 }
821 : condition2
822 ? () => {
823 return true
824 }
825 : () => {
826 return false
827 }
828```
829
eb39fafa
DC
830### ignoreComments
831
832Examples of additional **correct** code for this rule with the `4, { "ignoreComments": true }` option:
833
834```js
835/*eslint indent: ["error", 4, { "ignoreComments": true }] */
836
837if (foo) {
838 doSomething();
839
840// comment intentionally de-indented
841 doSomethingElse();
842}
843```
844
eb39fafa
DC
845## Compatibility
846
847* **JSHint**: `indent`
848* **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)