8000 feat(eslint-plugin): [sort-type-constituents] support case sensitive sorting by jsfm01 · Pull Request #8760 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

feat(eslint-plugin): [sort-type-constituents] support case sensitive sorting #8760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/eslint-plugin/docs/rules/sort-type-constituents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ type T4 =

## Options

### `caseSensitive`

Whether to sort using case sensitive string comparisons.

Examples of code with `{ "caseSensitive": true }`:

<Tabs>
<TabItem value="❌ Incorrect">

```ts option='{ "caseSensitive": true }'
type T = 'DeletedAt' | 'DeleteForever';
```

</TabItem>
<TabItem value="✅ Correct">

```ts option='{ "caseSensitive": true }'
type T = 'DeleteForever' | 'DeletedAt';
```

</TabItem>
</Tabs>

### `checkIntersections`

Whether to check intersection types (`&`).
Expand Down
24 changes: 23 additions & 1 deletion packages/eslint-plugin/src/rules/sort-type-constituents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,20 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function caseSensitiveSort(a: string, b: string): number {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
}

export type Options = [
{
checkIntersections?: boolean;
checkUnions?: boolean;
caseSensitive?: boolean;
groupOrder?: string[];
},
];
Expand Down Expand Up @@ -132,6 +142,10 @@ export default createRule<Options, MessageIds>({
description: 'Whether to check union types.',
type: 'boolean',
},
caseSensitive: {
description: 'Whether to sort using case sensitive sorting.',
type: 'boolean',
},
groupOrder: {
description: 'Ordering of the groups.',
type: 'array',
Expand All @@ -148,6 +162,7 @@ export default createRule<Options, MessageIds>({
{
checkIntersections: true,
checkUnions: true,
caseSensitive: false,
groupOrder: [
Group.named,
Group.keyword,
Expand All @@ -164,7 +179,10 @@ export default createRule<Options, MessageIds>({
],
},
],
create(context, [{ checkIntersections, checkUnions, groupOrder }]) {
create(
context,
[{ checkIntersections, checkUnions, caseSensitive, groupOrder }],
) {
const collator = new Intl.Collator('en', {
sensitivity: 'base',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-actionable] I tried switching this to 'accent', 'case', and 'variant' instead of the manual caseSensitiveSort function. Nothing passed all the tests. 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the sensitive case is true, it follows the Unicode code points, no matter the collator sensitivity. If the collator sensitivity is set to 'case', for instance, the sorting would still be case-sensitive, but diacritics and accents would be treated as insignificant. The “accent”, doesn’t distinguish the capital letters, for instance, a = A. I believe we should keep as ‘base’.

numeric: true,
Expand All @@ -186,6 +204,10 @@ export default createRule<Options, MessageIds>({
return a.group - b.group;
}

if (caseSensitive) {
return caseSensitiveSort(a.text, b.text);
}

return (
collator.compare(a.text, b.text) ||
(a.text < b.text ? -1 : a.text > b.text ? 1 : 0)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ ruleTester.run('sort-type-constituents', rule, {
},
],
},
{
code: "type T = 'DeleteForever' | 'DeletedAt';",
options: [
{
caseSensitive: true,
},
],
},
{
code: 'type T = { A: string } | { B: string } | { a: string };',
options: [
{
caseSensitive: true,
},
],
},

...valid('&'),
{
Expand All @@ -337,6 +353,14 @@ ruleTester.run('sort-type-constituents', rule, {
},
],
},
{
code: "type T = 'DeleteForever' & 'DeleteForever';",
options: [
{
caseSensitive: true,
},
],
},

{
code: noFormat`
Expand Down Expand Up @@ -390,5 +414,41 @@ type T = 1 | string | {} | A;
},
],
},
{
code: "type T = 'DeletedAt' | 'DeleteForever';",
output: "type T = 'DeleteForever' | 'DeletedAt';",
errors: [
{
messageId: 'notSortedNamed',
data: {
type: 'Union',
name: 'T',
},
},
],
options: [
{
caseSensitive: true,
},
],
},
{
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,
},
],
},
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0