Skip to content

Commit 7208274

Browse files
authoredFeb 12, 2025··
feat(sort-jsx-props): add use configuration if option
1 parent f639d94 commit 7208274

13 files changed

+359
-78
lines changed
 

‎docs/content/rules/sort-classes.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ This option is only applicable when `partitionByNewLine` is `false`.
294294

295295
### ignoreCallbackDependenciesPatterns
296296

297+
<sub>
298+
type: `string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]`
299+
</sub>
297300
<sub>default: `[]`</sub>
298301

299302
Allows you to specify regexp patterns of function names that should ignore dependency sorting in their callback functions.

‎docs/content/rules/sort-interfaces.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ interface User {
637637
partitionByComment: false,
638638
partitionByNewLine: false,
639639
newlinesBetween: 'ignore',
640+
useConfigurationIf: {},
640641
groupKind: 'mixed',
641642
groups: [],
642643
customGroups: [],
@@ -668,6 +669,7 @@ interface User {
668669
partitionByComment: false,
669670
partitionByNewLine: false,
670671
newlinesBetween: 'ignore',
672+
useConfigurationIf: {},
671673
groupKind: 'mixed',
672674
groups: [],
673675
customGroups: [],

‎docs/content/rules/sort-jsx-props.mdx

+68-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Specifies the sorting method.
144144
- `'natural'` — Sort items in a [natural](https://github.com/yobacca/natural-orderby) order (e.g., “item2” < “item10”).
145145
- `'line-length'` — Sort items by the length of the code line (shorter lines first).
146146
- `'custom'` — Sort items using the alphabet entered in the [`alphabet`](#alphabet) option.
147+
- `'unsorted'` — Do not sort items. To be used with the [`useConfigurationIf`](#useconfigurationif) option.
147148

148149
### order
149150

@@ -211,7 +212,7 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
211212
- `string` — A BCP 47 language tag (e.g. `'en'`, `'en-US'`, `'zh-CN'`).
212213
- `string[]` — An array of BCP 47 language tags.
213214

214-
### ignorePattern
215+
### [DEPRECATED] ignorePattern
215216

216217
<sub>
217218
type:
@@ -223,6 +224,8 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
223224
</sub>
224225
<sub>default: `[]`</sub>
225226

227+
Use the [useConfigurationIf.tagMatchesPattern](#useconfigurationif) option alongside [type: unsorted](#type) instead.
228+
226229
Allows you to specify names or patterns for JSX elements that should be ignored by this rule. This can be useful if you have specific components that you do not want to sort.
227230

228231
You can specify their names or a regexp pattern to ignore, for example: `'^Table.+'` to ignore all object types whose names begin with the word Table.
@@ -233,6 +236,68 @@ You can specify their names or a regexp pattern to ignore, for example: `'^Table
233236

234237
When `true`, the rule will not sort members if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.
235238

239+
### useConfigurationIf
240+
241+
<sub>
242+
type:
243+
```
244+
{
245+
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
246+
tagMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
247+
}
248+
```
249+
</sub>
250+
<sub>default: `{}`</sub>
251+
252+
Allows you to specify filters to match a particular options configuration for a given object type.
253+
254+
The first matching options configuration will be used. If no configuration matches, the default options configuration will be used.
255+
256+
- `allNamesMatchPattern` — A regexp pattern that all keys must match.
257+
258+
Example configuration:
259+
```ts
260+
{
261+
'perfectionist/sort-jsx-props': [
262+
'error',
263+
{
264+
groups: ['r', 'g', 'b'], // Sort tag with colors keys by RGB
265+
customGroups: {
266+
r: '^r$',
267+
g: '^g$',
268+
b: '^b$',
269+
},
270+
useConfigurationIf: {
271+
allNamesMatchPattern: '^r|g|b$',
272+
},
273+
},
274+
{
275+
type: 'alphabetical' // Fallback configuration
276+
}
277+
],
278+
}
279+
```
280+
281+
- `tagMatchesPattern` — A regexp pattern that the JSX tag must match.
282+
283+
Example configuration:
284+
```ts
285+
{
286+
'perfectionist/sort-jsx-props': [
287+
'error',
288+
{
289+
type: 'unsorted', // Do not sort Component elements
290+
useConfigurationIf: {
291+
tagMatchesPattern: '*Component$',
292+
},
293+
},
294+
{
295+
type: 'alphabetical' // Fallback configuration
296+
}
297+
],
298+
}
299+
```
300+
236301
### groups
237302

238303
<sub>
@@ -334,6 +399,7 @@ Custom group matching takes precedence over predefined group matching.
334399
ignorePattern: [],
335400
partitionByNewLine: false,
336401
newlinesBetween: 'ignore',
402+
useConfigurationIf: {},
337403
groups: [],
338404
customGroups: {},
339405
},
@@ -363,6 +429,7 @@ Custom group matching takes precedence over predefined group matching.
363429
ignorePattern: [],
364430
partitionByNewLine: false,
365431
newlinesBetween: 'ignore',
432+
useConfigurationIf: {},
366433
groups: [],
367434
customGroups: {},
368435
},

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

+2
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ This feature is only applicable when `partitionByNewLine` is false.
603603
partitionByComment: false,
604604
partitionByNewLine: false,
605605
newlinesBetween: 'ignore',
606+
useConfigurationIf: {},
606607
groups: [],
607608
customGroups: [],
608609
},
@@ -633,6 +634,7 @@ This feature is only applicable when `partitionByNewLine` is false.
633634
partitionByComment: false,
634635
partitionByNewLine: false,
635636
newlinesBetween: 'ignore',
637+
useConfigurationIf: {},
636638
groups: [],
637639
customGroups: [],
638640
},

‎docs/content/rules/sort-objects.mdx

+4-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Determines whether this rule should be applied to styled-components like librari
292292
- `true` — Apply the rule to styled-components.
293293
- `false` — Disable the rule for styled-components.
294294

295-
### ignorePattern
295+
### [DEPRECATED] ignorePattern
296296

297297
<sub>
298298
type:
@@ -304,7 +304,9 @@ Determines whether this rule should be applied to styled-components like librari
304304
</sub>
305305
<sub>default: `[]`</sub>
306306

307-
Allows you to specify names or patterns for object that should be ignored by this rule. This can be useful if you have specific objects that you do not want to sort.
307+
Use the [useConfigurationIf.declarationMatchesPattern](#useconfigurationif) option alongside [type: unsorted](#type) instead.
308+
309+
Allows you to specify names or patterns for objects that should be ignored by this rule. This can be useful if you have specific objects that you do not want to sort.
308310

309311
You can specify their names or a regexp pattern to ignore, for example: `'^User.+'` to ignore all object whose names begin with the word “User”.
310312

‎rules/sort-classes.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
buildTypeJsonSchema,
1717
commonJsonSchemas,
1818
groupsJsonSchema,
19+
regexJsonSchema,
1920
} from '../utils/common-json-schemas'
2021
import {
2122
DEPENDENCY_ORDER_ERROR,
@@ -250,8 +251,9 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
250251
'name' in nodeValue.callee ? nodeValue.callee.name : null
251252
shouldIgnore =
252253
functionName !== null &&
253-
options.ignoreCallbackDependenciesPatterns.some(pattern =>
254-
matches(functionName, pattern),
254+
matches(
255+
functionName,
256+
options.ignoreCallbackDependenciesPatterns,
255257
)
256258
}
257259
if (!shouldIgnore) {
@@ -654,17 +656,10 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
654656
{
655657
properties: {
656658
...commonJsonSchemas,
657-
ignoreCallbackDependenciesPatterns: {
658-
description:
659-
'Patterns that should be ignored when detecting dependencies in method callbacks.',
660-
items: {
661-
type: 'string',
662-
},
663-
type: 'array',
664-
},
665659
customGroups: buildCustomGroupsArrayJsonSchema({
666660
singleCustomGroupJsonSchema,
667661
}),
662+
ignoreCallbackDependenciesPatterns: regexJsonSchema,
668663
partitionByComment: partitionByCommentJsonSchema,
669664
partitionByNewLine: partitionByNewLineJsonSchema,
670665
newlinesBetween: newlinesBetweenJsonSchema,

‎rules/sort-classes/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export type SortClassesOptions = [
3232
Partial<
3333
{
3434
customGroups: CustomGroupsOption<SingleCustomGroup>
35+
ignoreCallbackDependenciesPatterns: RegexOption
3536
partitionByComment: PartitionByCommentOption
36-
ignoreCallbackDependenciesPatterns: string[]
3737
newlinesBetween: NewlinesBetweenOption
3838
groups: GroupsOptions<Group>
3939
partitionByNewLine: boolean

‎rules/sort-decorators.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
22
import type { TSESTree } from '@typescript-eslint/types'
33

44
import type {
5+
DeprecatedCustomGroupsOption,
56
PartitionByCommentOption,
67
CommonOptions,
8+
GroupsOptions,
79
TypeOption,
810
} from '../types/common-options'
911
import type { SortingNode } from '../types/sorting-node'
@@ -33,12 +35,12 @@ import { isSortable } from '../utils/is-sortable'
3335
import { useGroups } from '../utils/use-groups'
3436
import { complete } from '../utils/complete'
3537

36-
export type Options<T extends string = string> = [
38+
export type Options = [
3739
Partial<
3840
{
3941
partitionByComment: PartitionByCommentOption
40-
customGroups: Record<T, string[] | string>
41-
groups: (Group<T>[] | Group<T>)[]
42+
customGroups: DeprecatedCustomGroupsOption
43+
groups: GroupsOptions<Group>
4244
sortOnParameters: boolean
4345
sortOnProperties: boolean
4446
sortOnAccessors: boolean
@@ -53,7 +55,7 @@ type MESSAGE_ID = 'unexpectedDecoratorsGroupOrder' | 'unexpectedDecoratorsOrder'
5355

5456
type SortDecoratorsSortingNode = SortingNode<TSESTree.Decorator>
5557

56-
type Group<T extends string> = 'unknown' | T
58+
type Group = 'unknown' | string
5759

5860
let defaultOptions: Required<Options[0]> = {
5961
fallbackSort: { type: 'unsorted' },

‎rules/sort-heritage-clauses.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
22
import type { TSESTree } from '@typescript-eslint/types'
33

4-
import type { CommonOptions, TypeOption } from '../types/common-options'
4+
import type {
5+
DeprecatedCustomGroupsOption,
6+
CommonOptions,
7+
GroupsOptions,
8+
TypeOption,
9+
} from '../types/common-options'
510
import type { SortingNode } from '../types/sorting-node'
611

712
import {
@@ -25,11 +30,11 @@ import { isSortable } from '../utils/is-sortable'
2530
import { useGroups } from '../utils/use-groups'
2631
import { complete } from '../utils/complete'
2732

28-
export type Options<T extends string = string> = [
33+
export type Options = [
2934
Partial<
3035
{
31-
customGroups: Record<T, string[] | string>
32-
groups: (Group<T>[] | Group<T>)[]
36+
customGroups: DeprecatedCustomGroupsOption
37+
groups: GroupsOptions<Group>
3338
type: TypeOption
3439
} & CommonOptions
3540
>,
@@ -39,7 +44,7 @@ type MESSAGE_ID =
3944
| 'unexpectedHeritageClausesGroupOrder'
4045
| 'unexpectedHeritageClausesOrder'
4146

42-
type Group<T extends string> = 'unknown' | T
47+
type Group = 'unknown' | string
4348

4449
let defaultOptions: Required<Options[0]> = {
4550
fallbackSort: { type: 'unsorted' },

‎rules/sort-imports.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { TSESTree } from '@typescript-eslint/types'
33
import { builtinModules } from 'node:module'
44

55
import type {
6+
DeprecatedCustomGroupsOption,
67
PartitionByCommentOption,
78
SpecialCharactersOption,
89
NewlinesBetweenOption,
@@ -50,19 +51,19 @@ import { useGroups } from '../utils/use-groups'
5051
import { complete } from '../utils/complete'
5152
import { matches } from '../utils/matches'
5253

53-
export type Options<T extends string = string> = [
54+
export type Options = [
5455
Partial<{
5556
customGroups: {
56-
value?: Record<T, string[] | string>
57-
type?: Record<T, string[] | string>
57+
value?: DeprecatedCustomGroupsOption
58+
type?: DeprecatedCustomGroupsOption
5859
}
5960
partitionByComment: PartitionByCommentOption
6061
specialCharacters: SpecialCharactersOption
6162
locales: NonNullable<Intl.LocalesArgument>
6263
newlinesBetween: NewlinesBetweenOption
6364
fallbackSort: FallbackSortOption
64-
groups: GroupsOptions<Group<T>>
6565
internalPattern: RegexOption[]
66+
groups: GroupsOptions<Group>
6667
environment: 'node' | 'bun'
6768
partitionByNewLine: boolean
6869
sortSideEffects: boolean
@@ -81,7 +82,7 @@ export type MESSAGE_ID =
8182
| 'extraSpacingBetweenImports'
8283
| 'unexpectedImportsOrder'
8384

84-
type Group<T extends string> =
85+
type Group =
8586
| 'side-effect-style'
8687
| 'external-type'
8788
| 'internal-type'
@@ -100,7 +101,7 @@ type Group<T extends string> =
100101
| 'index'
101102
| 'style'
102103
| 'type'
103-
| T
104+
| string
104105

105106
interface SortImportsSortingNode extends SortingNode {
106107
isIgnored: boolean

0 commit comments

Comments
 (0)
Please sign in to comment.