8000 feat(sort-array-includes): add use configuration if and groups · azat-io/eslint-plugin-perfectionist@a06ce5c · GitHub
[go: up one dir, main page]

Skip to content

Commit a06ce5c

Browse files
authored
feat(sort-array-includes): add use configuration if and groups
1 parent b4ee1cc commit a06ce5c

14 files changed

+1575
-118
lines changed

docs/content/rules/sort-array-includes.mdx

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
177177
- `string` — A BCP 47 language tag (e.g. `'en'`, `'en-US'`, `'zh-CN'`).
178178
- `string[]` — An array of BCP 47 language tags.
179179

180-
### groupKind
180+
### [DEPRECATED] groupKind
181181

182182
<sub>default: `'literals-first'`</sub>
183183

184+
Use the [groups](#groups) option with the `literal` and `spread` selectors instead. Make sure to set this option to `mixed`.
185+
184186
Allows you to group array elements by their kind, determining whether spread values should come before or after literal values.
185187

186188
- `mixed` — Do not group array elements by their kind; spread values are sorted together with literal values.
@@ -228,6 +230,129 @@ if ([
228230

229231
Each group of elements (separated by empty lines) is treated independently, and the order within each group is preserved.
230232

233+
### useConfigurationIf
234+
235+
<sub>
236+
type: `{ allNamesMatchPattern?: string }`
237+
</sub>
238+
<sub>default: `{}`</sub>
239+
240+
Allows you to specify filters to match a particular options configuration for a given object.
241+
242+
The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.
243+
244+
- `allNamesMatchPattern` — A regexp pattern that all object keys must match.
245+
246+
Example configuration:
247+
```ts
248+
{
249+
'perfectionist/sort-array-includes': [
250+
'error',
251+
{
252+
groups: ['r', 'g', 'b'], // Sort colors by RGB
253+
customGroups: [
254+
{
255+
elementNamePattern: '^r$',
256+
groupName: 'r',
257+
},
258+
{
259+
elementNamePattern: '^g$',
260+
groupName: 'g',
261+
},
262+
{
263+
elementNamePattern: '^b$',
264+
groupName: 'b',
265+
},
266+
],
267+
useConfigurationIf: {
268+
allNamesMatchPattern: '^r|g|b$',
269+
},
270+
},
271+
{
272+
type: 'alphabetical' // Fallback configuration
273+
}
274+
],
275+
}
276+
```
277+
278+
### groups
279+
280+
<sub>
281+
type: `Array<string | string[]>`
282+
</sub>
283+
<sub>default: `[]`</sub>
284+
285+
Allows you to specify a list of groups for sorting. Groups help organize elements into categories.
286+
287+
Each element will be assigned a single group specified in the `groups` option (or the `unknown` group if no match is found).
288+
The order of items in the `groups` option determines how groups are ordered.
289+
290+
Within a given group, members will be sorted according to the `type`, `order`, `ignoreCase`, etc. options.
291+
292+
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter.
293+
All members of the groups in the array will be sorted together as if they were part of a single group.
294+
295+
Predefined groups are characterized by a selector.
296+
297+
##### List of selectors
298+
299+
- `literal`: Array elements that are not spread values.
300+
- `spread`: Array elements that are spread values.
301+
302+
### customGroups
303+
304+
<sub>
305+
type: `Array<CustomGroupDefinition | CustomGroupAnyOfDefinition>`
306+
</sub>
307+
<sub>default: `{}`</sub>
308+
309+
You can define your own groups and use regexp pattern to match specific object type members.
310+
311+
A custom group definition may follow one of the two following interfaces:
312+
313+
```ts
314+
interface CustomGroupDefinition {
315+
groupName: string
316+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
317+
order?: 'asc' | 'desc'
318+
selector?: string
319+
elementNamePattern?: string
320+
}
321+
322+
```
323+
An array element will match a `CustomGroupDefinition` group if it matches all the filters of the custom group's definition.
324+
325+
or:
326+
327+
```ts
328+
interface CustomGroupAnyOfDefinition {
329+
groupName: string
330+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
331+
order?: 'asc' | 'desc'
332+
anyOf: Array<{
333+
selector?: string
334+
elementNamePattern?: string
335+
}>
336+
}
337+
```
338+
339+
An array element will match a `CustomGroupAnyOfDefinition` group if it matches all the filters of at least one of the `anyOf` items.
340+
341+
#### Attributes
342+
343+
- `groupName`: The group's name, which needs to be put in the `groups` option.
344+
- `selector`: Filter on the `selector` of the element.
345+
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
346+
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
347+
- `order`: Overrides the sort order for that custom group
348+
349+
#### Match importance
350+
351+
The `customGroups` list is ordered:
352+
The first custom group definition that matches an element will be used.
353+
354+
Custom groups have a higher priority than any predefined group.
355+
231356
## Usage
232357

233358
<CodeTabs
@@ -252,6 +377,9 @@ Each group of elements (separated by empty lines) is treated independently, and
252377
specialCharacters: 'keep',
253378
groupKind: 'literals-first',
254379
partitionByNewLine: false,
380+
useConfigurationIf: {},
381+
groups: [],
382+
customGroups: [],
255383
},
256384
],
257385
},
@@ -278,6 +406,9 @@ Each group of elements (separated by empty lines) is treated independently, and
278406
specialCharacters: 'keep',
279407
groupKind: 'literals-first',
280408
partitionByNewLine: false,
409+
useConfigurationIf: {},
410+
groups: [],
411+
customGroups: [],
281412
},
282413
],
283414
},

