Skip to content

Commit

Permalink
feat(sort-jsx-props): add use configuration if option
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Feb 12, 2025
1 parent f639d94 commit 7208274
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 78 deletions.
3 changes: 3 additions & 0 deletions docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ This option is only applicable when `partitionByNewLine` is `false`.

### ignoreCallbackDependenciesPatterns

<sub>
type: `string | string[] | { pattern: string; flags?: string } | { pattern: string; flags?: string }[]`
</sub>
<sub>default: `[]`</sub>

Allows you to specify regexp patterns of function names that should ignore dependency sorting in their callback functions.
Expand Down
2 changes: 2 additions & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ interface User {
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groupKind: 'mixed',
groups: [],
customGroups: [],
Expand Down Expand Up @@ -668,6 +669,7 @@ interface User {
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groupKind: 'mixed',
groups: [],
customGroups: [],
Expand Down
69 changes: 68 additions & 1 deletion docs/content/rules/sort-jsx-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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 @@ -211,7 +212,7 @@ 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>
type:
Expand All @@ -223,6 +224,8 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales](
</sub>
<sub>default: `[]`</sub>

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

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.

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.
Expand All @@ -233,6 +236,68 @@ You can specify their names or a regexp pattern to ignore, for example: `'^Table

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.

### useConfigurationIf

<sub>
type:
```
{
allNamesMatchPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: string }[]
tagMatchesPattern?: string | string[] | { pattern: string; flags: string } | { pattern: string; flags: 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-jsx-props': [
'error',
{
groups: ['r', 'g', 'b'], // Sort tag with colors keys by RGB
customGroups: {
r: '^r$',
g: '^g$',
b: '^b$',
},
useConfigurationIf: {
allNamesMatchPattern: '^r|g|b$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

- `tagMatchesPattern` — A regexp pattern that the JSX tag must match.

Example configuration:
```ts
{
'perfectionist/sort-jsx-props': [
'error',
{
type: 'unsorted', // Do not sort Component elements
useConfigurationIf: {
tagMatchesPattern: '*Component$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

### groups

<sub>
Expand Down Expand Up @@ -334,6 +399,7 @@ Custom group matching takes precedence over predefined group matching.
ignorePattern: [],
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groups: [],
customGroups: {},
},
Expand Down Expand Up @@ -363,6 +429,7 @@ Custom group matching takes precedence over predefined group matching.
ignorePattern: [],
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groups: [],
customGroups: {},
},
Expand Down
2 changes: 2 additions & 0 deletions docs/content/rules/sort-object-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ This feature is only applicable when `partitionByNewLine` is false.
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groups: [],
customGroups: [],
},
Expand Down Expand Up @@ -633,6 +634,7 @@ This feature is only applicable when `partitionByNewLine` is false.
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
groups: [],
customGroups: [],
},
Expand Down
6 changes: 4 additions & 2 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Determines whether this rule should be applied to styled-components like librari
- `true` — Apply the rule to styled-components.
- `false` — Disable the rule for styled-components.

### ignorePattern
### [DEPRECATED] ignorePattern

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

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.
Use the [useConfigurationIf.declarationMatchesPattern](#useconfigurationif) option alongside [type: unsorted](#type) instead.

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.

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”.

Expand Down
15 changes: 5 additions & 10 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
buildTypeJsonSchema,
commonJsonSchemas,
groupsJsonSchema,
regexJsonSchema,
} from '../utils/common-json-schemas'
import {
DEPENDENCY_ORDER_ERROR,
Expand Down Expand Up @@ -250,8 +251,9 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
'name' in nodeValue.callee ? nodeValue.callee.name : null
shouldIgnore =
functionName !== null &&
options.ignoreCallbackDependenciesPatterns.some(pattern =>
matches(functionName, pattern),
matches(
functionName,
options.ignoreCallbackDependenciesPatterns,
)
}
if (!shouldIgnore) {
Expand Down Expand Up @@ -654,17 +656,10 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
{
properties: {
...commonJsonSchemas,
ignoreCallbackDependenciesPatterns: {
description:
'Patterns that should be ignored when detecting dependencies in method callbacks.',
items: {
type: 'string',
},
type: 'array',
},
customGroups: buildCustomGroupsArrayJsonSchema({
singleCustomGroupJsonSchema,
}),
ignoreCallbackDependenciesPatterns: regexJsonSchema,
partitionByComment: partitionByCommentJsonSchema,
partitionByNewLine: partitionByNewLineJsonSchema,
newlinesBetween: newlinesBetweenJsonSchema,
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-classes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export type SortClassesOptions = [
Partial<
{
customGroups: CustomGroupsOption<SingleCustomGroup>
ignoreCallbackDependenciesPatterns: RegexOption
partitionByComment: PartitionByCommentOption
ignoreCallbackDependenciesPatterns: string[]
newlinesBetween: NewlinesBetweenOption
groups: GroupsOptions<Group>
partitionByNewLine: boolean
Expand Down
10 changes: 6 additions & 4 deletions rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
import type { TSESTree } from '@typescript-eslint/types'

import type {
DeprecatedCustomGroupsOption,
PartitionByCommentOption,
CommonOptions,
GroupsOptions,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'
Expand Down Expand Up @@ -33,12 +35,12 @@ import { isSortable } from '../utils/is-sortable'
import { useGroups } from '../utils/use-groups'
import { complete } from '../utils/complete'

export type Options<T extends string = string> = [
export type Options = [
Partial<
{
partitionByComment: PartitionByCommentOption
customGroups: Record<T, string[] | string>
groups: (Group<T>[] | Group<T>)[]
customGroups: DeprecatedCustomGroupsOption
groups: GroupsOptions<Group>
sortOnParameters: boolean
sortOnProperties: boolean
sortOnAccessors: boolean
Expand All @@ -53,7 +55,7 @@ type MESSAGE_ID = 'unexpectedDecoratorsGroupOrder' | 'unexpectedDecoratorsOrder'

type SortDecoratorsSortingNode = SortingNode<TSESTree.Decorator>

type Group<T extends string> = 'unknown' | T
type Group = 'unknown' | string

let defaultOptions: Required<Options[0]> = {
fallbackSort: { type: 'unsorted' },
Expand Down
15 changes: 10 additions & 5 deletions rules/sort-heritage-clauses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
import type { TSESTree } from '@typescript-eslint/types'

import type { CommonOptions, TypeOption } from '../types/common-options'
import type {
DeprecatedCustomGroupsOption,
CommonOptions,
GroupsOptions,
TypeOption,
} from '../types/common-options'
import type { SortingNode } from '../types/sorting-node'

import {
Expand All @@ -25,11 +30,11 @@ import { isSortable } from '../utils/is-sortable'
import { useGroups } from '../utils/use-groups'
import { complete } from '../utils/complete'

export type Options<T extends string = string> = [
export type Options = [
Partial<
{
customGroups: Record<T, string[] | string>
groups: (Group<T>[] | Group<T>)[]
customGroups: DeprecatedCustomGroupsOption
groups: GroupsOptions<Group>
type: TypeOption
} & CommonOptions
>,
Expand All @@ -39,7 +44,7 @@ type MESSAGE_ID =
| 'unexpectedHeritageClausesGroupOrder'
| 'unexpectedHeritageClausesOrder'

type Group<T extends string> = 'unknown' | T
type Group = 'unknown' | string

let defaultOptions: Required<Options[0]> = {
fallbackSort: { type: 'unsorted' },
Expand Down
13 changes: 7 additions & 6 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { TSESTree } from '@typescript-eslint/types'
import { builtinModules } from 'node:module'

import type {
DeprecatedCustomGroupsOption,
PartitionByCommentOption,
SpecialCharactersOption,
NewlinesBetweenOption,
Expand Down Expand Up @@ -50,19 +51,19 @@ import { useGroups } from '../utils/use-groups'
import { complete } from '../utils/complete'
import { matches } from '../utils/matches'

export type Options<T extends string = string> = [
export type Options = [
Partial<{
customGroups: {
value?: Record<T, string[] | string>
type?: Record<T, string[] | string>
value?: DeprecatedCustomGroupsOption
type?: DeprecatedCustomGroupsOption
}
partitionByComment: PartitionByCommentOption
specialCharacters: SpecialCharactersOption
locales: NonNullable<Intl.LocalesArgument>
newlinesBetween: NewlinesBetweenOption
fallbackSort: FallbackSortOption
groups: GroupsOptions<Group<T>>
internalPattern: RegexOption[]
groups: GroupsOptions<Group>
environment: 'node' | 'bun'
partitionByNewLine: boolean
sortSideEffects: boolean
Expand All @@ -81,7 +82,7 @@ export type MESSAGE_ID =
| 'extraSpacingBetweenImports'
| 'unexpectedImportsOrder'

type Group<T extends string> =
type Group =
| 'side-effect-style'
| 'external-type'
| 'internal-type'
Expand All @@ -100,7 +101,7 @@ type Group<T extends string> =
| 'index'
| 'style'
| 'type'
| T
| string

interface SortImportsSortingNode extends SortingNode {
isIgnored: boolean
Expand Down
Loading

0 comments on commit 7208274

Please sign in to comment.