Skip to content

Commit 7bb92fc

Browse files
hugop95azat-io
authored andcommitted
feat(sort-heritage-clauses): add array-based custom groups api
1 parent ba6c12b commit 7bb92fc

File tree

6 files changed

+1441
-61
lines changed

6 files changed

+1441
-61
lines changed

docs/content/rules/sort-decorators.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ interface CustomGroupDefinition {
355355
order?: 'asc' | 'desc'
356356
fallbackSort?: { type: string; order?: 'asc' | 'desc' }
357357
newlinesInside?: number
358-
selector?: string
359358
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
360359
}
361360

docs/content/rules/sort-heritage-clauses.mdx

Lines changed: 140 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,41 @@ Specifies the sorting locales. Refer To [String.prototype.localeCompare() - loca
145145
- `string` — A BCP 47 language tag (e.g. `'en'`, `'en-US'`, `'zh-CN'`).
146146
- `string[]` — An array of BCP 47 language tags.
147147

148+
### partitionByComment
149+
150+
<sub>default: `false`</sub>
151+
152+
Enables the use of comments to separate class decorators into logical groups.
153+
154+
- `true` — All comments will be treated as delimiters, creating partitions.
155+
- `false` — Comments will not be used as delimiters.
156+
- `RegExpPattern = string | { pattern: string; flags: string}` — A regexp pattern to specify which comments should act as delimiters.
157+
- `RegExpPattern[]` — A list of regexp patterns to specify which comments should act as delimiters.
158+
- `{ block: boolean | RegExpPattern | RegExpPattern[]; line: boolean | RegExpPattern | RegExpPattern[] }` — Specify which block and line comments should act as delimiters.
159+
160+
### partitionByNewLine
161+
162+
<sub>default: `false`</sub>
163+
164+
When `true`, the rule will not sort the heritage clauses if there is an empty line between them.
165+
166+
### newlinesBetween
167+
168+
<sub>type: `number | 'ignore'`</sub>
169+
<sub>default: `'ignore'`</sub>
170+
171+
Specifies how to handle newlines between groups.
172+
173+
- `'ignore'` — Do not report errors related to newlines.
174+
- `0` — No newlines are allowed.
175+
- Any other number — Enforce this number of newlines between each group, and forbid newlines inside groups.
176+
177+
You can also enforce the newline behavior between two specific groups through the `groups` options.
178+
179+
See the [`groups`](#newlines-between-groups) option.
180+
181+
This option is only applicable when [`partitionByNewLine`](#partitionbynewline) is `false`.
182+
148183
### groups
149184

150185
<sub>
@@ -168,21 +203,110 @@ Within a given group, members will be sorted according to the `type`, `order`, `
168203
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter.
169204
All members of the groups in the array will be sorted together as if they were part of a single group.
170205

206+
#### Newlines between groups
207+
208+
You may place `newlinesBetween` objects between your groups to enforce the newline behavior between two specific groups.
209+
210+
See the [`newlinesBetween`](#newlinesbetween) option.
211+
212+
This feature is only applicable when [`partitionByNewLine`](#partitionbynewline) is `false`.
213+
214+
```ts
215+
{
216+
newlinesBetween: 1,
217+
groups: [
218+
'a',
219+
{ newlinesBetween: 0 }, // Overrides the global newlinesBetween option
220+
'b',
221+
]
222+
}
223+
```
224+
171225
### customGroups
172226

227+
<Important title="Migrating from the old API">
228+
Support for the object-based `customGroups` option has been removed.
229+
230+
Migrating from the old to the current API is easy:
231+
232+
Old API:
233+
```ts
234+
{
235+
"key1": "value1",
236+
"key2": "value2"
237+
}
238+
```
239+
240+
Current API:
241+
```ts
242+
[
243+
{
244+
"groupName": "key1",
245+
"elementNamePattern": "value1"
246+
},
247+
{
248+
"groupName": "key2",
249+
"elementNamePattern": "value2"
250+
}
251+
]
252+
```
253+
</Important>
254+
173255
<sub>
174-
type: `{ [groupName: string]: string | string[] }`
256+
type: `Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>`
175257
</sub>
176-
<sub>default: `{}`</sub>
258+
<sub>default: `[]`</sub>
259+
260+
Defines custom groups to match specific heritage clauses.
261+
262+
A custom group definition may follow one of the two following interfaces:
263+
264+
```ts
265+
interface CustomGroupDefinition {
266+
groupName: string
267+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
268+
order?: 'asc' | 'desc'
269+
fallbackSort?: { type: string; order?: 'asc' | 'desc' }
270+
newlinesInside?: number
271+
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
272+
}
273+
274+
```
275+
A declaration will match a `CustomGroupDefinition` group if it matches all the filters of the custom group's definition.
276+
277+
or:
278+
279+
```ts
280+
interface CustomGroupAnyOfDefinition {
281+
groupName: string
282+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
283+
order?: 'asc' | 'desc'
284+
fallbackSort?: { type: string; order?: 'asc' | 'desc' }
285+
newlinesInside?: number
286+
anyOf: Array<{
287+
selector?: string
288+
elementNamePattern?: string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]
289+
}>
290+
}
291+
```
292+
293+
A declaration will match a `CustomGroupAnyOfDefinition` group if it matches all the filters of at least one of the `anyOf` items.
294+
295+
#### Attributes
296+
297+
- `groupName` — The group's name, which needs to be put in the [`groups`](#groups) option.
298+
- `elementNamePattern` — If entered, will check that the name of the element matches the pattern entered.
299+
- `type` — Overrides the [`type`](#type) option for that custom group. `unsorted` will not sort the group.
300+
- `order` — Overrides the [`order`](#order) option for that custom group.
301+
- `fallbackSort` — Overrides the [`fallbackSort`](#fallbacksort) option for that custom group.
302+
- `newlinesInside` — Enforces a specific newline behavior between elements of the group.
177303

178-
You can define your own groups and use regex to match specific heritage clauses.
304+
#### Match importance
179305

180-
Each key of `customGroups` represents a group name which you can then use in the `groups` option. The value for each key can either be of type:
181-
- `string` — A heritage clause's name matching the value will be marked as part of the group referenced by the key.
182-
- `string[]` — A heritage clause's name matching any of the values of the array will be marked as part of the group referenced by the key.
183-
The order of values in the array does not matter.
306+
The `customGroups` list is ordered:
307+
The first custom group definition that matches an element will be used.
184308

185-
Custom group matching takes precedence over predefined group matching.
309+
Custom groups have a higher priority than any predefined group.
186310

187311
#### Example
188312

@@ -231,8 +355,11 @@ interface Interface extends WithId, Logged, StartupService {
231355
fallbackSort: { type: 'unsorted' },
232356
ignoreCase: true,
233357
specialCharacters: 'keep',
358+
partitionByComment: false,
359+
partitionByNewLine: false,
360+
newlinesBetween: 'ignore',
234361
groups: [],
235-
customGroups: {}
362+
customGroups: [],
236363
},
237364
],
238365
},
@@ -258,8 +385,11 @@ interface Interface extends WithId, Logged, StartupService {
258385
fallbackSort: { type: 'unsorted' },
259386
ignoreCase: true,
260387
specialCharacters: 'keep',
388+
partitionByComment: false,
389+
partitionByNewLine: false,
390+
newlinesBetween: 'ignore',
261391
groups: [],
262-
customGroups: {}
392+
customGroups: [],
263393
},
264394
],
265395
},

docs/content/rules/sort-intersection-types.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: sort-intersection-types
3-
description: ESEnsure intersection types in TypeScript are sorted for cleaner and more maintainable code. This ESLint rule promotes a standardized ordering of intersection types
3+
description: Ensure intersection types in TypeScript are sorted for cleaner and more maintainable code. This ESLint rule promotes a standardized ordering of intersection types
44
shortDescription: Enforce sorted intersection types
55
keywords:
66
- eslint

0 commit comments

Comments
 (0)