Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sort-objects): Adds ignore rule for objects passed as arguments to function calls #420

Merged
merged 12 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of the name 'no-sort' or 'preserve-order'? Or do you prefer 'unsorted'?

Copy link
Contributor Author

@hugop95 hugop95 Dec 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat-io

  • I prefer unsorted and no-sort over preserve-order (I feel the first two are more explicit).
  • unsorted is already used in customGroups.
    type CustomGroup = (
    | {
    order?: SortClassesOptions[0]['order']
    type?: SortClassesOptions[0]['type']
    }
    | {
    type?: 'unsorted'
    }
    ) &
    (SingleCustomGroup | AnyOfCustomGroup) & {
    groupName: string
    }

I think it's better for consistency to keep the same name, so I would personally pick unsorted.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thank you 👍


### order

Expand Down
24 changes: 22 additions & 2 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 @@ -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,
builtTypeJsonSchema,
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: builtTypeJsonSchema({ 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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
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,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
builtTypeJsonSchema,
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: builtTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
Loading
Loading