docs/content/rules/sort-interfaces.mdx

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,53 @@ Current API:
410410
411411
You can define your own groups and use regexp patterns to match specific interface members.
412412
413-
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:
414-
- `string` — An interface member's name matching the value will be marked as part of the group referenced by the key.
415-
- `string[]` — An interface member's name matching any of the values of the array will be marked as part of the group referenced by the key.
416-
The order of values in the array does not matter.
413+
A custom group definition may follow one of the two following interfaces:
417414
418-
Custom group matching takes precedence over predefined group matching.
415+
```ts
416+
interface CustomGroupDefinition {
417+
groupName: string
418+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
419+
order?: 'asc' | 'desc'
420+
selector?: string
421+
modifiers?: string[]
422+
elementNamePattern?: string
423+
}
424+
425+
```
426+
An interface member will match a `CustomGroupDefinition` group if it matches all the filters of the custom group's definition.
427+
428+
or:
429+
430+
```ts
431+
interface CustomGroupAnyOfDefinition {
432+
groupName: string
433+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
434+
order?: 'asc' | 'desc'
435+
anyOf: Array<{
436+
selector?: string
437+
modifiers?: string[]
438+
elementNamePattern?: string
439+
}>
440+
}
441+
```
442+
443+
An interface member will match a `CustomGroupAnyOfDefinition` group if it matches all the filters of at least one of the `anyOf` items.
444+
445+
#### Attributes
446+
447+
- `groupName`: The group's name, which needs to be put in the `groups` option.
448+
- `selector`: Filter on the `selector` of the element.
449+
- `modifiers`: Filter on the `modifiers` of the element. (All the modifiers of the element must be present in that list)
450+
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
451+
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
452+
- `order`: Overrides the sort order for that custom group
453+
454+
#### Match importance
455+
456+
The `customGroups` list is ordered:
457+
The first custom group definition that matches an element will be used.
458+
459+
Custom groups have a higher priority than any predefined group.
419460
420461
#### Example
421462

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,53 @@ Current API:
375375
376376
You can define your own groups and use regexp pattern to match specific object type members.
377377
378-
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:
379-
- `string` — A type member's name matching the value will be marked as part of the group referenced by the key.
380-
- `string[]` — A type member's name matching any of the values of the array will be marked as part of the group referenced by the key.
381-
The order of values in the array does not matter.
378+
A custom group definition may follow one of the two following interfaces:
382379
383-
Custom group matching takes precedence over predefined group matching.
380+
```ts
381+
interface CustomGroupDefinition {
382+
groupName: string
383+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
384+
order?: 'asc' | 'desc'
385+
selector?: string
386+
modifiers?: string[]
387+
elementNamePattern?: string
388+
}
389+
390+
```
391+
An object type will match a `CustomGroupDefinition` group if it matches all the filters of the custom group's definition.
392+
393+
or:
394+
395+
```ts
396+
interface CustomGroupAnyOfDefinition {
397+
groupName: string
398+
type?: 'alphabetical' | 'natural' | 'line-length' | 'unsorted'
399+
order?: 'asc' | 'desc'
400+
anyOf: Array<{
401+
selector?: string
402+
modifiers?: string[]
403+
elementNamePattern?: string
404+
}>
405+
}
406+
```
407+
408+
An object type will match a `CustomGroupAnyOfDefinition` group if it matches all the filters of at least one of the `anyOf` items.
409+
410+
#### Attributes
411+
412+
- `groupName`: The group's name, which needs to be put in the `groups` option.
413+
- `selector`: Filter on the `selector` of the element.
414+
- `modifiers`: Filter on the `modifiers` of the element. (All the modifiers of the element must be present in that list)
415+
- `elementNamePattern`: If entered, will check that the name of the element matches the pattern entered.
416+
- `type`: Overrides the sort type for that custom group. `unsorted` will not sort the group.
417+
- `order`: Overrides the sort order for that custom group
418+
419+
#### Match importance
420+
421+
The `customGroups` list is ordered:
422+
The first custom group definition that matches an element will be used.
423+
424+
Custom groups have a higher priority than any predefined group.
384425
385426
#### Example
386427

docs/content/rules/sort-objects.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@ Example configuration:
320320
{
321321
groups: ['r', 'g', 'b'], // Sort colors by RGB
322322
customGroups: {
323-
r: 'r',
324-
g: 'g',
325-
b: 'b',
323+
r: '^r$',
324+
g: '^g$',
325+
b: '^b$',
326326
},
327327
useConfigurationIf: {
328328
allNamesMatchPattern: '^r|g|b$',

0 commit comments

Comments
 (0)
0