diff --git a/packages/eslint-plugin/docs/rules/member-ordering.md b/packages/eslint-plugin/docs/rules/member-ordering.md index 24c286c3ae90..cccf2bbbe0ab 100644 --- a/packages/eslint-plugin/docs/rules/member-ordering.md +++ b/packages/eslint-plugin/docs/rules/member-ordering.md @@ -166,8 +166,10 @@ The default configuration looks as follows: ```json5 { default: [ + // Index signature 'signature', + // Fields 'public-static-field', 'protected-static-field', 'private-static-field', @@ -481,7 +483,7 @@ const foo = class { }; ``` -Issue: Public static fields should come first, followed by static fields and instance fields. +Note: Public static fields should come first, followed by static fields and instance fields. ##### Correct examples @@ -575,21 +577,19 @@ class Foo { ##### Correct example -Examples of **correct** code for `{ "classes": [...] }` option: - ```ts class Foo { private C: string; // (irrelevant) public D: string; // (irrelevant) - public static E: string; // -> public static field + public B(): void {} // -> public instance method constructor() {} // (irrelevant) public static A(): void {} // (irrelevant) - public B(): void {} // -> public instance method + public static E: string; // -> public static field } ``` @@ -749,7 +749,7 @@ type Foo = { It is possible to sort all members within a group alphabetically. -#### Configuration: `{ default: { order: 'alphabetically' } }` +#### Configuration: `{ default: { memberTypes: , order: 'alphabetically' } }` This will apply the default order (see above) and enforce an alphabetic order within each group. @@ -761,38 +761,36 @@ interface Foo { b: x; c: x; + new (): Bar; + (): Baz; + a(): void; b(): void; c(): void; + // Wrong group order, should be placed before all field definitions [a: string]: number; - (): Baz; - - new (): Bar; } ``` -Note: Wrong group order for `new () : Bar`. - ```ts interface Foo { + [a: string]: number; + a: x; b: x; c: x; new (): Bar; + (): Baz; + // Wrong alphabetic order within group c(): void; b(): void; a(): void; - - [a: string]: number; - (): Baz; } ``` -Note: Wrong alphabetic order (should be `a(): void` --> `b(): void` --> `c(): void`). - ### Sorting alphabetically while ignoring member groups It is also possible to sort all members and ignore the member groups completely. diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index fe34a1a92fe2..f427679284f6 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -3629,14 +3629,14 @@ const sortedWithoutGroupingDefaultOption: TSESLint.RunTests< { code: ` interface Foo { - a : b; - [a: string] : number; - b() : void; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + a(): Foo; + (): Foo; + b(): Foo; } `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], }, // default option + interface + lower/upper case @@ -3668,8 +3668,8 @@ type Foo = { a : b; [a: string] : number; b() : void; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], @@ -3704,7 +3704,7 @@ class Foo { public static a : string; protected static b : string = ""; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -3742,7 +3742,7 @@ const foo = class Foo { public static a : string; protected static b : string = ""; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -3781,8 +3781,8 @@ interface Foo { b() : void; a : b; [a: string] : number; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], @@ -3832,8 +3832,8 @@ type Foo = { b() : void; a : b; [a: string] : number; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], @@ -3883,7 +3883,7 @@ class Foo { protected static b : string = ""; public static a : string; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -3936,7 +3936,7 @@ const foo = class Foo { protected static b : string = ""; public static a : string; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -4068,7 +4068,7 @@ class Foo { public static a : string; protected static b : string = ""; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -4145,7 +4145,7 @@ class Foo { protected static b : string = ""; public static a : string; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -4333,7 +4333,7 @@ const foo = class Foo { public static a : string; protected static b : string = ""; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -4378,7 +4378,7 @@ const foo = class Foo { protected static b : string = ""; public static a : string; private static c : string = ""; - constructor() {} // Will be ignored (no sortable identifier) + constructor() {} public d : string = ""; protected e : string = ""; private f : string = ""; @@ -4442,8 +4442,8 @@ interface Foo { [a: string] : number; a : b; b() : void; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [ @@ -4615,8 +4615,8 @@ interface Foo { b() : void; a : b; [a: string] : number; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [ @@ -4719,8 +4719,8 @@ type Foo = { [a: string] : number; a : b; b() : void; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [ @@ -4850,8 +4850,8 @@ type Foo = { b() : void; a : b; [a: string] : number; - new () : Bar; // Will be ignored (no sortable identifier) - () : Baz; // Will be ignored (no sortable identifier) + new () : Bar; + () : Baz; } `, options: [ @@ -4921,7 +4921,7 @@ interface Foo { b() : void; c() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -4944,7 +4944,7 @@ interface Foo { c : x; [a: string] : number; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -4973,7 +4973,7 @@ type Foo = { b() : void; c() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -4997,7 +4997,7 @@ type Foo = { b : x; c : x; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -5115,7 +5115,7 @@ interface Foo { b() : void; a() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; new () : Bar; } @@ -5148,7 +5148,7 @@ type Foo = { b() : void; a() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; new () : Bar; } @@ -5533,7 +5533,7 @@ interface Foo { new () : Bar; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -5561,7 +5561,7 @@ interface Foo { c : x; [a: string] : number; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -5647,7 +5647,7 @@ interface Foo { b() : void; a() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; new () : Bar; } @@ -5739,7 +5739,7 @@ type Foo = { c : x; [a: string] : number; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; } `, options: [ @@ -5803,7 +5803,7 @@ type Foo = { b() : void; a() : void; - () : Baz; // Will be ignored (no sortable identifier) + () : Baz; new () : Bar; }