From f489bd6be1a3a75f6411a9b2a72b13d5d3ffe155 Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Fri, 22 Mar 2024 21:38:59 +0000 Subject: [PATCH 01/10] Adding caseSensitiveSorting option --- .../docs/rules/sort-type-constituents.mdx | 2 ++ .../src/rules/sort-type-constituents.ts | 25 ++++++++++++++++++- .../sort-type-constituents.shot | 6 +++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index 011fcf6d6a02..cc510b13e6f2 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -88,6 +88,8 @@ type T4 = ## Options +### `caseSensitiveSorting` + ### `checkIntersections` Whether to check intersection types (`&`). diff --git a/packages/eslint-plugin/src/rules/sort-type-constituents.ts b/packages/eslint-plugin/src/rules/sort-type-constituents.ts index 909af8c59bf0..2e2ccbd00bd9 100644 --- a/packages/eslint-plugin/src/rules/sort-type-constituents.ts +++ b/packages/eslint-plugin/src/rules/sort-type-constituents.ts @@ -95,10 +95,21 @@ function getGroup(node: TSESTree.TypeNode): Group { } } +function caseSensitiveSort(a: string, b: string): number { + if (a < b) { + return -1; + } else if (a > b) { + return 1; + } else { + return 0; + } +} + export type Options = [ { checkIntersections?: boolean; checkUnions?: boolean; + caseSensitiveSorting?: boolean; groupOrder?: string[]; }, ]; @@ -132,6 +143,10 @@ export default createRule({ description: 'Whether to check union types.', type: 'boolean', }, + caseSensitiveSorting: { + description: 'Whether to sort using case sensitive sorting.', + type: 'boolean', + }, groupOrder: { description: 'Ordering of the groups.', type: 'array', @@ -148,6 +163,7 @@ export default createRule({ { checkIntersections: true, checkUnions: true, + caseSensitiveSorting: false, groupOrder: [ Group.named, Group.keyword, @@ -164,7 +180,10 @@ export default createRule({ ], }, ], - create(context, [{ checkIntersections, checkUnions, groupOrder }]) { + create( + context, + [{ checkIntersections, checkUnions, caseSensitiveSorting, groupOrder }], + ) { const collator = new Intl.Collator('en', { sensitivity: 'base', numeric: true, @@ -186,6 +205,10 @@ export default createRule({ return a.group - b.group; } + if (caseSensitiveSorting) { + return caseSensitiveSort(a.text, b.text); + } + return ( collator.compare(a.text, b.text) || (a.text < b.text ? -1 : a.text > b.text ? 1 : 0) diff --git a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot index 3384c8b4d50e..dbbcbf2b633e 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot @@ -16,6 +16,10 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "description": "Whether to check union types.", "type": "boolean" }, + "caseSensitiveSorting": { + "description": "Whether to sort using case sensitive.", + "type": "boolean" + }, "groupOrder": { "description": "Ordering of the groups.", "items": { @@ -51,6 +55,8 @@ type Options = [ checkIntersections?: boolean; /** Whether to check union types. */ checkUnions?: boolean; + /** Whether to sort using case sensitive. */ + caseSensitiveSorting?: boolean; /** Ordering of the groups. */ groupOrder?: ( | 'conditional' From 1d2562cc7e020e526709291ad7db39db627973a9 Mon Sep 17 00:00:00 2001 From: Marta Cardoso Date: Sat, 23 Mar 2024 22:16:36 +0000 Subject: [PATCH 02/10] fix snapshot sort-type-constituents.shot --- .../docs/rules/sort-type-constituents.mdx | 2 ++ .../schema-snapshots/sort-type-constituents.shot | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index cc510b13e6f2..f220d425e9fc 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -90,6 +90,8 @@ type T4 = ### `caseSensitiveSorting` +Whether to sort using case sensitive. + ### `checkIntersections` Whether to check intersection types (`&`). diff --git a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot index dbbcbf2b633e..30aa695a9864 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot @@ -8,6 +8,10 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos { "additionalProperties": false, "properties": { + "caseSensitiveSorting": { + "description": "Whether to sort using case sensitive sorting.", + "type": "boolean" + }, "checkIntersections": { "description": "Whether to check intersection types.", "type": "boolean" @@ -16,10 +20,6 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "description": "Whether to check union types.", "type": "boolean" }, - "caseSensitiveSorting": { - "description": "Whether to sort using case sensitive.", - "type": "boolean" - }, "groupOrder": { "description": "Ordering of the groups.", "items": { @@ -51,12 +51,12 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos type Options = [ { + /** Whether to sort using case sensitive sorting. */ + caseSensitiveSorting?: boolean; /** Whether to check intersection types. */ checkIntersections?: boolean; /** Whether to check union types. */ checkUnions?: boolean; - /** Whether to sort using case sensitive. */ - caseSensitiveSorting?: boolean; /** Ordering of the groups. */ groupOrder?: ( | 'conditional' From fbd438f57920a6263370a5425f2e852bf082b3a3 Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Sun, 24 Mar 2024 11:50:31 +0000 Subject: [PATCH 03/10] Add tests and fix lint. --- .../eslint-plugin/src/rules/sort-type-constituents.ts | 3 +-- .../tests/rules/sort-type-constituents.test.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/sort-type-constituents.ts b/packages/eslint-plugin/src/rules/sort-type-constituents.ts index 2e2ccbd00bd9..61dd6eed7437 100644 --- a/packages/eslint-plugin/src/rules/sort-type-constituents.ts +++ b/packages/eslint-plugin/src/rules/sort-type-constituents.ts @@ -100,9 +100,8 @@ function caseSensitiveSort(a: string, b: string): number { return -1; } else if (a > b) { return 1; - } else { - return 0; } + return 0; } export type Options = [ diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index e1a2afc38288..5bb35fc684b3 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -327,6 +327,14 @@ ruleTester.run('sort-type-constituents', rule, { }, ], }, + { + code: "type T = 'DeleteForever' | 'DeletedAt';", + options: [ + { + caseSensitiveSorting: true, + }, + ], + }, ...valid('&'), { From 5e89536bcdc2eb1d767fae2e71367bbd157f329c Mon Sep 17 00:00:00 2001 From: Marta Cardoso Date: Sun, 31 Mar 2024 19:36:32 +0100 Subject: [PATCH 04/10] add examples of the use of case-sensitive sorting web page --- .../docs/rules/sort-type-constituents.mdx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index f220d425e9fc..4831e7b15efe 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -92,6 +92,25 @@ type T4 = Whether to sort using case sensitive. +Examples of code with `{ "caseSensitiveSorting": true }` (the default): + + + + +```ts option='{ "caseSensitiveSorting": true }' +type T = 'DeletedAt' | 'DeleteForever'; +``` + + + + +```ts option='{ "caseSensitiveSorting": true }' +type T = 'DeleteForever' | 'DeletedAt'; +``` + + + + ### `checkIntersections` Whether to check intersection types (`&`). From 96ffc92614ad780b9cea34edbcbb0d8d29c4ec27 Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Mon, 1 Apr 2024 00:16:10 +0100 Subject: [PATCH 05/10] Add tests --- .../docs/rules/sort-type-constituents.mdx | 2 +- .../rules/sort-type-constituents.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index 4831e7b15efe..d1d9540c86c7 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -92,7 +92,7 @@ type T4 = Whether to sort using case sensitive. -Examples of code with `{ "caseSensitiveSorting": true }` (the default): +Examples of code with `{ "caseSensitiveSorting": true }`: diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index 5bb35fc684b3..7c019d653eb4 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -345,6 +345,14 @@ ruleTester.run('sort-type-constituents', rule, { }, ], }, + { + code: "type T = 'DeleteForever' & 'DeletedForever';", + options: [ + { + caseSensitiveSorting: true, + }, + ], + }, { code: noFormat` @@ -398,5 +406,23 @@ type T = 1 | string | {} | A; }, ], }, + { + code: "type T = 'DeletedAt' | 'DeleteForever';", + output: "type T = 'DeleteForever' | 'DeletedAt';", + errors: [ + { + messageId: 'notSortedNamed', + data: { + type: 'Union', + name: 'T', + }, + }, + ], + options: [ + { + caseSensitiveSorting: true, + }, + ], + }, ], }); From 5cb3848ac55aa925639b6f1b977d4221b036ddef Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Mon, 1 Apr 2024 00:23:41 +0100 Subject: [PATCH 06/10] Fix test --- .../eslint-plugin/tests/rules/sort-type-constituents.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index 7c019d653eb4..f3c71ba47451 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -346,7 +346,7 @@ ruleTester.run('sort-type-constituents', rule, { ], }, { - code: "type T = 'DeleteForever' & 'DeletedForever';", + code: "type T = 'DeleteForever' & 'DeleteForever';", options: [ { caseSensitiveSorting: true, From ed54004c40b366642132b377601f4c3e50373cc8 Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Thu, 11 Apr 2024 19:45:17 +0100 Subject: [PATCH 07/10] Changing Naming --- .../docs/rules/sort-type-constituents.mdx | 8 ++++---- .../eslint-plugin/src/rules/sort-type-constituents.ts | 10 +++++----- .../tests/rules/sort-type-constituents.test.ts | 6 +++--- .../tests/schema-snapshots/sort-type-constituents.shot | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index d1d9540c86c7..9ce9a1c8bad1 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -88,23 +88,23 @@ type T4 = ## Options -### `caseSensitiveSorting` +### `caseSensitive` Whether to sort using case sensitive. -Examples of code with `{ "caseSensitiveSorting": true }`: +Examples of code with `{ "caseSensitive": true }`: -```ts option='{ "caseSensitiveSorting": true }' +```ts option='{ "caseSensitive": true }' type T = 'DeletedAt' | 'DeleteForever'; ``` -```ts option='{ "caseSensitiveSorting": true }' +```ts option='{ "caseSensitive": true }' type T = 'DeleteForever' | 'DeletedAt'; ``` diff --git a/packages/eslint-plugin/src/rules/sort-type-constituents.ts b/packages/eslint-plugin/src/rules/sort-type-constituents.ts index 61dd6eed7437..2f1b8f5cbd34 100644 --- a/packages/eslint-plugin/src/rules/sort-type-constituents.ts +++ b/packages/eslint-plugin/src/rules/sort-type-constituents.ts @@ -108,7 +108,7 @@ export type Options = [ { checkIntersections?: boolean; checkUnions?: boolean; - caseSensitiveSorting?: boolean; + caseSensitive?: boolean; groupOrder?: string[]; }, ]; @@ -142,7 +142,7 @@ export default createRule({ description: 'Whether to check union types.', type: 'boolean', }, - caseSensitiveSorting: { + caseSensitive: { description: 'Whether to sort using case sensitive sorting.', type: 'boolean', }, @@ -162,7 +162,7 @@ export default createRule({ { checkIntersections: true, checkUnions: true, - caseSensitiveSorting: false, + caseSensitive: false, groupOrder: [ Group.named, Group.keyword, @@ -181,7 +181,7 @@ export default createRule({ ], create( context, - [{ checkIntersections, checkUnions, caseSensitiveSorting, groupOrder }], + [{ checkIntersections, checkUnions, caseSensitive, groupOrder }], ) { const collator = new Intl.Collator('en', { sensitivity: 'base', @@ -204,7 +204,7 @@ export default createRule({ return a.group - b.group; } - if (caseSensitiveSorting) { + if (caseSensitive) { return caseSensitiveSort(a.text, b.text); } diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index f3c71ba47451..301c98ef5bce 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -331,7 +331,7 @@ ruleTester.run('sort-type-constituents', rule, { code: "type T = 'DeleteForever' | 'DeletedAt';", options: [ { - caseSensitiveSorting: true, + caseSensitive: true, }, ], }, @@ -349,7 +349,7 @@ ruleTester.run('sort-type-constituents', rule, { code: "type T = 'DeleteForever' & 'DeleteForever';", options: [ { - caseSensitiveSorting: true, + caseSensitive: true, }, ], }, @@ -420,7 +420,7 @@ type T = 1 | string | {} | A; ], options: [ { - caseSensitiveSorting: true, + caseSensitive: true, }, ], }, diff --git a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot index 30aa695a9864..2b5fe0203a1a 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/sort-type-constituents.shot @@ -8,7 +8,7 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos { "additionalProperties": false, "properties": { - "caseSensitiveSorting": { + "caseSensitive": { "description": "Whether to sort using case sensitive sorting.", "type": "boolean" }, @@ -52,7 +52,7 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos type Options = [ { /** Whether to sort using case sensitive sorting. */ - caseSensitiveSorting?: boolean; + caseSensitive?: boolean; /** Whether to check intersection types. */ checkIntersections?: boolean; /** Whether to check union types. */ From c155671151ba3fcb15eccafff6d087979adcdd45 Mon Sep 17 00:00:00 2001 From: jsfm01 Date: Thu, 11 Apr 2024 23:06:58 +0100 Subject: [PATCH 08/10] Add more tests --- .../rules/sort-type-constituents.test.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index 301c98ef5bce..c020f5a18d39 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -335,6 +335,14 @@ ruleTester.run('sort-type-constituents', rule, { }, ], }, + { + code: 'type T = { A: string } | { B: string } | { a: string };', + options: [ + { + caseSensitive: true, + }, + ], + }, ...valid('&'), { @@ -424,5 +432,23 @@ type T = 1 | string | {} | A; }, ], }, + { + code: 'type T = { a: string } | { A: string } | { B: string };', + output: 'type T = { A: string } | { B: string } | { a: string };', + errors: [ + { + messageId: 'notSortedNamed', + data: { + type: 'Union', + name: 'T', + }, + }, + ], + options: [ + { + caseSensitive: true, + }, + ], + }, ], }); From 7bea7e4807822703f686a39bb64f2800c15a603b Mon Sep 17 00:00:00 2001 From: Marta Cardoso Date: Sat, 20 Apr 2024 17:14:45 +0100 Subject: [PATCH 09/10] update snapshot --- .../sort-type-constituents.shot | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/sort-type-constituents.shot b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/sort-type-constituents.shot index 5f814b118459..6cb7d94ae0d1 100644 --- a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/sort-type-constituents.shot +++ b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/sort-type-constituents.shot @@ -85,6 +85,23 @@ type T4 = exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 3`] = ` "Incorrect +Options: { "caseSensitive": true } + +type T = 'DeletedAt' | 'DeleteForever'; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Union type T constituents must be sorted. +" +`; + +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 4`] = ` +"Correct +Options: { "caseSensitive": true } + +type T = 'DeleteForever' | 'DeletedAt'; +" +`; + +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 5`] = ` +"Incorrect Options: { "checkIntersections": true } type ExampleIntersection = B & A; @@ -92,7 +109,7 @@ type ExampleIntersection = B & A; " `; -exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 4`] = ` +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 6`] = ` "Correct Options: { "checkIntersections": true } @@ -100,7 +117,7 @@ type ExampleIntersection = A & B; " `; -exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 5`] = ` +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 7`] = ` "Incorrect Options: { "checkUnions": true } @@ -109,7 +126,7 @@ type ExampleUnion = B | A; " `; -exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 6`] = ` +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 8`] = ` "Correct Options: { "checkUnions": true } @@ -117,7 +134,7 @@ type ExampleUnion = A | B; " `; -exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 7`] = ` +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 9`] = ` "Incorrect Options: { "groupOrder": ["literal", "nullish" ]} @@ -126,7 +143,7 @@ type ExampleGroup = null | 123; " `; -exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 8`] = ` +exports[`Validating rule docs sort-type-constituents.mdx code examples ESLint output 10`] = ` "Correct Options: { "groupOrder": ["literal", "nullish" ]} From ba019c5e28063c26e650352c60188fe266350578 Mon Sep 17 00:00:00 2001 From: jsfm01 <145563295+jsfm01@users.noreply.github.com> Date: Thu, 25 Apr 2024 21:14:24 +0100 Subject: [PATCH 10/10] Update packages/eslint-plugin/docs/rules/sort-type-constituents.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Josh Goldberg ✨ --- packages/eslint-plugin/docs/rules/sort-type-constituents.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx index 1e43a09c55e0..a4e29c2cdc43 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx +++ b/packages/eslint-plugin/docs/rules/sort-type-constituents.mdx @@ -99,7 +99,7 @@ type T4 = ### `caseSensitive` -Whether to sort using case sensitive. +Whether to sort using case sensitive string comparisons. Examples of code with `{ "caseSensitive": true }`: