Skip to content

Commit

Permalink
feat: improve groups and custom groups in sort-interfaces and sort-ob…
Browse files Browse the repository at this point in the history
…ject-types
  • Loading branch information
hugop95 authored Dec 2, 2024
1 parent 82cb68a commit 8407bae
Show file tree
Hide file tree
Showing 19 changed files with 2,626 additions and 947 deletions.
67 changes: 32 additions & 35 deletions docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ The `private` modifier will currently match any of the following:
##### Scope of the `public` modifier
Elements that are not `protected` nor `private` will be matched with the `public` modifier, even if the keyword is not present.


##### The `unknown` group
Members that don’t fit into any group specified in the `groups` option will be placed in the `unknown` group. If the `unknown` group is not specified in the `groups` option,
the members will remain in their original order.
Expand Down Expand Up @@ -651,40 +650,38 @@ Example:
'unknown',
],
+ customGroups: [ // [!code ++]
+ [ // [!code ++]
+ { // [!code ++]
+ // `constructor()` members must not match // [!code ++]
+ // `unsorted-methods-and-other-properties` // [!code ++]
+ // so make them match this first // [!code ++]
+ groupName: 'constructor', // [!code ++]
+ selector: 'constructor', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'input-properties', // [!code ++]
+ selector: 'property', // [!code ++]
+ modifiers: ['decorated'], // [!code ++]
+ decoratorNamePattern: 'Input', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'output-properties', // [!code ++]
+ selector: 'property', // [!code ++]
+ modifiers: ['decorated'], // [!code ++]
+ decoratorNamePattern: 'Output', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'unsorted-methods-and-other-properties', // [!code ++]
+ type: 'unsorted', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'method', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'property', // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ ] // [!code ++]
+ { // [!code ++]
+ // `constructor()` members must not match // [!code ++]
+ // `unsorted-methods-and-other-properties` // [!code ++]
+ // so make them match this first // [!code ++]
+ groupName: 'constructor', // [!code ++]
+ selector: 'constructor', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'input-properties', // [!code ++]
+ selector: 'property', // [!code ++]
+ modifiers: ['decorated'], // [!code ++]
+ decoratorNamePattern: 'Input', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'output-properties', // [!code ++]
+ selector: 'property', // [!code ++]
+ modifiers: ['decorated'], // [!code ++]
+ decoratorNamePattern: 'Output', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'unsorted-methods-and-other-properties', // [!code ++]
+ type: 'unsorted', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'method', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'property', // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
}
```

Expand Down
136 changes: 118 additions & 18 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,12 @@ Specifies how new lines should be handled between interface groups.

This options is only applicable when `partitionByNewLine` is `false`.

### groupKind
### [DEPRECATED] groupKind

<sub>default: `'mixed'`</sub>

Use the [groups](#groups) option with the `optional` and `required` modifiers instead.

Specifies how optional and required members should be ordered in TypeScript interfaces.

- `'optional-first'` — Put all optional members before required members.
Expand All @@ -254,14 +256,6 @@ Specifies how optional and required members should be ordered in TypeScript inte

Allows you to specify a list of interface member groups for sorting. Groups help organize members into categories, making your interfaces more readable and maintainable.

Predefined groups:

- `'multiline'` — Members with multiline definitions, such as methods or properties with complex types.
- `'method'` - Members that are methods.
- `'unknown'` — Interface members that don’t fit into any group specified in the `groups` option.

If the `unknown` group is not specified in the `groups` option, it will automatically be added to the end of the list.

Each interface member will be assigned a single group specified in the `groups` option (or the `unknown` group if no match is found).
The order of items in the `groups` option determines how groups are ordered.

Expand All @@ -270,6 +264,8 @@ Within a given group, members will be sorted according to the `type`, `order`, `
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter.
All members of the groups in the array will be sorted together as if they were part of a single group.

Predefined groups are characterized by a single selector and potentially multiple modifiers. You may enter modifiers in any order, but the selector must always come at the end.

#### Example

```ts
Expand Down Expand Up @@ -298,10 +294,106 @@ interface User {
}
```

#### Index-signatures

- Selectors: `index-signature`, `member`.
- Modifiers: `required`, `optional`, `multiline`.
- Example: `optional-index-signature`, `index-signature`, `member`.

#### Methods

- Selectors: `method`, `member`.
- Modifiers: `required`, `optional`, `multiline`.
- Example: `optional-multiline-method`, `method`, `member`.

#### Properties

- Selectors: `property`, `member`.
- Modifiers: `required`, `optional`, `multiline`.
- Example: `optional-property`, `property`, `member`.

##### Scope of the `required` modifier

Elements that are not `optional` will be matched with the `required` modifier, even if the keyword is not present.

##### The `unknown` group

Members that don’t fit into any group specified in the `groups` option will be placed in the `unknown` group. If the `unknown` group is not specified in the `groups` option,
it will automatically be added to the end of the list.

##### Behavior when multiple groups match an element

The lists of modifiers above are sorted by importance, from most to least important.
In case of multiple groups matching an element, the following rules will be applied:

1. The group with the most modifiers matching will be selected.
2. If modifiers quantity is the same, order will be chosen based on modifier importance as listed above.

Example :

```ts
interface Test {
optionalMethod?: () => {
property: string;
}
}
```

`optionalMethod` can be matched by the following groups, from most to least important:
- `multiline-optional-method` or `optional-multiline-method`.
- `multiline-method`.
- `optional-method`.
- `method`.
- `multiline-optional-member` or `optional-multiline-member`.
- `multiline-member`.
- `optional-member`.
- `member`.
- `unknown`.

Example 2 (The most important group is written in the comments):

```ts
interface Interface {
// 'index-signature'
[key: string]: any;
// 'optional-property'
description?: string;
// 'required-method'
method(): string
```
### customGroups
<Important title="Migrating from the old API">
Support for the object-based `customGroups` option is deprecated.
Migrating from the old to the current API is easy:
Old API:
```ts
{
"key1": "value1",
"key2": "value2"
}
```
Current API:
```ts
[
{
"groupName": "key1",
"elementNamePattern": "value1"
},
{
"groupName": "key2",
"elementNamePattern": "value2"
}
]
```
</Important>
<sub>
type: `{ [groupName: string]: string | string[] }`
type: `Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>`
</sub>
<sub>default: `{}`</sub>
Expand All @@ -316,7 +408,7 @@ Custom group matching takes precedence over predefined group matching.
#### Example
Put all properties starting with `id` and `name` at the top, combine and sort metadata and multiline properties at the bottom.
Put all properties starting with `id` and `name` at the top, combine and sort metadata and optional multiline properties at the bottom.
Anything else is put in the middle.
```ts
Expand All @@ -326,7 +418,7 @@ interface User {
age: number // unknown
isAdmin: boolean // unknown
lastUpdated_metadata: Date // bottom
localization: { // multiline
localization?: { // multiline
// Stuff about localization
}
version_metadata: string // bottom
Expand All @@ -338,14 +430,22 @@ interface User {
```js
{
groups: [
+ 'top', // [!code ++]
+ 'top', // [!code ++]
'unknown',
['multiline', 'bottom'] // [!code ++]
+ ['optional-multiline', 'bottom'] // [!code ++]
],
+ customGroups: { // [!code ++]
+ top: ['^id$', '^name$'] // [!code ++]
+ bottom: '.+_metadata$' // [!code ++]
+ } // [!code ++]
+ customGroups: [ // [!code ++]
+ { // [!code ++]
+ groupName: 'top', // [!code ++]
+ selector: 'property', // [!code ++]
+ elementNamePattern: '^(?:id|name)$', // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'bottom', // [!code ++]
+ selector: 'property', // [!code ++]
+ elementNamePattern: '.+_metadata$', // [!code ++]
+ } // [!code ++]
+ ] // [!code ++]
}
```
Expand Down
68 changes: 33 additions & 35 deletions docs/content/rules/sort-modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Example :
export default class {}
```

`field` can be matched by the following groups, from most to least important:
`class` can be matched by the following groups, from most to least important:
- `default-export-class` or `export-default-class`.
- `default-class`.
- `export-class`.
Expand Down Expand Up @@ -446,40 +446,38 @@ Example:
'unknown',
],
+ customGroups: [ // [!code ++]
+ [ // [!code ++]
+ { // [!code ++]
+ groupName: 'input-types-and-interfaces', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'type', // [!code ++]
+ elementNamePattern: 'Input'. // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'interface', // [!code ++]
+ elementNamePattern: 'Input'. // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'output-types-and-interfaces', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'type', // [!code ++]
+ elementNamePattern: 'Output' // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'interface', // [!code ++]
+ elementNamePattern: 'Output' // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'unsorted-functions', // [!code ++]
+ type: 'unsorted', // [!code ++]
+ selector: 'function', // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ ] // [!code ++]
+ { // [!code ++]
+ groupName: 'input-types-and-interfaces', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'type', // [!code ++]
+ elementNamePattern: 'Input'. // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'interface', // [!code ++]
+ elementNamePattern: 'Input'. // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'output-types-and-interfaces', // [!code ++]
+ anyOf: [ // [!code ++]
+ { // [!code ++]
+ selector: 'type', // [!code ++]
+ elementNamePattern: 'Output' // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ selector: 'interface', // [!code ++]
+ elementNamePattern: 'Output' // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
+ }, // [!code ++]
+ { // [!code ++]
+ groupName: 'unsorted-functions', // [!code ++]
+ type: 'unsorted', // [!code ++]
+ selector: 'function', // [!code ++]
+ }, // [!code ++]
+ ] // [!code ++]
}
```

Expand Down
Loading

0 comments on commit 8407bae

Please sign in to comment.