]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/indent-legacy.md
first commit
[pve-eslint.git] / eslint / docs / rules / indent-legacy.md
1 # enforce consistent indentation (indent-legacy)
2
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.
4
5 ---
6
7 There are several common guidelines which require specific indentation of nested blocks and statements, like:
8
9 ```js
10 function hello(indentSize, type) {
11 if (indentSize === 4 && type !== 'tab') {
12 console.log('Each next indentation will increase on 4 spaces');
13 }
14 }
15 ```
16
17 These are the most common scenarios recommended in different style guides:
18
19 * Two spaces, not longer and no tabs: Google, npm, Node.js, Idiomatic, Felix
20 * Tabs: jQuery
21 * Four spaces: Crockford
22
23 ## Rule Details
24
25 This rule enforces a consistent indentation style. The default style is `4 spaces`.
26
27 ## Options
28
29 This rule has a mixed option:
30
31 For example, for 2-space indentation:
32
33 ```json
34 {
35 "indent": ["error", 2]
36 }
37 ```
38
39 Or for tabbed indentation:
40
41 ```json
42 {
43 "indent": ["error", "tab"]
44 }
45 ```
46
47 Examples of **incorrect** code for this rule with the default options:
48
49 ```js
50 /*eslint indent: "error"*/
51
52 if (a) {
53 b=c;
54 function foo(d) {
55 e=f;
56 }
57 }
58 ```
59
60 Examples of **correct** code for this rule with the default options:
61
62 ```js
63 /*eslint indent: "error"*/
64
65 if (a) {
66 b=c;
67 function foo(d) {
68 e=f;
69 }
70 }
71 ```
72
73 This rule has an object option:
74
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.
89
90 Level of indentation denotes the multiple of the indent specified. Example:
91
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.
106
107 ### tab
108
109 Examples of **incorrect** code for this rule with the `"tab"` option:
110
111 ```js
112 /*eslint indent: ["error", "tab"]*/
113
114 if (a) {
115 b=c;
116 function foo(d) {
117 e=f;
118 }
119 }
120 ```
121
122 Examples of **correct** code for this rule with the `"tab"` option:
123
124 ```js
125 /*eslint indent: ["error", "tab"]*/
126
127 if (a) {
128 /*tab*/b=c;
129 /*tab*/function foo(d) {
130 /*tab*//*tab*/e=f;
131 /*tab*/}
132 }
133 ```
134
135 ### SwitchCase
136
137 Examples of **incorrect** code for this rule with the `2, { "SwitchCase": 1 }` options:
138
139 ```js
140 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
141
142 switch(a){
143 case "a":
144 break;
145 case "b":
146 break;
147 }
148 ```
149
150 Examples of **correct** code for this rule with the `2, { "SwitchCase": 1 }` option:
151
152 ```js
153 /*eslint indent: ["error", 2, { "SwitchCase": 1 }]*/
154
155 switch(a){
156 case "a":
157 break;
158 case "b":
159 break;
160 }
161 ```
162
163 ### VariableDeclarator
164
165 Examples of **incorrect** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
166
167 ```js
168 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
169 /*eslint-env es6*/
170
171 var a,
172 b,
173 c;
174 let a,
175 b,
176 c;
177 const a = 1,
178 b = 2,
179 c = 3;
180 ```
181
182 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 1 }` options:
183
184 ```js
185 /*eslint indent: ["error", 2, { "VariableDeclarator": 1 }]*/
186 /*eslint-env es6*/
187
188 var a,
189 b,
190 c;
191 let a,
192 b,
193 c;
194 const a = 1,
195 b = 2,
196 c = 3;
197 ```
198
199 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": 2 }` options:
200
201 ```js
202 /*eslint indent: ["error", 2, { "VariableDeclarator": 2 }]*/
203 /*eslint-env es6*/
204
205 var a,
206 b,
207 c;
208 let a,
209 b,
210 c;
211 const a = 1,
212 b = 2,
213 c = 3;
214 ```
215
216 Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
217
218 ```js
219 /*eslint indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }]*/
220 /*eslint-env es6*/
221
222 var a,
223 b,
224 c;
225 let a,
226 b,
227 c;
228 const a = 1,
229 b = 2,
230 c = 3;
231 ```
232
233 ### outerIIFEBody
234
235 Examples of **incorrect** code for this rule with the options `2, { "outerIIFEBody": 0 }`:
236
237 ```js
238 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
239
240 (function() {
241
242 function foo(x) {
243 return x + 1;
244 }
245
246 })();
247
248
249 if(y) {
250 console.log('foo');
251 }
252 ```
253
254 Examples of **correct** code for this rule with the options `2, {"outerIIFEBody": 0}`:
255
256 ```js
257 /*eslint indent: ["error", 2, { "outerIIFEBody": 0 }]*/
258
259 (function() {
260
261 function foo(x) {
262 return x + 1;
263 }
264
265 })();
266
267
268 if(y) {
269 console.log('foo');
270 }
271 ```
272
273 ### MemberExpression
274
275 Examples of **incorrect** code for this rule with the `2, { "MemberExpression": 1 }` options:
276
277 ```js
278 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
279
280 foo
281 .bar
282 .baz()
283 ```
284
285 Examples of **correct** code for this rule with the `2, { "MemberExpression": 1 }` option:
286
287 ```js
288 /*eslint indent: ["error", 2, { "MemberExpression": 1 }]*/
289
290 foo
291 .bar
292 .baz();
293
294 // Any indentation is permitted in variable declarations and assignments.
295 var bip = aardvark.badger
296 .coyote;
297 ```
298
299 ### FunctionDeclaration
300
301 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
302
303 ```js
304 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
305
306 function foo(bar,
307 baz,
308 qux) {
309 qux();
310 }
311 ```
312
313 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }` option:
314
315 ```js
316 /*eslint indent: ["error", 2, { "FunctionDeclaration": {"body": 1, "parameters": 2} }]*/
317
318 function foo(bar,
319 baz,
320 qux) {
321 qux();
322 }
323 ```
324
325 Examples of **incorrect** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
326
327 ```js
328 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
329
330 function foo(bar, baz,
331 qux, boop) {
332 qux();
333 }
334 ```
335
336 Examples of **correct** code for this rule with the `2, { "FunctionDeclaration": {"parameters": "first"} }` option:
337
338 ```js
339 /*eslint indent: ["error", 2, {"FunctionDeclaration": {"parameters": "first"}}]*/
340
341 function foo(bar, baz,
342 qux, boop) {
343 qux();
344 }
345 ```
346
347 ### FunctionExpression
348
349 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
350
351 ```js
352 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
353
354 var foo = function(bar,
355 baz,
356 qux) {
357 qux();
358 }
359 ```
360
361 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"body": 1, "parameters": 2} }` option:
362
363 ```js
364 /*eslint indent: ["error", 2, { "FunctionExpression": {"body": 1, "parameters": 2} }]*/
365
366 var foo = function(bar,
367 baz,
368 qux) {
369 qux();
370 }
371 ```
372
373 Examples of **incorrect** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
374
375 ```js
376 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
377
378 var foo = function(bar, baz,
379 qux, boop) {
380 qux();
381 }
382 ```
383
384 Examples of **correct** code for this rule with the `2, { "FunctionExpression": {"parameters": "first"} }` option:
385
386 ```js
387 /*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/
388
389 var foo = function(bar, baz,
390 qux, boop) {
391 qux();
392 }
393 ```
394
395 ### CallExpression
396
397 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
398
399 ```js
400 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
401
402 foo(bar,
403 baz,
404 qux
405 );
406 ```
407
408 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": 1} }` option:
409
410 ```js
411 /*eslint indent: ["error", 2, { "CallExpression": {"arguments": 1} }]*/
412
413 foo(bar,
414 baz,
415 qux
416 );
417 ```
418
419 Examples of **incorrect** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
420
421 ```js
422 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
423
424 foo(bar, baz,
425 baz, boop, beep);
426 ```
427
428 Examples of **correct** code for this rule with the `2, { "CallExpression": {"arguments": "first"} }` option:
429
430 ```js
431 /*eslint indent: ["error", 2, {"CallExpression": {"arguments": "first"}}]*/
432
433 foo(bar, baz,
434 baz, boop, beep);
435 ```
436
437 ### ArrayExpression
438
439 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": 1 }` option:
440
441 ```js
442 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
443
444 var foo = [
445 bar,
446 baz,
447 qux
448 ];
449 ```
450
451 Examples of **correct** code for this rule with the `2, { "ArrayExpression": 1 }` option:
452
453 ```js
454 /*eslint indent: ["error", 2, { "ArrayExpression": 1 }]*/
455
456 var foo = [
457 bar,
458 baz,
459 qux
460 ];
461 ```
462
463 Examples of **incorrect** code for this rule with the `2, { "ArrayExpression": "first" }` option:
464
465 ```js
466 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
467
468 var foo = [bar,
469 baz,
470 qux
471 ];
472 ```
473
474 Examples of **correct** code for this rule with the `2, { "ArrayExpression": "first" }` option:
475
476 ```js
477 /*eslint indent: ["error", 2, {"ArrayExpression": "first"}]*/
478
479 var foo = [bar,
480 baz,
481 qux
482 ];
483 ```
484
485 ### ObjectExpression
486
487 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": 1 }` option:
488
489 ```js
490 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
491
492 var foo = {
493 bar: 1,
494 baz: 2,
495 qux: 3
496 };
497 ```
498
499 Examples of **correct** code for this rule with the `2, { "ObjectExpression": 1 }` option:
500
501 ```js
502 /*eslint indent: ["error", 2, { "ObjectExpression": 1 }]*/
503
504 var foo = {
505 bar: 1,
506 baz: 2,
507 qux: 3
508 };
509 ```
510
511 Examples of **incorrect** code for this rule with the `2, { "ObjectExpression": "first" }` option:
512
513 ```js
514 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
515
516 var foo = { bar: 1,
517 baz: 2 };
518 ```
519
520 Examples of **correct** code for this rule with the `2, { "ObjectExpression": "first" }` option:
521
522 ```js
523 /*eslint indent: ["error", 2, {"ObjectExpression": "first"}]*/
524
525 var foo = { bar: 1,
526 baz: 2 };
527 ```
528
529 ## Compatibility
530
531 * **JSHint**: `indent`
532 * **JSCS**: [validateIndentation](https://jscs-dev.github.io/rule/validateIndentation)