Skip to content

Commit

Permalink
feat(sort-objects): adds `useConfigurationIf.callingFunctionNamePatte…
Browse files Browse the repository at this point in the history
…rn` option
  • Loading branch information
hugop95 committed Dec 14, 2024
1 parent 574e2cc commit 4184e70
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
19 changes: 19 additions & 0 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -336,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
28 changes: 25 additions & 3 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ import { pairwise } from '../utils/pairwise'
import { matches } from '../utils/matches'

type Options = Partial<{
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
useConfigurationIf: {
callingFunctionNamePattern?: string
allNamesMatchPattern?: string
}
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
destructuredObjects: { groups: boolean } | boolean
customGroups: Record<string, string[] | string>
partitionByComment: string[] | boolean | string
Expand Down Expand Up @@ -119,9 +120,24 @@ export default createEslintRule<Options, MESSAGE_ID>({
.map(property => getNodeName({ sourceCode, property }))
.filter(nodeName => nodeName !== null),
contextOptions: context.options,
}).find(options => {
if (!options.useConfigurationIf?.callingFunctionNamePattern) {
return true
}
if (
objectParent?.type === 'VariableDeclarator' ||
!objectParent?.name
) {
return false
}
return matches(
objectParent.name,
options.useConfigurationIf.callingFunctionNamePattern,
)
})

let completeOptions = complete(
matchedContextOptions[0],
matchedContextOptions,
settings,
defaultOptions,
)
Expand Down Expand Up @@ -512,6 +528,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
type: 'array',
},
useConfigurationIf: buildUseConfigurationIfJsonSchema({
additionalProperties: {
callingFunctionNamePattern: {
type: 'string',
},
},
}),
partitionByComment: {
...partitionByCommentJsonSchema,
description:
Expand All @@ -529,7 +552,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
description: 'Controls whether to sort styled components.',
type: 'boolean',
},
useConfigurationIf: buildUseConfigurationIfJsonSchema(),
type: builtTypeJsonSchema({ withUnsorted: true }),
partitionByNewLine: partitionByNewLineJsonSchema,
specialCharacters: specialCharactersJsonSchema,
Expand Down

0 comments on commit 4184e70

Please sign in to comment.