Skip to content

Commit

Permalink
feat: add ignore rule for objects passed as arguments to function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Dec 15, 2024
1 parent 7fdadd7 commit 41c9f4f
Show file tree
Hide file tree
Showing 28 changed files with 378 additions and 127 deletions.
1 change: 1 addition & 0 deletions docs/content/rules/sort-array-includes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +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.

### order

Expand Down
26 changes: 23 additions & 3 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +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.

### order

Expand Down Expand Up @@ -271,9 +272,9 @@ Determines whether this rule should be applied to styled-components like librari

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

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 objects that you do not want to sort.
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.

You can specify their names or a regexp pattern to ignore, for example: `'^User.+'` to ignore all object types whose names begin with the word “User”.
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”.

### [DEPRECATED] destructureOnly

Expand Down Expand Up @@ -302,7 +303,7 @@ The `groups` attribute allows you to specify whether to use groups to sort destr
### useConfigurationIf

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

Expand Down Expand Up @@ -335,6 +336,25 @@ Example configuration:
}
```

- `callingFunctionNamePattern` — A regexp pattern for matching objects that are passed as arguments to a function with a specific name.

```ts
{
'perfectionist/sort-objects': [
'error',
{
type: 'unsorted', // Don't sort objects passed to createSlice
useConfigurationIf: {
callingFunctionNamePattern: '^createSlice$',
},
},
{
type: 'alphabetical' // Fallback configuration
}
],
}
```

### groups

<sub>
Expand Down
22 changes: 17 additions & 5 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import type { Selector, Options } from './sort-array-includes.types'
import type { SortingNode } from '../typings'

import {
buildUseConfigurationIfJsonSchema,
buildCustomGroupsArrayJsonSchema,
partitionByCommentJsonSchema,
useConfigurationIfJsonSchema,
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateGeneratedGroupsConfiguration } from '../utils/validate-generated-groups-configuration'
import { getCustomGroupsCompareOptions } from '../utils/get-custom-groups-compare-options'
Expand Down Expand Up @@ -89,15 +89,15 @@ export let jsonSchema: JSONSchema4 = {
customGroups: buildCustomGroupsArrayJsonSchema({
singleCustomGroupJsonSchema,
}),
useConfigurationIf: buildUseConfigurationIfJsonSchema(),
type: buildTypeJsonSchema({ withUnsorted: true }),
partitionByNewLine: partitionByNewLineJsonSchema,
useConfigurationIf: useConfigurationIfJsonSchema,
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down Expand Up @@ -174,7 +174,19 @@ export let sortArray = <MessageIds extends string>({
.map(element => getNodeName({ sourceCode, element })),
contextOptions: context.options,
})
let options = complete(matchedContextOptions, settings, defaultOptions)
let completeOptions = complete(
matchedContextOptions[0],
settings,
defaultOptions,
)
let { type } = completeOptions
if (type === 'unsorted') {
return
}
let options = {
...completeOptions,
type,
}
validateGeneratedGroupsConfiguration({
customGroups: options.customGroups,
selectors: allSelectors,
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-array-includes.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
} from '../utils/common-json-schemas'

export type Options = Partial<{
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
useConfigurationIf: {
allNamesMatchPattern?: string
}
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
/**
* @deprecated for {@link `groups`}
*/
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
Expand Down Expand Up @@ -694,10 +694,10 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
specialCharactersJsonSchema,
customGroupsJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
Expand Down Expand Up @@ -116,10 +116,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
Expand Down Expand Up @@ -282,9 +282,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
Expand Down Expand Up @@ -197,9 +197,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-heritage-clauses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
specialCharactersJsonSchema,
customGroupsJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
Expand Down Expand Up @@ -69,10 +69,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
Expand Down Expand Up @@ -635,10 +635,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
definitions: {
'max-line-length-requires-line-length-type': {
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
Expand Down Expand Up @@ -302,10 +302,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
specialCharactersJsonSchema,
customGroupsJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
Expand Down Expand Up @@ -205,10 +205,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
Expand Down Expand Up @@ -199,9 +199,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
Expand Down Expand Up @@ -117,10 +117,10 @@ export default createEslintRule<SortModulesOptions, MESSAGE_ID>({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
Loading

0 comments on commit 41c9f4f

Please sign in to comment.