1 | # Require a consistent member declaration order (member-ordering)
|
2 |
|
3 | A consistent ordering of fields, methods and constructors can make interfaces, type literals, classes and class
|
4 | expressions easier to read, navigate and edit.
|
5 |
|
6 | ## Rule Details
|
7 |
|
8 | This rule aims to standardise the way interfaces, type literals, classes and class expressions are structured.
|
9 |
|
10 | ## Options
|
11 |
|
12 | This rule, in its default state, does not require any argument, in which case the following order is enforced:
|
13 | - `public-static-field`
|
14 | - `protected-static-field`
|
15 | - `private-static-field`
|
16 | - `public-instance-field`
|
17 | - `protected-instance-field`
|
18 | - `private-instance-field`
|
19 | - `public-field` (ignores scope)
|
20 | - `protected-field` (ignores scope)
|
21 | - `private-field` (ignores scope)
|
22 | - `static-field` (ignores accessibility)
|
23 | - `instance-field` (ignores accessibility)
|
24 | - `field` (ignores scope and/or accessibility)
|
25 | - `constructor` (ignores scope and/or accessibility)
|
26 | - `public-static-method`
|
27 | - `protected-static-method`
|
28 | - `private-static-method`
|
29 | - `public-instance-method`
|
30 | - `protected-instance-method`
|
31 | - `private-instance-method`
|
32 | - `public-method` (ignores scope)
|
33 | - `protected-method` (ignores scope)
|
34 | - `private-method` (ignores scope)
|
35 | - `static-method` (ignores accessibility)
|
36 | - `instance-method` (ignores accessibility)
|
37 | - `method` (ignores scope and/or accessibility)
|
38 |
|
39 | The rule can also take one or more of the following options:
|
40 | - `default`, use this to change the default order (used when no specific configuration has been provided).
|
41 | - `classes`, use this to change the order in classes.
|
42 | - `classExpressions`, use this to change the order in class expressions.
|
43 | - `interfaces`, use this to change the order in interfaces.
|
44 | - `typeLiterals`, use this to change the order in type literals.
|
45 |
|
46 | ### default
|
47 | Disable using `never` or use one of the following values to specify an order:
|
48 | - Fields:
|
49 | `public-static-field`
|
50 | `protected-static-field`
|
51 | `private-static-field`
|
52 | `public-instance-field`
|
53 | `protected-instance-field`
|
54 | `private-instance-field`
|
55 | `public-field` (= public-*-field)
|
56 | `protected-field` (= protected-*-field)
|
57 | `private-field` (= private-*-field)
|
58 | `static-field` (= *-static-field)
|
59 | `instance-field` (= *-instance-field)
|
60 | `field` (= all)
|
61 |
|
62 | - Constructors:
|
63 | `public-constructor`
|
64 | `protected-constructor`
|
65 | `private-constructor`
|
66 | `constructor` (= *-constructor)
|
67 |
|
68 | - Methods:
|
69 | `public-static-method`
|
70 | `protected-static-method`
|
71 | `private-static-method`
|
72 | `public-instance-method`
|
73 | `protected-instance-method`
|
74 | `private-instance-method`
|
75 | `public-method` (= public-*-method)
|
76 | `protected-method` (= protected-*-method)
|
77 | `private-method` (= private-*-method)
|
78 | `static-method` (= *-static-method)
|
79 | `instance-method` (= *-instance-method)
|
80 | `method` (= all)
|
81 |
|
82 | Examples of **incorrect** code for the `{ "default": [...] }` option:
|
83 | ```ts
|
84 | // { "default": ["method", "constructor", "field"] }
|
85 |
|
86 | interface Foo {
|
87 | // -> field
|
88 | B: string;
|
89 |
|
90 | // -> constructor
|
91 | new();
|
92 |
|
93 | // -> method
|
94 | A() : void;
|
95 | }
|
96 |
|
97 | type Foo = {
|
98 | // -> field
|
99 | B: string;
|
100 |
|
101 | // no constructor
|
102 |
|
103 | // -> method
|
104 | A() : void;
|
105 | }
|
106 |
|
107 | class Foo {
|
108 | // -> * field
|
109 | private C: string
|
110 | public D: string
|
111 | protected static E: string
|
112 |
|
113 | // -> constructor
|
114 | constructor() {}
|
115 |
|
116 | // -> * method
|
117 | public static A(): void {}
|
118 | public B(): void {}
|
119 | }
|
120 |
|
121 | const Foo = class {
|
122 | // -> * field
|
123 | private C: string
|
124 | public D: string
|
125 |
|
126 | // -> constructor
|
127 | constructor() {}
|
128 |
|
129 | // -> * method
|
130 | public static A(): void {}
|
131 | public B(): void {}
|
132 |
|
133 | // * field
|
134 | protected static E: string
|
135 | }
|
136 |
|
137 | // { "default": ["public-instance-method", "public-static-field"] }
|
138 |
|
139 | // does not apply for interfaces/type literals (accessibility and scope are not part of interfaces/type literals)
|
140 |
|
141 | class Foo {
|
142 | // private instance field
|
143 | private C: string
|
144 |
|
145 | // public instance field
|
146 | public D: string
|
147 |
|
148 | // -> public static field
|
149 | public static E: string
|
150 |
|
151 | // constructor
|
152 | constructor() {}
|
153 |
|
154 | // public static method
|
155 | public static A(): void {}
|
156 |
|
157 | // -> public instance method
|
158 | public B(): void {}
|
159 | }
|
160 |
|
161 | const Foo = class {
|
162 | // private instance field
|
163 | private C: string
|
164 |
|
165 | // -> public static field
|
166 | public static E: string
|
167 |
|
168 | // public instance field
|
169 | public D: string
|
170 |
|
171 | // constructor
|
172 | constructor() {}
|
173 |
|
174 | // public static method
|
175 | public static A(): void {}
|
176 |
|
177 | // -> public instance method
|
178 | public B(): void {}
|
179 | }
|
180 | ```
|
181 |
|
182 | Examples of **correct** code for the `{ "default": [...] }` option:
|
183 | ```ts
|
184 | // { "default": ["method", "constructor", "field"] }
|
185 |
|
186 | interface Foo {
|
187 | // -> method
|
188 | A() : void;
|
189 |
|
190 | // -> constructor
|
191 | new();
|
192 |
|
193 | // -> field
|
194 | B: string;
|
195 | }
|
196 |
|
197 | type Foo = {
|
198 | // -> method
|
199 | A() : void;
|
200 |
|
201 | // -> field
|
202 | B: string;
|
203 | }
|
204 |
|
205 | class Foo {
|
206 | // -> * method
|
207 | public static A(): void {}
|
208 | public B(): void {}
|
209 |
|
210 | // -> constructor
|
211 | constructor() {}
|
212 |
|
213 | // -> * field
|
214 | private C: string
|
215 | public D: string
|
216 | protected static E: string
|
217 | }
|
218 |
|
219 | const Foo = class {
|
220 | // -> * method
|
221 | public static A(): void {}
|
222 | public B(): void {}
|
223 |
|
224 | // -> constructor
|
225 | constructor() {}
|
226 |
|
227 | // -> * field
|
228 | private C: string
|
229 | public D: string
|
230 | protected static E: string
|
231 | }
|
232 |
|
233 | // { "default": ["public-instance-method", "public-static-field"] }
|
234 |
|
235 | // does not apply for interfaces/type literals (accessibility and scope are not part of interfaces/type literals)
|
236 |
|
237 | class Foo {
|
238 | // -> public instance method
|
239 | public B(): void {}
|
240 |
|
241 | // private instance field
|
242 | private C: string
|
243 |
|
244 | // public instance field
|
245 | public D: string
|
246 |
|
247 | // -> public static field
|
248 | public static E: string
|
249 |
|
250 | // constructor
|
251 | constructor() {}
|
252 |
|
253 | // public static method
|
254 | public static A(): void {}
|
255 | }
|
256 |
|
257 | const Foo = class {
|
258 | // -> public instance method
|
259 | public B(): void {}
|
260 |
|
261 | // private instance field
|
262 | private C: string
|
263 |
|
264 | // public instance field
|
265 | public D: string
|
266 |
|
267 | // constructor
|
268 | constructor() {}
|
269 |
|
270 | // public static method
|
271 | public static A(): void {}
|
272 |
|
273 | // -> protected static field
|
274 | protected static: string
|
275 | }
|
276 |
|
277 | // { "default": ["public-static-field", "static-field", "instance-field"] }
|
278 |
|
279 | // does not apply for interfaces/type literals (accessibility and scope are not part of interfaces/type literals)
|
280 |
|
281 | class Foo {
|
282 | // -> public static field
|
283 | public static A: string;
|
284 |
|
285 | // -> * static field
|
286 | private static B: string;
|
287 | protected statis C:string;
|
288 | private static D: string;
|
289 |
|
290 | // -> * instance field
|
291 | private E: string;
|
292 | }
|
293 |
|
294 | const foo = class {
|
295 | // * method
|
296 | public T(): void {}
|
297 |
|
298 | // -> public static field
|
299 | public static A: string;
|
300 |
|
301 | // constructor
|
302 | constructor(){}
|
303 |
|
304 | // -> * static field
|
305 | private static B: string;
|
306 | protected statis C:string;
|
307 | private static D: string;
|
308 |
|
309 | // -> * instance field
|
310 | private E: string;
|
311 | }
|
312 | ```
|
313 |
|
314 | ### classes
|
315 | Disable using `never` or use one of the valid values (see default) to specify an order.
|
316 |
|
317 | Examples of **incorrect** code for the `{ "classes": [...] }` option:
|
318 | ```ts
|
319 | // { "classes": ["method", "constructor", "field"] }
|
320 |
|
321 | // does not apply for interfaces/type literals/class expressions.
|
322 |
|
323 | class Foo {
|
324 | // -> field
|
325 | private C: string
|
326 | public D: string
|
327 | protected static E: string
|
328 |
|
329 | // -> constructor
|
330 | constructor() {}
|
331 |
|
332 | // -> method
|
333 | public static A(): void {}
|
334 | public B(): void {}
|
335 | }
|
336 |
|
337 | // { "classes": ["public-instance-method", "public-static-field"] }
|
338 |
|
339 | // does not apply for interfaces/type literals/class expressions.
|
340 |
|
341 | class Foo {
|
342 | // private instance field
|
343 | private C: string
|
344 |
|
345 | // public instance field
|
346 | public D: string
|
347 |
|
348 | // -> public static field
|
349 | public static E: string
|
350 |
|
351 | // constructor
|
352 | constructor() {}
|
353 |
|
354 | // public static method
|
355 | public static A(): void {}
|
356 |
|
357 | // -> public instance method
|
358 | public B(): void {}
|
359 | }
|
360 | ```
|
361 |
|
362 | Examples of **correct** code for `{ "classes": [...] }` option:
|
363 | ```ts
|
364 | // { "classes": ["method", "constructor", "field"] }
|
365 |
|
366 | // does not apply for interfaces/type literals/class expressions.
|
367 |
|
368 | class Foo {
|
369 | // -> * method
|
370 | public static A(): void {}
|
371 | public B(): void {}
|
372 |
|
373 | // -> constructor
|
374 | constructor() {}
|
375 |
|
376 | // -> * field
|
377 | private C: string
|
378 | public D: string
|
379 | protected static E: string
|
380 | }
|
381 |
|
382 | // { "classes": ["public-instance-method", "public-static-field"] }
|
383 |
|
384 | // does not apply for interfaces/type literals/class expressions.
|
385 |
|
386 | class Foo {
|
387 | // private instance field
|
388 | private C: string
|
389 |
|
390 | // public instance field
|
391 | public D: string
|
392 |
|
393 | // -> public static field
|
394 | public static E: string
|
395 |
|
396 | // constructor
|
397 | constructor() {}
|
398 |
|
399 | // public static method
|
400 | public static A(): void {}
|
401 |
|
402 | // -> public instance method
|
403 | public B(): void {}
|
404 | }
|
405 | ```
|
406 |
|
407 | ### classExpressions
|
408 | Disable using `never` or use one of the valid values (see default) to specify an order.
|
409 |
|
410 | Examples of **incorrect** code for the `{ "classExpressions": [...] }` option:
|
411 | ```ts
|
412 | // { "classExpressions": ["method", "constructor", "field"] }
|
413 |
|
414 | // does not apply for interfaces/type literals/class expressions.
|
415 |
|
416 | const foo = class {
|
417 | // -> field
|
418 | private C: string
|
419 | public D: string
|
420 | protected static E: string
|
421 |
|
422 | // -> constructor
|
423 | constructor() {}
|
424 |
|
425 | // -> method
|
426 | public static A(): void {}
|
427 | public B(): void {}
|
428 | }
|
429 |
|
430 | // { "classExpressions": ["public-instance-method", "public-static-field"] }
|
431 |
|
432 | // does not apply for interfaces/type literals/class expressions.
|
433 |
|
434 | const foo = class {
|
435 | // private instance field
|
436 | private C: string
|
437 |
|
438 | // public instance field
|
439 | public D: string
|
440 |
|
441 | // -> public static field
|
442 | public static E: string
|
443 |
|
444 | // constructor
|
445 | constructor() {}
|
446 |
|
447 | // public static method
|
448 | public static A(): void {}
|
449 |
|
450 | // -> public instance method
|
451 | public B(): void {}
|
452 | }
|
453 | ```
|
454 |
|
455 | Examples of **correct** code for `{ "classExpressions": [...] }` option:
|
456 | ```ts
|
457 | // { "classExpressions": ["method", "constructor", "field"] }
|
458 |
|
459 | // does not apply for interfaces/type literals/class expressions.
|
460 |
|
461 | const foo = class {
|
462 | // -> * method
|
463 | public static A(): void {}
|
464 | public B(): void {}
|
465 |
|
466 | // -> constructor
|
467 | constructor() {}
|
468 |
|
469 | // -> * field
|
470 | private C: string
|
471 | public D: string
|
472 | protected static E: string
|
473 | }
|
474 |
|
475 | // { "classExpressions": ["public-instance-method", "public-static-field"] }
|
476 |
|
477 | // does not apply for interfaces/type literals/class expressions.
|
478 |
|
479 | const foo = class {
|
480 | // private instance field
|
481 | private C: string
|
482 |
|
483 | // public instance field
|
484 | public D: string
|
485 |
|
486 | // -> public static field
|
487 | public static E: string
|
488 |
|
489 | // constructor
|
490 | constructor() {}
|
491 |
|
492 | // public static method
|
493 | public static A(): void {}
|
494 |
|
495 | // -> public instance method
|
496 | public B(): void {}
|
497 | }
|
498 | ```
|
499 |
|
500 | ### interfaces
|
501 | Disable using `never` or use one of the following values to specify an order:
|
502 | `field`
|
503 | `constructor`
|
504 | `method`
|
505 |
|
506 | Examples of **incorrect** code for the `{ "interfaces": [...] }` option:
|
507 | ```ts
|
508 | // { "interfaces": ["method", "constructor", "field"] }
|
509 |
|
510 | // does not apply for classes/class expressions/type literals
|
511 |
|
512 | interface Foo {
|
513 | // -> field
|
514 | B: string;
|
515 |
|
516 | // -> constructor
|
517 | new();
|
518 |
|
519 | // -> method
|
520 | A() : void;
|
521 | }
|
522 | ```
|
523 |
|
524 | Examples of **correct** code for the `{ "interfaces": [...] }` option:
|
525 | ```ts
|
526 | // { "interfaces": ["method", "constructor", "field"] }
|
527 |
|
528 | // does not apply for classes/class expressions/type literals
|
529 |
|
530 | interface Foo {
|
531 | // -> method
|
532 | A() : void;
|
533 |
|
534 | // -> constructor
|
535 | new();
|
536 |
|
537 | // -> field
|
538 | B: string;
|
539 | }
|
540 | ```
|
541 |
|
542 | ### typeLiterals
|
543 | Disable using `never` or use one of the valid values (see interfaces) to specify an order.
|
544 |
|
545 | Examples of **incorrect** code for the `{ "typeLiterals": [...] }` option:
|
546 | ```ts
|
547 | // { "typeLiterals": ["method", "constructor", "field"] }
|
548 |
|
549 | // does not apply for classes/class expressions/interfaces
|
550 |
|
551 | type Foo = {
|
552 | // -> field
|
553 | B: string;
|
554 |
|
555 | // -> method
|
556 | A() : void;
|
557 | }
|
558 | ```
|
559 |
|
560 | Examples of **correct** code for the `{ "typeLiterals": [...] }` option:
|
561 | ```ts
|
562 | // { "typeLiterals": ["method", "constructor", "field"] }
|
563 |
|
564 | // does not apply for classes/class expressions/interfaces
|
565 |
|
566 | type Foo = {
|
567 | // -> method
|
568 | A() : void;
|
569 |
|
570 | // -> constructor
|
571 | new();
|
572 |
|
573 | // -> field
|
574 | B: string;
|
575 | }
|
576 | ```
|
577 |
|
578 | ## When Not To Use It
|
579 |
|
580 | If you don't care about the general structure of your classes and interfaces, then you will not need this rule.
|
581 |
|
582 | ## Compatibility
|
583 |
|
584 | * TSLint: [member-ordering](https://palantir.github.io/tslint/rules/member-ordering/) |
\ | No newline at end of file |