Skip to content
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

feat: adds useConfigurationIf option to sort-object-types and sort-interfaces #423

Merged
merged 4 commits into from
Dec 19, 2024
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
2 changes: 1 addition & 1 deletion docs/content/rules/sort-array-includes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useConfigurationIf) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.

### order

Expand Down
61 changes: 60 additions & 1 deletion docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.

### order

Expand Down Expand Up @@ -191,10 +192,12 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
- `string` — A BCP 47 language tag (e.g. `'en'`, `'en-US'`, `'zh-CN'`).
- `string[]` — An array of BCP 47 language tags.

### ignorePattern
### [DEPRECATED] ignorePattern

<sub>default: `[]`</sub>

Use the [useConfigurationIf.declarationMatchesPattern](#useconfigurationif) option alongside [type: unsorted](#type) instead.

Allows you to specify names or patterns for interfaces that should be ignored by this rule. This can be useful if you have specific interfaces that you do not want to sort.

You can specify their names or a regexp pattern to ignore, for example: `'^Component.+'` to ignore all interfaces whose names begin with the word “Component”.
Expand Down Expand Up @@ -258,6 +261,62 @@ Specifies how optional and required members should be ordered in TypeScript inte
- `'required-first'` — Put all required members before optional members.
- `'mixed'` — Do not enforce any specific order based on optionality.

### useConfigurationIf

<sub>
type: `{ allNamesMatchPattern?: string; declarationMatchesPattern?: string }`
</sub>
<sub>default: `{}`</sub>

Allows you to specify filters to match a particular options configuration for a given interface.

The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.

- `allNamesMatchPattern` — A regexp pattern that all keys must match.

Example configuration:
```ts
{
'perfectionist/sort-interfaces': [
'error',
{
groups: ['r', 'g', 'b'], // Sort colors types by RGB
customGroups: {
r: '^r$',
g: '^g$',
b: '^b$',
},
useConfigurationIf: {
allNamesMatchPattern: '^r|g|b$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

- `declarationMatchesPattern` — A regexp pattern that the interface declaration must match.

Example configuration:
```ts
{
'perfectionist/sort-interfaces': [
'error',
{
type: 'unsorted', // Do not sort Metadata interfaces
useConfigurationIf: {
declarationMatchesPattern: '*Metadata$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

### groups

<sub>
Expand Down
61 changes: 60 additions & 1 deletion docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.

### order

Expand Down Expand Up @@ -153,10 +154,12 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
- `string` — A BCP 47 language tag (e.g. `'en'`, `'en-US'`, `'zh-CN'`).
- `string[]` — An array of BCP 47 language tags.

### ignorePattern
### [DEPRECATED] ignorePattern

<sub>default: `[]`</sub>

Use the [useConfigurationIf.declarationMatchesPattern](#useconfigurationif) option alongside [type: unsorted](#type) instead.

Allows you to specify names or patterns for object types that should be ignored by this rule. This can be useful if you have specific object types that you do not want to sort.

You can specify their names or a regexp pattern to ignore, for example: `'^Component.+'` to ignore all object types whose names begin with the word “Component”.
Expand Down Expand Up @@ -223,6 +226,62 @@ Allows you to group type object keys by their kind, determining whether required
- `required-first` — Group all required values before optional.
- `optional-first` — Group all optional values before required.

### useConfigurationIf

<sub>
type: `{ allNamesMatchPattern?: string; declarationMatchesPattern?: string }`
</sub>
<sub>default: `{}`</sub>

Allows you to specify filters to match a particular options configuration for a given object type.

The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.

- `allNamesMatchPattern` — A regexp pattern that all keys must match.

Example configuration:
```ts
{
'perfectionist/sort-object-types': [
'error',
{
groups: ['r', 'g', 'b'], // Sort colors types by RGB
customGroups: {
r: '^r$',
g: '^g$',
b: '^b$',
},
useConfigurationIf: {
allNamesMatchPattern: '^r|g|b$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

- `declarationMatchesPattern` — A regexp pattern that the object type declaration must match.

Example configuration:
```ts
{
'perfectionist/sort-object-types': [
'error',
{
type: 'unsorted', // Do not sort Metadata types
useConfigurationIf: {
declarationMatchesPattern: '*Metadata$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

### groups

<sub>
Expand Down
2 changes: 1 addition & 1 deletion docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Specifies the sorting method.
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useConfigurationIf) option.
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.

### order

Expand Down
3 changes: 2 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let defaultOptions: Required<Options[0]> = {
partitionByNewLine: false,
newlinesBetween: 'ignore',
specialCharacters: 'keep',
useConfigurationIf: {},
type: 'alphabetical',
groupKind: 'mixed',
ignorePattern: [],
Expand Down Expand Up @@ -44,7 +45,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
description: 'Enforce sorted interface properties.',
recommended: true,
},
schema: [jsonSchema],
schema: jsonSchema,
type: 'suggestion',
fixable: 'code',
},
Expand Down
Loading
Loading