diff --git a/packages/eslint-plugin/docs/rules/member-ordering.md b/packages/eslint-plugin/docs/rules/member-ordering.md index 5924b18aaaa..7a83efd213c 100644 --- a/packages/eslint-plugin/docs/rules/member-ordering.md +++ b/packages/eslint-plugin/docs/rules/member-ordering.md @@ -26,10 +26,10 @@ These options allow to specify how to group the members and sort their groups. type TypeOptions = | { memberTypes: Array | 'never', - order?: 'alphabetically' | 'as-written', + order?: 'alphabetically' | 'alphabetically-case-insensitive' | 'as-written', } | { - order: 'alphabetically', + order: 'alphabetically' | 'alphabetically-case-insensitive' | 'as-written', }; { @@ -956,21 +956,21 @@ It is possible to sort all members within a group alphabetically. #### Configuration: `{ "default": { "memberTypes": , "order": "alphabetically" } }` -This will apply the default order (see above) and enforce an alphabetic order within each group. +This will apply the default order (see above) and enforce an alphabetic case-sensitive order within each group. ##### Incorrect examples ```ts interface Foo { + B: x; a: x; - b: x; c: x; new (): Bar; (): Baz; + B(): void; a(): void; - b(): void; c(): void; // Wrong group order, should be placed before all field definitions @@ -982,8 +982,8 @@ interface Foo { interface Foo { [a: string]: number; + B: x; a: x; - b: x; c: x; new (): Bar; @@ -991,7 +991,7 @@ interface Foo { // Wrong alphabetic order within group c(): void; - b(): void; + B(): void; a(): void; } ``` @@ -1017,6 +1017,73 @@ interface Foo { Note: Wrong alphabetic order `b(): void` should come after `a: b`. +### Sorting alphabetically case-insensitive within member groups + +It is possible to sort all members within a group alphabetically with case insensitivity. + +#### Configuration: `{ "default": { "memberTypes": , "order": "alphabetically-case-insensitive" } }` + +This will apply the default order (see above) and enforce an alphabetic case-insensitive order within each group. + +##### Incorrect examples + +```ts +interface Foo { + a: x; + 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; +} +``` + +```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; +} +``` + +### Sorting alphabetically case-insensitive while ignoring member groups + +It is also possible to sort all members with case insensitivity and ignore the member groups completely. + +#### Configuration: `{ "default": { "memberTypes": "never", "order": "alphabetically-case-insensitive" } }` + +##### Incorrect example + +```ts +interface Foo { + B(): void; + a: number; + + [a: string]: number; // Order doesn't matter (no sortable identifier) + new (): Bar; // Order doesn't matter (no sortable identifier) + (): Baz; // Order doesn't matter (no sortable identifier) +} +``` + +Note: Wrong alphabetic order `B(): void` should come after `a: number`. + ## When Not To Use It If you don't care about the general structure of your classes and interfaces, then you will not need this rule. diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index c1e30adc2ef..fd93da81d65 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -8,9 +8,14 @@ import * as util from '../util'; export type MessageIds = 'incorrectGroupOrder' | 'incorrectOrder'; +type Order = + | 'alphabetically' + | 'alphabetically-case-insensitive' + | 'as-written'; + interface SortedOrderConfig { memberTypes?: string[] | 'never'; - order: 'alphabetically' | 'as-written'; + order: Order; } type OrderConfig = string[] | SortedOrderConfig | 'never'; @@ -46,7 +51,7 @@ const objectConfig = (memberTypes: string[]): JSONSchema.JSONSchema4 => ({ }, order: { type: 'string', - enum: ['alphabetically', 'as-written'], + enum: ['alphabetically', 'alphabetically-case-insensitive', 'as-written'], }, }, additionalProperties: false, @@ -539,10 +544,14 @@ export default util.createRule({ * Checks if the members are alphabetically sorted. * * @param members Members to be validated. + * @param caseSensitive indicates if the alpha ordering is case sensitive or not. * * @return True if all members are correctly sorted. */ - function checkAlphaSort(members: Member[]): boolean { + function checkAlphaSort( + members: Member[], + caseSensitive: boolean, + ): boolean { let previousName = ''; let isCorrectlySorted = true; @@ -552,7 +561,11 @@ export default util.createRule({ // Note: Not all members have names if (name) { - if (name < previousName) { + if ( + caseSensitive + ? name < previousName + : name.toLowerCase() < previousName.toLowerCase() + ) { context.report({ node: member, messageId: 'incorrectOrder', @@ -589,7 +602,7 @@ export default util.createRule({ } // Standardize config - let order = null; + let order: Order | null = null; let memberTypes; if (Array.isArray(orderConfig)) { @@ -599,6 +612,10 @@ export default util.createRule({ memberTypes = orderConfig.memberTypes; } + const hasAlphaSort = order?.startsWith('alphabetically'); + const alphaSortIsCaseSensitive = + order !== 'alphabetically-case-insensitive'; + // Check order if (Array.isArray(memberTypes)) { const grouped = checkGroupSort(members, memberTypes, supportsModifiers); @@ -607,11 +624,14 @@ export default util.createRule({ return; } - if (order === 'alphabetically') { - grouped.some(groupMember => !checkAlphaSort(groupMember)); + if (hasAlphaSort) { + grouped.some( + groupMember => + !checkAlphaSort(groupMember, alphaSortIsCaseSensitive), + ); } - } else if (order === 'alphabetically') { - checkAlphaSort(members); + } else if (hasAlphaSort) { + checkAlphaSort(members, alphaSortIsCaseSensitive); } } diff --git a/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-case-insensitive-order.test.ts b/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-case-insensitive-order.test.ts new file mode 100644 index 00000000000..db62c315101 --- /dev/null +++ b/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-case-insensitive-order.test.ts @@ -0,0 +1,660 @@ +import rule, { + defaultOrder, + MessageIds, + Options, +} from '../../src/rules/member-ordering'; +import { RuleTester } from '../RuleTester'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; + +const ruleTester = new RuleTester({ + parser: '@typescript-eslint/parser', +}); + +const sortedCiWithoutGrouping: TSESLint.RunTests = { + valid: [ + // default option + interface + lower/upper case + { + code: ` +interface Foo { + a : b; + B : b; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + type literal + lower/upper case + { + code: ` +type Foo = { + a : b; + B : b; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class + lower/upper case + { + code: ` +class Foo { + public static a : string; + public static B : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class expression + lower/upper case + { + code: ` +const foo = class Foo { + public static a : string; + public static B : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class + decorators + { + code: ` +class Foo { + public static a : string; + @Dec() static B : string; + public static c : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + ], + invalid: [ + // default option + interface + wrong order (multiple) + { + code: ` +interface Foo { + c : string; + B : string; + a : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'B', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'B', + }, + }, + ], + }, + + // default option + interface + lower/upper case wrong order + { + code: ` +interface Foo { + B : b; + a : b; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'B', + }, + }, + ], + }, + + // default option + type literal + lower/upper case wrong order + { + code: ` +type Foo = { + B : b; + a : b; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'B', + }, + }, + ], + }, + + // default option + class + lower/upper case wrong order + { + code: ` +class Foo { + public static B : string; + public static a : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'B', + }, + }, + ], + }, + + // default option + class expression + lower/upper case wrong order + { + code: ` +const foo = class Foo { + public static B : string; + public static a : string; +} + `, + options: [ + { + default: { + memberTypes: 'never', + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'B', + }, + }, + ], + }, + ], +}; + +const sortedCiWithGrouping: TSESLint.RunTests = { + valid: [ + // default option + interface + default order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + () : Baz; + + a : x; + B : x; + c : x; + + new () : Bar; + + a() : void; + B() : void; + c() : void; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + interface + custom order + alphabetically + { + code: ` +interface Foo { + new () : Bar; + + a() : void; + B() : void; + c() : void; + + a : x; + B : x; + c : x; + + [a: string] : number; + () : Baz; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + type literal + default order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + () : Baz; + + a : x; + B : x; + c : x; + + new () : Bar; + + a() : void; + B() : void; + c() : void; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + type literal + custom order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + new () : Bar; + + a() : void; + B() : void; + c() : void; + + a : x; + B : x; + c : x; + + () : Baz; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class + default order + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected E: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + // default option + class + decorators + default order + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + @Dec() public d: string; + @Dec() protected E: string; + @Dec() private f: string; + + public g: string = ""; + protected h: string = ""; + private i: string = ""; + + constructor() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class + custom order + alphabetically + { + code: ` +class Foo { + constructor() {} + + public d: string = ""; + protected E: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class expression + default order + alphabetically + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected E: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + + // default option + class expression + custom order + alphabetically + { + code: ` +const foo = class Foo { + constructor() {} + + public d: string = ""; + protected E: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically-case-insensitive', + }, + }, + ], + }, + ], + invalid: [ + // default option + interface + wrong order within group and wrong group order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + a : x; + B : x; + c : x; + + c() : void; + B() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + + // default option + type literal + wrong order within group and wrong group order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + a : x; + B : x; + c : x; + + c() : void; + B() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + + // default option + class + wrong order within group and wrong group order + alphabetically + { + code: ` +class Foo { + public static c: string = ""; + public static B: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + + // default option + class expression + wrong order within group and wrong group order + alphabetically + { + code: ` +const foo = class Foo { + public static c: string = ""; + public static B: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically-case-insensitive', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + ], +}; + +ruleTester.run('member-ordering-alphabetically-case-insensitive-order', rule, { + valid: [...sortedCiWithoutGrouping.valid, ...sortedCiWithGrouping.valid], + invalid: [ + ...sortedCiWithoutGrouping.invalid, + ...sortedCiWithGrouping.invalid, + ], +}); diff --git a/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-order.test.ts b/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-order.test.ts new file mode 100644 index 00000000000..7d910e09a33 --- /dev/null +++ b/packages/eslint-plugin/tests/rules/member-ordering-alphabetically-order.test.ts @@ -0,0 +1,2611 @@ +import rule, { + defaultOrder, + MessageIds, + Options, +} from '../../src/rules/member-ordering'; +import { RuleTester } from '../RuleTester'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; + +const ruleTester = new RuleTester({ + parser: '@typescript-eslint/parser', +}); +const sortedWithoutGroupingDefaultOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // default option + interface + multiple types + { + code: ` +interface Foo { + (): Foo; + a(): Foo; + b(): Foo; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + interface + lower/upper case + { + code: ` +interface Foo { + A : b; + a : b; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + interface + numbers + { + code: ` +interface Foo { + a1 : b; + aa : b; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + type literal + multiple types + { + code: ` +type Foo = { + a : b; + [a: string] : number; + b() : void; + () : Baz; + new () : Bar; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + type literal + lower/upper case + { + code: ` +type Foo = { + A : b; + a : b; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + type literal + numbers + { + code: ` +type Foo = { + a1 : b; + aa : b; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class + multiple types + { + code: ` +class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + constructor() {} + @Dec() d: string; + public e : string = ""; + @Dec() f : string = ""; + protected g : string = ""; + private h : string = ""; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class + lower/upper case + { + code: ` +class Foo { + public static A : string; + public static a : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class + numbers + { + code: ` +class Foo { + public static a1 : string; + public static aa : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class expression + multiple types + { + code: ` +const foo = class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class expression + lower/upper case + { + code: ` +const foo = class Foo { + public static A : string; + public static a : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class expression + numbers + { + code: ` +const foo = class Foo { + public static a1 : string; + public static aa : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + class + decorators + { + code: ` +class Foo { + public static a : string; + @Dec() static b : string; + public static c : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // default option + private identifiers + { + code: ` +class Foo { + #a = 1; + #b = 2; + #c = 3; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + ], + invalid: [ + // default option + interface + wrong order + { + code: ` +interface Foo { + b() : void; + a : b; + [a: string] : number; + new () : Bar; + () : Baz; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, + ], + }, + + // default option + interface + wrong order (multiple) + { + code: ` +interface Foo { + c : string; + b : string; + a : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // default option + type literal + wrong order + { + code: ` +type Foo = { + b() : void; + a : b; + [a: string] : number; + new () : Bar; + () : Baz; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, + ], + }, + + // default option + type literal + wrong order (multiple) + { + code: ` +type Foo = { + c : string; + b : string; + a : string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // default option + class + wrong order + { + code: ` +class Foo { + protected static b : string = ""; + public static a : string; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // default option + class + wrong order (multiple) + { + code: ` +class Foo { + public static c: string; + public static b: string; + public static a: string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // default option + class expression + wrong order + { + code: ` +const foo = class Foo { + protected static b : string = ""; + public static a : string; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // default option + class expression + wrong order (multiple) + { + code: ` +const foo = class Foo { + public static c: string; + public static b: string; + public static a: string; +} + `, + options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + ], +}; + +const sortedWithoutGroupingClassesOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // classes option + interface + multiple types --> Only member group order is checked (default config) + { + code: ` +interface Foo { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + interface + lower/upper case --> Only member group order is checked (default config) + { + code: ` +interface Foo { + a : b; + A : b; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + interface + numbers --> Only member group order is checked (default config) + { + code: ` +interface Foo { + aa : b; + a1 : b; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + type literal + multiple types --> Only member group order is checked (default config) + { + code: ` +type Foo = { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + type literal + lower/upper case --> Only member group order is checked (default config) + { + code: ` +type Foo = { + a : b; + A : b; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + type literal + numbers --> Only member group order is checked (default config) + { + code: ` +type Foo = { + aa : b; + a1 : b; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class + multiple types + { + code: ` +class Foo { + public static a : string; + protected static b : string = ""; + @Dec() private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + @Dec() + private f : string = ""; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class + lower/upper case + { + code: ` +class Foo { + public static A : string; + public static a : string; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class + numbers + { + code: ` +class Foo { + public static a1 : string; + public static aa : string; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class expression + multiple types --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class expression + lower/upper case --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + public static A : string; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + + // classes option + class expression + numbers --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + }, + ], + invalid: [ + // classes option + class + wrong order + { + code: ` +class Foo { + protected static b : string = ""; + public static a : string; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // classes option + class + wrong order (multiple) + { + code: ` +class Foo { + public static c: string; + public static b: string; + public static a: string; +} + `, + options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + ], +}; + +const sortedWithoutGroupingClassExpressionsOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // classExpressions option + interface + multiple types --> Only member group order is checked (default config) + { + code: ` +interface Foo { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + interface + lower/upper case --> Only member group order is checked (default config) + { + code: ` +interface Foo { + a : b; + A : b; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + interface + numbers --> Only member group order is checked (default config) + { + code: ` +interface Foo { + aa : b; + a1 : b; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + type literal + multiple types --> Only member group order is checked (default config) + { + code: ` +type Foo = { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + type literal + lower/upper case --> Only member group order is checked (default config) + { + code: ` +type Foo = { + a : b; + A : b; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + type literal + numbers --> Only member group order is checked (default config) + { + code: ` +type Foo = { + aa : b; + a1 : b; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class + multiple types --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class + lower/upper case --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + public static A : string; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class + numbers --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class expression + multiple types + { + code: ` +const foo = class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class expression + lower/upper case + { + code: ` +const foo = class Foo { + public static A : string; + public static a : string; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // classExpressions option + class expression + numbers + { + code: ` +const foo = class Foo { + public static a1 : string; + public static aa : string; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + ], + invalid: [ + // classExpressions option + class expression + wrong order + { + code: ` +const foo = class Foo { + protected static b : string = ""; + public static a : string; + private static c : string = ""; + constructor() {} + public d : string = ""; + protected e : string = ""; + private f : string = ""; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + + // classExpressions option + class expression + wrong order (multiple) + { + code: ` +const foo = class Foo { + public static c: string; + public static b: string; + public static a: string; +} + `, + options: [ + { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + ], +}; + +const sortedWithoutGroupingInterfacesOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // interfaces option + interface + multiple types + { + code: ` +interface Foo { + [a: string] : number; + a : b; + b() : void; + () : Baz; + new () : Bar; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + interface + lower/upper case + { + code: ` +interface Foo { + A : b; + a : b; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + interface + numbers + { + code: ` +interface Foo { + a1 : b; + aa : b; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + type literal + multiple types --> Only member group order is checked (default config) + { + code: ` +type Foo = { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + type literal + lower/upper case --> Only member group order is checked (default config) + { + code: ` +type Foo = { + a : b; + A : b; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + type literal + numbers --> Only member group order is checked (default config) + { + code: ` +type Foo = { + aa : b; + a1 : b; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class + multiple types --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class + lower/upper case --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + public static A : string; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class + numbers --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class expression + multiple types --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class expression + lower/upper case --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + public static A : string; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // interfaces option + class expression + numbers --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + ], + invalid: [ + // interfaces option + interface + wrong order + { + code: ` +interface Foo { + b() : void; + a : b; + [a: string] : number; + new () : Bar; + () : Baz; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, + ], + }, + + // interfaces option + interface + wrong order (multiple) + { + code: ` +interface Foo { + c : string; + b : string; + a : string; +} + `, + options: [ + { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + ], +}; + +const sortedWithoutGroupingTypeLiteralsOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // typeLiterals option + interface + multiple types --> Only member group order is checked (default config) + { + code: ` +interface Foo { + [a: string] : number; + () : Baz; + c : b; + new () : Bar; + b() : void; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + interface + lower/upper case --> Only member group order is checked (default config) + { + code: ` +interface Foo { + a : b; + A : b; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + interface + numbers --> Only member group order is checked (default config) + { + code: ` +interface Foo { + aa : b; + a1 : b; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + type literal + multiple types + { + code: ` +type Foo = { + [a: string] : number; + a : b; + b() : void; + () : Baz; + new () : Bar; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + type literal + lower/upper case + { + code: ` +type Foo = { + A : b; + a : b; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + type literal + numbers + { + code: ` +type Foo = { + a1 : b; + aa : b; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class + multiple types --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class + lower/upper case --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static a : string; + public static A : string; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class + numbers --> Only member group order is checked (default config) + { + code: ` +class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class expression + multiple types --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + protected static b : string = ""; + private static c : string = ""; + public d : string = ""; + protected e : string = ""; + private f : string = ""; + constructor() {} +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class expression + lower/upper case --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static a : string; + public static A : string; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + + // typeLiterals option + class expression + numbers --> Only member group order is checked (default config) + { + code: ` +const foo = class Foo { + public static aa : string; + public static a1 : string; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + }, + ], + invalid: [ + // typeLiterals option + type literal + wrong order + { + code: ` +type Foo = { + b() : void; + a : b; + [a: string] : number; + new () : Bar; + () : Baz; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'call', + beforeMember: 'new', + }, + }, + ], + }, + + // typeLiterals option + type literal + wrong order (multiple) + { + code: ` +type Foo = { + c : string; + b : string; + a : string; +} + `, + options: [ + { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + data: { + member: 'b', + beforeMember: 'c', + }, + }, + { + messageId: 'incorrectOrder', + data: { + member: 'a', + beforeMember: 'b', + }, + }, + ], + }, + ], +}; + +const sortedWithGroupingDefaultOption: TSESLint.RunTests = + { + valid: [ + // default option + interface + default order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + () : Baz; + + a : x; + b : x; + c : x; + + new () : Bar; + + a() : void; + b() : void; + c() : void; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + interface + custom order + alphabetically + { + code: ` +interface Foo { + new () : Bar; + + a() : void; + b() : void; + c() : void; + + a : x; + b : x; + c : x; + + [a: string] : number; + () : Baz; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically', + }, + }, + ], + }, + + // default option + type literal + default order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + () : Baz; + + a : x; + b : x; + c : x; + + new () : Bar; + + a() : void; + b() : void; + c() : void; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + type literal + custom order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + new () : Bar; + + a() : void; + b() : void; + c() : void; + + a : x; + b : x; + c : x; + + () : Baz; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically', + }, + }, + ], + }, + + // default option + class + default order + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + class + defaultOrder + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} + + get h() {} + + set g() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically', + }, + }, + ], + }, + + // default option + class + custom + alphabetically + { + code: ` +class Foo { + get a() {} + + @Bar + get b() {} + + set c() {} + + @Bar + set d() {} +} + `, + options: [ + { + default: { + memberTypes: ['get', 'decorated-get', 'set', 'decorated-set'], + order: 'alphabetically', + }, + }, + ], + }, + + // default option + class + decorators + default order + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + @Dec() public d: string; + @Dec() protected e: string; + @Dec() private f: string; + + public g: string = ""; + protected h: string = ""; + private i: string = ""; + + constructor() {} +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + class + custom order + alphabetically + { + code: ` +class Foo { + constructor() {} + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically', + }, + }, + ], + }, + + // default option + class expression + default order + alphabetically + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // default option + class expression + custom order + alphabetically + { + code: ` +const foo = class Foo { + constructor() {} + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + default: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically', + }, + }, + ], + }, + ], + invalid: [ + // default option + class + wrong order within group and wrong group order + alphabetically + { + code: ` +class FooTestGetter { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + get h() {} + + set g() {} + + constructor() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'constructor', + rank: 'public instance get', + }, + }, + ], + }, + + // default option + class + custom + alphabetically + { + code: ` +class Foo { + @Bar + get a() {} + + get b() {} + + @Bar + set c() {} + + set d() {} +} + `, + options: [ + { + default: { + memberTypes: ['get', 'decorated-get', 'set', 'decorated-set'], + order: 'alphabetically', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'b', + rank: 'decorated get', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'decorated set', + }, + }, + ], + }, + + // default option + class + wrong order within group and wrong group order + alphabetically + { + code: ` +class FooTestGetter { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + set g() {} + + constructor() {} + + get h() {} +} + `, + options: [ + { + default: { + memberTypes: defaultOrder, + order: 'alphabetically', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'constructor', + rank: 'public instance set', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'h', + rank: 'public instance set', + }, + }, + ], + }, + + // default option + interface + wrong order within group and wrong group order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + a : x; + b : x; + c : x; + + c() : void; + b() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + + // default option + type literal + wrong order within group and wrong group order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + a : x; + b : x; + c : x; + + c() : void; + b() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + + // default option + class + wrong order within group and wrong group order + alphabetically + { + code: ` +class Foo { + public static c: string = ""; + public static b: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + + // default option + class expression + wrong order within group and wrong group order + alphabetically + { + code: ` +const foo = class Foo { + public static c: string = ""; + public static b: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + // default option + class + decorators + custom order + wrong order within group and wrong group order + alphabetically + { + code: ` +class Foo { + @Dec() a1: string; + @Dec() + a3: string; + @Dec() + a2: string; + + constructor() {} + + b1: string; + b2: string; + + public c(): void; + @Dec() d(): void +} + `, + options: [ + { + default: { + memberTypes: [ + 'decorated-field', + 'field', + 'constructor', + 'decorated-method', + ], + order: 'alphabetically', + }, + }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'b1', + rank: 'constructor', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'b2', + rank: 'constructor', + }, + }, + ], + }, + ], + }; + +const sortedWithGroupingClassesOption: TSESLint.RunTests = + { + valid: [ + // classes option + interface + alphabetically --> Default order applies + { + code: ` +interface Foo { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ classes: { order: 'alphabetically' } }], + }, + + // classes option + type literal + alphabetically --> Default order applies + { + code: ` +type Foo = { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ classes: { order: 'alphabetically' } }], + }, + + // classes option + class + default order + alphabetically + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { classes: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + }, + + // classes option + class + custom order + alphabetically + { + code: ` +class Foo { + constructor() {} + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + classes: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically', + }, + }, + ], + }, + + // classes option + class expression + alphabetically --> Default order applies + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ classes: { order: 'alphabetically' } }], + }, + ], + invalid: [ + // default option + class + wrong order within group and wrong group order + alphabetically + { + code: ` +class Foo { + public static c: string = ""; + public static b: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + ], + }; + +const sortedWithGroupingClassExpressionsOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // classExpressions option + interface + alphabetically --> Default order applies + { + code: ` +interface Foo { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ classExpressions: { order: 'alphabetically' } }], + }, + + // classExpressions option + type literal + alphabetically --> Default order applies + { + code: ` +type Foo = { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ classExpressions: { order: 'alphabetically' } }], + }, + + // classExpressions option + class + alphabetically --> Default order applies + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ classExpressions: { order: 'alphabetically' } }], + }, + + // classExpressions option + class expression + default order + alphabetically + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [ + { + classExpressions: { + memberTypes: defaultOrder, + order: 'alphabetically', + }, + }, + ], + }, + + // classExpressions option + class expression + custom order + alphabetically + { + code: ` +const foo = class Foo { + constructor() {} + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + public static a: string; + protected static b: string = ""; + private static c: string = ""; +} + `, + options: [ + { + classExpressions: { + memberTypes: ['constructor', 'instance-field', 'static-field'], + order: 'alphabetically', + }, + }, + ], + }, + ], + invalid: [ + // default option + class expression + wrong order within group and wrong group order + alphabetically + { + code: ` +const foo = class Foo { + public static c: string = ""; + public static b: string = ""; + public static a: string; + + constructor() {} + + public d: string = ""; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'd', + rank: 'public constructor', + }, + }, + ], + }, + ], +}; + +const sortedWithGroupingInterfacesOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // interfaces option + interface + default order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + a : x; + b : x; + c : x; + + a() : void; + b() : void; + c() : void; + + new () : Bar; + + () : Baz; +} + `, + options: [ + { + interfaces: { + memberTypes: ['signature', 'field', 'method', 'constructor'], + order: 'alphabetically', + }, + }, + ], + }, + + // interfaces option + interface + custom order + alphabetically + { + code: ` +interface Foo { + new () : Bar; + + a() : void; + b() : void; + c() : void; + + a : x; + b : x; + c : x; + + [a: string] : number; + () : Baz; +} + `, + options: [ + { + interfaces: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically', + }, + }, + ], + }, + + // interfaces option + type literal + alphabetically --> Default order applies + { + code: ` +type Foo = { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ interfaces: { order: 'alphabetically' } }], + }, + + // interfaces option + class + alphabetically --> Default order applies + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ interfaces: { order: 'alphabetically' } }], + }, + + // interfaces option + class expression + alphabetically --> Default order applies + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ interfaces: { order: 'alphabetically' } }], + }, + ], + invalid: [ + // default option + interface + wrong order within group and wrong group order + alphabetically + { + code: ` +interface Foo { + [a: string] : number; + + a : x; + b : x; + c : x; + + c() : void; + b() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + ], +}; + +const sortedWithGroupingTypeLiteralsOption: TSESLint.RunTests< + MessageIds, + Options +> = { + valid: [ + // typeLiterals option + interface + alphabetically --> Default order applies + { + code: ` +interface Foo { + [a: string] : number; + + () : Baz; + + c : x; + b : x; + a : x; + + new () : Bar; + + c() : void; + b() : void; + a() : void; +} + `, + options: [{ typeLiterals: { order: 'alphabetically' } }], + }, + + // typeLiterals option + type literal + default order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + a : x; + b : x; + c : x; + + a() : void; + b() : void; + c() : void; + + new () : Bar; + + () : Baz; +} + `, + options: [ + { + typeLiterals: { + memberTypes: ['signature', 'field', 'method', 'constructor'], + order: 'alphabetically', + }, + }, + ], + }, + + // typeLiterals option + type literal + custom order + alphabetically + { + code: ` +type Foo = { + new () : Bar; + + a() : void; + b() : void; + c() : void; + + a : x; + b : x; + c : x; + + [a: string] : number; + () : Baz; +} + `, + options: [ + { + typeLiterals: { + memberTypes: ['constructor', 'method', 'field'], + order: 'alphabetically', + }, + }, + ], + }, + + // typeLiterals option + class + alphabetically --> Default order applies + { + code: ` +class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ typeLiterals: { order: 'alphabetically' } }], + }, + + // typeLiterals option + class expression + alphabetically --> Default order applies + { + code: ` +const foo = class Foo { + public static a: string; + protected static b: string = ""; + private static c: string = ""; + + public d: string = ""; + protected e: string = ""; + private f: string = ""; + + constructor() {} +} + `, + options: [{ typeLiterals: { order: 'alphabetically' } }], + }, + ], + invalid: [ + // default option + type literal + wrong order within group and wrong group order + alphabetically + { + code: ` +type Foo = { + [a: string] : number; + + a : x; + b : x; + c : x; + + c() : void; + b() : void; + a() : void; + + () : Baz; + + new () : Bar; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectGroupOrder', + data: { + name: 'call', + rank: 'field', + }, + }, + { + messageId: 'incorrectGroupOrder', + data: { + name: 'new', + rank: 'method', + }, + }, + ], + }, + + // default option + private identifiers + { + code: ` +class Foo { + #c = 3; + #b = 2; + #a = 1; +} + `, + options: [ + { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, + ], + errors: [ + { + messageId: 'incorrectOrder', + line: 4, + column: 3, + }, + { + messageId: 'incorrectOrder', + line: 5, + column: 3, + }, + ], + }, + ], +}; + +const sortedWithoutGrouping = { + valid: [ + ...sortedWithoutGroupingDefaultOption.valid, + ...sortedWithoutGroupingClassesOption.valid, + ...sortedWithoutGroupingClassExpressionsOption.valid, + ...sortedWithoutGroupingInterfacesOption.valid, + ...sortedWithoutGroupingTypeLiteralsOption.valid, + ], + invalid: [ + ...sortedWithoutGroupingDefaultOption.invalid, + ...sortedWithoutGroupingClassesOption.invalid, + ...sortedWithoutGroupingClassExpressionsOption.invalid, + ...sortedWithoutGroupingInterfacesOption.invalid, + ...sortedWithoutGroupingTypeLiteralsOption.invalid, + ], +}; + +const sortedWithGrouping = { + valid: [ + ...sortedWithGroupingDefaultOption.valid, + ...sortedWithGroupingClassesOption.valid, + ...sortedWithGroupingClassExpressionsOption.valid, + ...sortedWithGroupingInterfacesOption.valid, + ...sortedWithGroupingTypeLiteralsOption.valid, + ], + invalid: [ + ...sortedWithGroupingDefaultOption.invalid, + ...sortedWithGroupingClassesOption.invalid, + ...sortedWithGroupingClassExpressionsOption.invalid, + ...sortedWithGroupingInterfacesOption.invalid, + ...sortedWithGroupingTypeLiteralsOption.invalid, + ], +}; + +ruleTester.run('member-ordering-alphabetically-order', rule, { + valid: [...sortedWithoutGrouping.valid, ...sortedWithGrouping.valid], + invalid: [...sortedWithoutGrouping.invalid, ...sortedWithGrouping.invalid], +}); diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index 97b05cfdeff..cbab8ced2a3 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -1,8 +1,4 @@ -import rule, { - defaultOrder, - MessageIds, - Options, -} from '../../src/rules/member-ordering'; +import rule, { MessageIds, Options } from '../../src/rules/member-ordering'; import { RuleTester } from '../RuleTester'; import { TSESLint } from '@typescript-eslint/experimental-utils'; @@ -3830,2611 +3826,4 @@ class Foo { ], }; -const sortedWithoutGroupingDefaultOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // default option + interface + multiple types - { - code: ` -interface Foo { - (): Foo; - a(): Foo; - b(): Foo; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + interface + lower/upper case - { - code: ` -interface Foo { - A : b; - a : b; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + interface + numbers - { - code: ` -interface Foo { - a1 : b; - aa : b; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + type literal + multiple types - { - code: ` -type Foo = { - a : b; - [a: string] : number; - b() : void; - () : Baz; - new () : Bar; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + type literal + lower/upper case - { - code: ` -type Foo = { - A : b; - a : b; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + type literal + numbers - { - code: ` -type Foo = { - a1 : b; - aa : b; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class + multiple types - { - code: ` -class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - constructor() {} - @Dec() d: string; - public e : string = ""; - @Dec() f : string = ""; - protected g : string = ""; - private h : string = ""; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class + lower/upper case - { - code: ` -class Foo { - public static A : string; - public static a : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class + numbers - { - code: ` -class Foo { - public static a1 : string; - public static aa : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class expression + multiple types - { - code: ` -const foo = class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class expression + lower/upper case - { - code: ` -const foo = class Foo { - public static A : string; - public static a : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class expression + numbers - { - code: ` -const foo = class Foo { - public static a1 : string; - public static aa : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + class + decorators - { - code: ` -class Foo { - public static a : string; - @Dec() static b : string; - public static c : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // default option + private identifiers - { - code: ` -class Foo { - #a = 1; - #b = 2; - #c = 3; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - ], - invalid: [ - // default option + interface + wrong order - { - code: ` -interface Foo { - b() : void; - a : b; - [a: string] : number; - new () : Bar; - () : Baz; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'call', - beforeMember: 'new', - }, - }, - ], - }, - - // default option + interface + wrong order (multiple) - { - code: ` -interface Foo { - c : string; - b : string; - a : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // default option + type literal + wrong order - { - code: ` -type Foo = { - b() : void; - a : b; - [a: string] : number; - new () : Bar; - () : Baz; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'call', - beforeMember: 'new', - }, - }, - ], - }, - - // default option + type literal + wrong order (multiple) - { - code: ` -type Foo = { - c : string; - b : string; - a : string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // default option + class + wrong order - { - code: ` -class Foo { - protected static b : string = ""; - public static a : string; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // default option + class + wrong order (multiple) - { - code: ` -class Foo { - public static c: string; - public static b: string; - public static a: string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // default option + class expression + wrong order - { - code: ` -const foo = class Foo { - protected static b : string = ""; - public static a : string; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // default option + class expression + wrong order (multiple) - { - code: ` -const foo = class Foo { - public static c: string; - public static b: string; - public static a: string; -} - `, - options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - ], -}; - -const sortedWithoutGroupingClassesOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // classes option + interface + multiple types --> Only member group order is checked (default config) - { - code: ` -interface Foo { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + interface + lower/upper case --> Only member group order is checked (default config) - { - code: ` -interface Foo { - a : b; - A : b; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + interface + numbers --> Only member group order is checked (default config) - { - code: ` -interface Foo { - aa : b; - a1 : b; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + type literal + multiple types --> Only member group order is checked (default config) - { - code: ` -type Foo = { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + type literal + lower/upper case --> Only member group order is checked (default config) - { - code: ` -type Foo = { - a : b; - A : b; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + type literal + numbers --> Only member group order is checked (default config) - { - code: ` -type Foo = { - aa : b; - a1 : b; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class + multiple types - { - code: ` -class Foo { - public static a : string; - protected static b : string = ""; - @Dec() private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - @Dec() - private f : string = ""; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class + lower/upper case - { - code: ` -class Foo { - public static A : string; - public static a : string; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class + numbers - { - code: ` -class Foo { - public static a1 : string; - public static aa : string; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class expression + multiple types --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class expression + lower/upper case --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - public static A : string; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - - // classes option + class expression + numbers --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - }, - ], - invalid: [ - // classes option + class + wrong order - { - code: ` -class Foo { - protected static b : string = ""; - public static a : string; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // classes option + class + wrong order (multiple) - { - code: ` -class Foo { - public static c: string; - public static b: string; - public static a: string; -} - `, - options: [{ classes: { memberTypes: 'never', order: 'alphabetically' } }], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - ], -}; - -const sortedWithoutGroupingClassExpressionsOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // classExpressions option + interface + multiple types --> Only member group order is checked (default config) - { - code: ` -interface Foo { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + interface + lower/upper case --> Only member group order is checked (default config) - { - code: ` -interface Foo { - a : b; - A : b; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + interface + numbers --> Only member group order is checked (default config) - { - code: ` -interface Foo { - aa : b; - a1 : b; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + type literal + multiple types --> Only member group order is checked (default config) - { - code: ` -type Foo = { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + type literal + lower/upper case --> Only member group order is checked (default config) - { - code: ` -type Foo = { - a : b; - A : b; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + type literal + numbers --> Only member group order is checked (default config) - { - code: ` -type Foo = { - aa : b; - a1 : b; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class + multiple types --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class + lower/upper case --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - public static A : string; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class + numbers --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class expression + multiple types - { - code: ` -const foo = class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class expression + lower/upper case - { - code: ` -const foo = class Foo { - public static A : string; - public static a : string; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // classExpressions option + class expression + numbers - { - code: ` -const foo = class Foo { - public static a1 : string; - public static aa : string; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - ], - invalid: [ - // classExpressions option + class expression + wrong order - { - code: ` -const foo = class Foo { - protected static b : string = ""; - public static a : string; - private static c : string = ""; - constructor() {} - public d : string = ""; - protected e : string = ""; - private f : string = ""; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - - // classExpressions option + class expression + wrong order (multiple) - { - code: ` -const foo = class Foo { - public static c: string; - public static b: string; - public static a: string; -} - `, - options: [ - { classExpressions: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - ], -}; - -const sortedWithoutGroupingInterfacesOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // interfaces option + interface + multiple types - { - code: ` -interface Foo { - [a: string] : number; - a : b; - b() : void; - () : Baz; - new () : Bar; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + interface + lower/upper case - { - code: ` -interface Foo { - A : b; - a : b; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + interface + numbers - { - code: ` -interface Foo { - a1 : b; - aa : b; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + type literal + multiple types --> Only member group order is checked (default config) - { - code: ` -type Foo = { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + type literal + lower/upper case --> Only member group order is checked (default config) - { - code: ` -type Foo = { - a : b; - A : b; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + type literal + numbers --> Only member group order is checked (default config) - { - code: ` -type Foo = { - aa : b; - a1 : b; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class + multiple types --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class + lower/upper case --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - public static A : string; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class + numbers --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class expression + multiple types --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class expression + lower/upper case --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - public static A : string; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // interfaces option + class expression + numbers --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - ], - invalid: [ - // interfaces option + interface + wrong order - { - code: ` -interface Foo { - b() : void; - a : b; - [a: string] : number; - new () : Bar; - () : Baz; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'call', - beforeMember: 'new', - }, - }, - ], - }, - - // interfaces option + interface + wrong order (multiple) - { - code: ` -interface Foo { - c : string; - b : string; - a : string; -} - `, - options: [ - { interfaces: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - ], -}; - -const sortedWithoutGroupingTypeLiteralsOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // typeLiterals option + interface + multiple types --> Only member group order is checked (default config) - { - code: ` -interface Foo { - [a: string] : number; - () : Baz; - c : b; - new () : Bar; - b() : void; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + interface + lower/upper case --> Only member group order is checked (default config) - { - code: ` -interface Foo { - a : b; - A : b; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + interface + numbers --> Only member group order is checked (default config) - { - code: ` -interface Foo { - aa : b; - a1 : b; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + type literal + multiple types - { - code: ` -type Foo = { - [a: string] : number; - a : b; - b() : void; - () : Baz; - new () : Bar; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + type literal + lower/upper case - { - code: ` -type Foo = { - A : b; - a : b; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + type literal + numbers - { - code: ` -type Foo = { - a1 : b; - aa : b; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class + multiple types --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class + lower/upper case --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static a : string; - public static A : string; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class + numbers --> Only member group order is checked (default config) - { - code: ` -class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class expression + multiple types --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - protected static b : string = ""; - private static c : string = ""; - public d : string = ""; - protected e : string = ""; - private f : string = ""; - constructor() {} -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class expression + lower/upper case --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static a : string; - public static A : string; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - - // typeLiterals option + class expression + numbers --> Only member group order is checked (default config) - { - code: ` -const foo = class Foo { - public static aa : string; - public static a1 : string; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - }, - ], - invalid: [ - // typeLiterals option + type literal + wrong order - { - code: ` -type Foo = { - b() : void; - a : b; - [a: string] : number; - new () : Bar; - () : Baz; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'call', - beforeMember: 'new', - }, - }, - ], - }, - - // typeLiterals option + type literal + wrong order (multiple) - { - code: ` -type Foo = { - c : string; - b : string; - a : string; -} - `, - options: [ - { typeLiterals: { memberTypes: 'never', order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - data: { - member: 'b', - beforeMember: 'c', - }, - }, - { - messageId: 'incorrectOrder', - data: { - member: 'a', - beforeMember: 'b', - }, - }, - ], - }, - ], -}; - -const sortedWithGroupingDefaultOption: TSESLint.RunTests = - { - valid: [ - // default option + interface + default order + alphabetically - { - code: ` -interface Foo { - [a: string] : number; - - () : Baz; - - a : x; - b : x; - c : x; - - new () : Bar; - - a() : void; - b() : void; - c() : void; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + interface + custom order + alphabetically - { - code: ` -interface Foo { - new () : Bar; - - a() : void; - b() : void; - c() : void; - - a : x; - b : x; - c : x; - - [a: string] : number; - () : Baz; -} - `, - options: [ - { - default: { - memberTypes: ['constructor', 'method', 'field'], - order: 'alphabetically', - }, - }, - ], - }, - - // default option + type literal + default order + alphabetically - { - code: ` -type Foo = { - [a: string] : number; - - () : Baz; - - a : x; - b : x; - c : x; - - new () : Bar; - - a() : void; - b() : void; - c() : void; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + type literal + custom order + alphabetically - { - code: ` -type Foo = { - [a: string] : number; - - new () : Bar; - - a() : void; - b() : void; - c() : void; - - a : x; - b : x; - c : x; - - () : Baz; -} - `, - options: [ - { - default: { - memberTypes: ['constructor', 'method', 'field'], - order: 'alphabetically', - }, - }, - ], - }, - - // default option + class + default order + alphabetically - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + class + defaultOrder + alphabetically - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} - - get h() {} - - set g() {} -} - `, - options: [ - { - default: { - memberTypes: defaultOrder, - order: 'alphabetically', - }, - }, - ], - }, - - // default option + class + custom + alphabetically - { - code: ` -class Foo { - get a() {} - - @Bar - get b() {} - - set c() {} - - @Bar - set d() {} -} - `, - options: [ - { - default: { - memberTypes: ['get', 'decorated-get', 'set', 'decorated-set'], - order: 'alphabetically', - }, - }, - ], - }, - - // default option + class + decorators + default order + alphabetically - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - @Dec() public d: string; - @Dec() protected e: string; - @Dec() private f: string; - - public g: string = ""; - protected h: string = ""; - private i: string = ""; - - constructor() {} -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + class + custom order + alphabetically - { - code: ` -class Foo { - constructor() {} - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - public static a: string; - protected static b: string = ""; - private static c: string = ""; -} - `, - options: [ - { - default: { - memberTypes: ['constructor', 'instance-field', 'static-field'], - order: 'alphabetically', - }, - }, - ], - }, - - // default option + class expression + default order + alphabetically - { - code: ` -const foo = class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // default option + class expression + custom order + alphabetically - { - code: ` -const foo = class Foo { - constructor() {} - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - public static a: string; - protected static b: string = ""; - private static c: string = ""; -} - `, - options: [ - { - default: { - memberTypes: ['constructor', 'instance-field', 'static-field'], - order: 'alphabetically', - }, - }, - ], - }, - ], - invalid: [ - // default option + class + wrong order within group and wrong group order + alphabetically - { - code: ` -class FooTestGetter { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - get h() {} - - set g() {} - - constructor() {} -} - `, - options: [ - { - default: { - memberTypes: defaultOrder, - order: 'alphabetically', - }, - }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'constructor', - rank: 'public instance get', - }, - }, - ], - }, - - // default option + class + custom + alphabetically - { - code: ` -class Foo { - @Bar - get a() {} - - get b() {} - - @Bar - set c() {} - - set d() {} -} - `, - options: [ - { - default: { - memberTypes: ['get', 'decorated-get', 'set', 'decorated-set'], - order: 'alphabetically', - }, - }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'b', - rank: 'decorated get', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'd', - rank: 'decorated set', - }, - }, - ], - }, - - // default option + class + wrong order within group and wrong group order + alphabetically - { - code: ` -class FooTestGetter { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - set g() {} - - constructor() {} - - get h() {} -} - `, - options: [ - { - default: { - memberTypes: defaultOrder, - order: 'alphabetically', - }, - }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'constructor', - rank: 'public instance set', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'h', - rank: 'public instance set', - }, - }, - ], - }, - - // default option + interface + wrong order within group and wrong group order + alphabetically - { - code: ` -interface Foo { - [a: string] : number; - - a : x; - b : x; - c : x; - - c() : void; - b() : void; - a() : void; - - () : Baz; - - new () : Bar; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'call', - rank: 'field', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'new', - rank: 'method', - }, - }, - ], - }, - - // default option + type literal + wrong order within group and wrong group order + alphabetically - { - code: ` -type Foo = { - [a: string] : number; - - a : x; - b : x; - c : x; - - c() : void; - b() : void; - a() : void; - - () : Baz; - - new () : Bar; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'call', - rank: 'field', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'new', - rank: 'method', - }, - }, - ], - }, - - // default option + class + wrong order within group and wrong group order + alphabetically - { - code: ` -class Foo { - public static c: string = ""; - public static b: string = ""; - public static a: string; - - constructor() {} - - public d: string = ""; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'd', - rank: 'public constructor', - }, - }, - ], - }, - - // default option + class expression + wrong order within group and wrong group order + alphabetically - { - code: ` -const foo = class Foo { - public static c: string = ""; - public static b: string = ""; - public static a: string; - - constructor() {} - - public d: string = ""; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'd', - rank: 'public constructor', - }, - }, - ], - }, - // default option + class + decorators + custom order + wrong order within group and wrong group order + alphabetically - { - code: ` -class Foo { - @Dec() a1: string; - @Dec() - a3: string; - @Dec() - a2: string; - - constructor() {} - - b1: string; - b2: string; - - public c(): void; - @Dec() d(): void -} - `, - options: [ - { - default: { - memberTypes: [ - 'decorated-field', - 'field', - 'constructor', - 'decorated-method', - ], - order: 'alphabetically', - }, - }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'b1', - rank: 'constructor', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'b2', - rank: 'constructor', - }, - }, - ], - }, - ], - }; - -const sortedWithGroupingClassesOption: TSESLint.RunTests = - { - valid: [ - // classes option + interface + alphabetically --> Default order applies - { - code: ` -interface Foo { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ classes: { order: 'alphabetically' } }], - }, - - // classes option + type literal + alphabetically --> Default order applies - { - code: ` -type Foo = { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ classes: { order: 'alphabetically' } }], - }, - - // classes option + class + default order + alphabetically - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [ - { classes: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - }, - - // classes option + class + custom order + alphabetically - { - code: ` -class Foo { - constructor() {} - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - public static a: string; - protected static b: string = ""; - private static c: string = ""; -} - `, - options: [ - { - classes: { - memberTypes: ['constructor', 'instance-field', 'static-field'], - order: 'alphabetically', - }, - }, - ], - }, - - // classes option + class expression + alphabetically --> Default order applies - { - code: ` -const foo = class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ classes: { order: 'alphabetically' } }], - }, - ], - invalid: [ - // default option + class + wrong order within group and wrong group order + alphabetically - { - code: ` -class Foo { - public static c: string = ""; - public static b: string = ""; - public static a: string; - - constructor() {} - - public d: string = ""; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'd', - rank: 'public constructor', - }, - }, - ], - }, - ], - }; - -const sortedWithGroupingClassExpressionsOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // classExpressions option + interface + alphabetically --> Default order applies - { - code: ` -interface Foo { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ classExpressions: { order: 'alphabetically' } }], - }, - - // classExpressions option + type literal + alphabetically --> Default order applies - { - code: ` -type Foo = { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ classExpressions: { order: 'alphabetically' } }], - }, - - // classExpressions option + class + alphabetically --> Default order applies - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ classExpressions: { order: 'alphabetically' } }], - }, - - // classExpressions option + class expression + default order + alphabetically - { - code: ` -const foo = class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [ - { - classExpressions: { - memberTypes: defaultOrder, - order: 'alphabetically', - }, - }, - ], - }, - - // classExpressions option + class expression + custom order + alphabetically - { - code: ` -const foo = class Foo { - constructor() {} - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - public static a: string; - protected static b: string = ""; - private static c: string = ""; -} - `, - options: [ - { - classExpressions: { - memberTypes: ['constructor', 'instance-field', 'static-field'], - order: 'alphabetically', - }, - }, - ], - }, - ], - invalid: [ - // default option + class expression + wrong order within group and wrong group order + alphabetically - { - code: ` -const foo = class Foo { - public static c: string = ""; - public static b: string = ""; - public static a: string; - - constructor() {} - - public d: string = ""; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'd', - rank: 'public constructor', - }, - }, - ], - }, - ], -}; - -const sortedWithGroupingInterfacesOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // interfaces option + interface + default order + alphabetically - { - code: ` -interface Foo { - [a: string] : number; - - a : x; - b : x; - c : x; - - a() : void; - b() : void; - c() : void; - - new () : Bar; - - () : Baz; -} - `, - options: [ - { - interfaces: { - memberTypes: ['signature', 'field', 'method', 'constructor'], - order: 'alphabetically', - }, - }, - ], - }, - - // interfaces option + interface + custom order + alphabetically - { - code: ` -interface Foo { - new () : Bar; - - a() : void; - b() : void; - c() : void; - - a : x; - b : x; - c : x; - - [a: string] : number; - () : Baz; -} - `, - options: [ - { - interfaces: { - memberTypes: ['constructor', 'method', 'field'], - order: 'alphabetically', - }, - }, - ], - }, - - // interfaces option + type literal + alphabetically --> Default order applies - { - code: ` -type Foo = { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ interfaces: { order: 'alphabetically' } }], - }, - - // interfaces option + class + alphabetically --> Default order applies - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ interfaces: { order: 'alphabetically' } }], - }, - - // interfaces option + class expression + alphabetically --> Default order applies - { - code: ` -const foo = class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ interfaces: { order: 'alphabetically' } }], - }, - ], - invalid: [ - // default option + interface + wrong order within group and wrong group order + alphabetically - { - code: ` -interface Foo { - [a: string] : number; - - a : x; - b : x; - c : x; - - c() : void; - b() : void; - a() : void; - - () : Baz; - - new () : Bar; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'call', - rank: 'field', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'new', - rank: 'method', - }, - }, - ], - }, - ], -}; - -const sortedWithGroupingTypeLiteralsOption: TSESLint.RunTests< - MessageIds, - Options -> = { - valid: [ - // typeLiterals option + interface + alphabetically --> Default order applies - { - code: ` -interface Foo { - [a: string] : number; - - () : Baz; - - c : x; - b : x; - a : x; - - new () : Bar; - - c() : void; - b() : void; - a() : void; -} - `, - options: [{ typeLiterals: { order: 'alphabetically' } }], - }, - - // typeLiterals option + type literal + default order + alphabetically - { - code: ` -type Foo = { - [a: string] : number; - - a : x; - b : x; - c : x; - - a() : void; - b() : void; - c() : void; - - new () : Bar; - - () : Baz; -} - `, - options: [ - { - typeLiterals: { - memberTypes: ['signature', 'field', 'method', 'constructor'], - order: 'alphabetically', - }, - }, - ], - }, - - // typeLiterals option + type literal + custom order + alphabetically - { - code: ` -type Foo = { - new () : Bar; - - a() : void; - b() : void; - c() : void; - - a : x; - b : x; - c : x; - - [a: string] : number; - () : Baz; -} - `, - options: [ - { - typeLiterals: { - memberTypes: ['constructor', 'method', 'field'], - order: 'alphabetically', - }, - }, - ], - }, - - // typeLiterals option + class + alphabetically --> Default order applies - { - code: ` -class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ typeLiterals: { order: 'alphabetically' } }], - }, - - // typeLiterals option + class expression + alphabetically --> Default order applies - { - code: ` -const foo = class Foo { - public static a: string; - protected static b: string = ""; - private static c: string = ""; - - public d: string = ""; - protected e: string = ""; - private f: string = ""; - - constructor() {} -} - `, - options: [{ typeLiterals: { order: 'alphabetically' } }], - }, - ], - invalid: [ - // default option + type literal + wrong order within group and wrong group order + alphabetically - { - code: ` -type Foo = { - [a: string] : number; - - a : x; - b : x; - c : x; - - c() : void; - b() : void; - a() : void; - - () : Baz; - - new () : Bar; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectGroupOrder', - data: { - name: 'call', - rank: 'field', - }, - }, - { - messageId: 'incorrectGroupOrder', - data: { - name: 'new', - rank: 'method', - }, - }, - ], - }, - - // default option + private identifiers - { - code: ` -class Foo { - #c = 3; - #b = 2; - #a = 1; -} - `, - options: [ - { default: { memberTypes: defaultOrder, order: 'alphabetically' } }, - ], - errors: [ - { - messageId: 'incorrectOrder', - line: 4, - column: 3, - }, - { - messageId: 'incorrectOrder', - line: 5, - column: 3, - }, - ], - }, - ], -}; - -const sortedWithoutGrouping = { - valid: [ - ...sortedWithoutGroupingDefaultOption.valid, - ...sortedWithoutGroupingClassesOption.valid, - ...sortedWithoutGroupingClassExpressionsOption.valid, - ...sortedWithoutGroupingInterfacesOption.valid, - ...sortedWithoutGroupingTypeLiteralsOption.valid, - ], - invalid: [ - ...sortedWithoutGroupingDefaultOption.invalid, - ...sortedWithoutGroupingClassesOption.invalid, - ...sortedWithoutGroupingClassExpressionsOption.invalid, - ...sortedWithoutGroupingInterfacesOption.invalid, - ...sortedWithoutGroupingTypeLiteralsOption.invalid, - ], -}; - -const sortedWithGrouping = { - valid: [ - ...sortedWithGroupingDefaultOption.valid, - ...sortedWithGroupingClassesOption.valid, - ...sortedWithGroupingClassExpressionsOption.valid, - ...sortedWithGroupingInterfacesOption.valid, - ...sortedWithGroupingTypeLiteralsOption.valid, - ], - invalid: [ - ...sortedWithGroupingDefaultOption.invalid, - ...sortedWithGroupingClassesOption.invalid, - ...sortedWithGroupingClassExpressionsOption.invalid, - ...sortedWithGroupingInterfacesOption.invalid, - ...sortedWithGroupingTypeLiteralsOption.invalid, - ], -}; - -ruleTester.run('member-ordering', rule, { - valid: [ - ...grouped.valid, - ...sortedWithoutGrouping.valid, - ...sortedWithGrouping.valid, - ], - invalid: [ - ...grouped.invalid, - ...sortedWithoutGrouping.invalid, - ...sortedWithGrouping.invalid, - ], -}); +ruleTester.run('member-ordering', rule, grouped);