]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/indent-legacy.md
97372ebc8ff0963f13f7646e6f8dd4105734f9df
[pve-eslint.git] / eslint / docs / src / rules / indent-legacy.md
1 ---
2 title: indent-legacy
3 rule_type: layout
4 ---
5
6
7
8 This rule was **deprecated** in ESLint v4.0.0.
9
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.
11
12 ---
13
14 There are several common guidelines which require specific indentation of nested blocks and statements, like:
15
16 ```js
17 function hello(indentSize, type) {
18 if (indentSize === 4 && type !== 'tab') {
19 console.log('Each next indentation will increase on 4 spaces');
20 }
21 }
22 ```
23
24 These are the most common scenarios recommended in different style guides:
25
26 * Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
27 * Tabs: jQuery
28 * Four spaces: Crockford
29
30 ## Rule Details
31
32 This rule enforces a consistent indentation style. The default style is `4 spaces`.
33
34 ## Options
35
36 This rule has a mixed option:
37
38 For example, for 2-space indentation:
39
40 ```json
41 {
42 "indent": ["error", 2]
43 }
44 ```
45
46 Or for tabbed indentation:
47
48 ```json
49 {
50 "indent": ["error", "tab"]
51 }
52 ```
53
54 Examples of **incorrect** code for this rule with the default options:
55
56 ::: incorrect
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 :::
70
71 Examples of **correct** code for this rule with the default options:
72
73 ::: correct
74
75 ```js
76 /*eslint indent: "error"*/
77
78 if (a) {
79 b=c;
80 function foo(d) {
81 e=f;
82 }
83 }
84 ```
85
86 :::
87
88 This rule has an object option:
89
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.
104
105 Level of indentation denotes the multiple of the indent specified. Example:
106
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.
121
122 ### tab
123
124 Examples of **incorrect** code for this rule with the `"tab"` option:
125
126 ::: incorrect
127
128 ```js
129 /*eslint indent: ["error", "tab"]*/
130
131 if (a) {
132 b=c;
133 function foo(d) {
134 e=f;
135 }
136 }
137 ```
138
139 :::
140
141 Examples of **correct** code for this rule with the `"tab"` option:
142
143 ::: correct
144
145 ```js
146 /*eslint indent: ["error", "tab"]*/
147
148 if (a) {
149 /*tab*/b=c;
150 /*tab*/function foo(d) {
151 /*tab*//*tab*/e=f;
152 /*tab*/}
153 }
154 ```
155
156 :::
157
158 ### SwitchCase
159
160 Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
161
162 ::: incorrect
163
164 ```js
165 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
166
167 switch(a){
168 case "a":
169 break;
170 case "b":
171 break;
172 }
173 ```
174
175 :::
176
177 Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
178
179 ::: correct
180
181 ```js
182 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
183
184 switch(a){
185 case "a":
186 break;
187 case "b":
188 break;
189 }
190 ```
191
192 :::
193
194 ### VariableDeclarator
195
196 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
197
198 ::: incorrect
199
200 ```js
201 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
202 /*eslint-env es6*/
203
204 var a,
205 b,
206 c;
207 let a,
208 b,
209 c;
210 const a = 1,
211 b = 2,
212 c = 3;
213 ```
214
215 :::
216
217 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
218
219 ::: correct
220
221 ```js
222 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
223 /*eslint-env es6*/
224
225 var a,
226 b,
227 c;
228 let a,
229 b,
230 c;
231 const a = 1,
232 b = 2,
233 c = 3;
234 ```
235
236 :::
237
238 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
239
240 ::: correct
241
242 ```js
243 /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
244 /*eslint-env es6*/
245
246 var a,
247 b,
248 c;
249 let a,
250 b,
251 c;
252 const a = 1,
253 b = 2,
254 c = 3;
255 ```
256
257 :::
258
259 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
260
261 ::: correct
262
263 ```js
264 /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
265 /*eslint-env es6*/
266
267 var a,
268 b,
269 c;
270 let a,
271 b,
272 c;
273 const a = 1,
274 b = 2,
275 c = 3;
276 ```
277
278 :::
279
280 ### outerIIFEBody
281
282 Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
283
284 ::: incorrect
285
286 ```js
287 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
288
289 (function() {
290
291 function foo(x) {
292 return x + 1;
293 }
294
295 })();
296
297 if(y) {
298 console.log('foo');
299 }
300 ```
301
302 :::
303
304 Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`:
305
306 ::: correct
307
308 ```js
309 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
310
311 (function() {
312
313 function foo(x) {
314 return x + 1;
315 }
316
317 })();
318
319 if(y) {
320 console.log('foo');
321 }
322 ```
323
324 :::
325
326 ### MemberExpression
327
328 Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
329
330 ::: incorrect
331
332 ```js
333 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
334
335 foo
336 .bar
337 .baz()
338 ```
339
340 :::
341
342 Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
343
344 ::: correct
345
346 ```js
347 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
348
349 foo
350 .bar
351 .baz();
352
353 // Any indentation is permitted in variable declarations and assignments.
354 var bip = aardvark.badger
355 .coyote;
356 ```
357
358 :::
359
360 ### FunctionDeclaration
361
362 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
363
364 ::: incorrect
365
366 ```js
367 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
368
369 function foo(bar,
370 baz,
371 qux) {
372 qux();
373 }
374 ```
375
376 :::
377
378 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
379
380 ::: correct
381
382 ```js
383 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
384
385 function foo(bar,
386 baz,
387 qux) {
388 qux();
389 }
390 ```
391
392 :::
393
394 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
395
396 ::: incorrect
397
398 ```js
399 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
400
401 function foo(bar, baz,
402 qux, boop) {
403 qux();
404 }
405 ```
406
407 :::
408
409 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
410
411 ::: correct
412
413 ```js
414 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
415
416 function foo(bar, baz,
417 qux, boop) {
418 qux();
419 }
420 ```
421
422 :::
423
424 ### FunctionExpression
425
426 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
427
428 ::: incorrect
429
430 ```js
431 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
432
433 var foo = function(bar,
434 baz,
435 qux) {
436 qux();
437 }
438 ```
439
440 :::
441
442 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
443
444 ::: correct
445
446 ```js
447 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
448
449 var foo = function(bar,
450 baz,
451 qux) {
452 qux();
453 }
454 ```
455
456 :::
457
458 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
459
460 ::: incorrect
461
462 ```js
463 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
464
465 var foo = function(bar, baz,
466 qux, boop) {
467 qux();
468 }
469 ```
470
471 :::
472
473 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
474
475 ::: correct
476
477 ```js
478 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
479
480 var foo = function(bar, baz,
481 qux, boop) {
482 qux();
483 }
484 ```
485
486 :::
487
488 ### CallExpression
489
490 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
491
492 ::: incorrect
493
494 ```js
495 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
496
497 foo(bar,
498 baz,
499 qux
500 );
501 ```
502
503 :::
504
505 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
506
507 ::: correct
508
509 ```js
510 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
511
512 foo(bar,
513 baz,
514 qux
515 );
516 ```
517
518 :::
519
520 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
521
522 ::: incorrect
523
524 ```js
525 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
526
527 foo(bar, baz,
528 baz, boop, beep);
529 ```
530
531 :::
532
533 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
534
535 ::: correct
536
537 ```js
538 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
539
540 foo(bar, baz,
541 baz, boop, beep);
542 ```
543
544 :::
545
546 ### ArrayExpression
547
548 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
549
550 ::: incorrect
551
552 ```js
553 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
554
555 var foo = [
556 bar,
557 baz,
558 qux
559 ];
560 ```
561
562 :::
563
564 Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
565
566 ::: correct
567
568 ```js
569 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
570
571 var foo = [
572 bar,
573 baz,
574 qux
575 ];
576 ```
577
578 :::
579
580 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
581
582 ::: incorrect
583
584 ```js
585 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
586
587 var foo = [bar,
588 baz,
589 qux
590 ];
591 ```
592
593 :::
594
595 Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
596
597 ::: correct
598
599 ```js
600 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
601
602 var foo = [bar,
603 baz,
604 qux
605 ];
606 ```
607
608 :::
609
610 ### ObjectExpression
611
612 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
613
614 ::: incorrect
615
616 ```js
617 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
618
619 var foo = {
620 bar: 1,
621 baz: 2,
622 qux: 3
623 };
624 ```
625
626 :::
627
628 Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
629
630 ::: correct
631
632 ```js
633 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
634
635 var foo = {
636 bar: 1,
637 baz: 2,
638 qux: 3
639 };
640 ```
641
642 :::
643
644 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
645
646 ::: incorrect
647
648 ```js
649 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
650
651 var foo = { bar: 1,
652 baz: 2 };
653 ```
654
655 :::
656
657 Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
658
659 ::: correct
660
661 ```js
662 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
663
664 var foo = { bar: 1,
665 baz: 2 };
666 ```
667
668 :::
669
670 ## Compatibility
671
672 * **JSHint**: `indent`
673 * **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)