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