diff --git a/docs/content/rules/sort-array-includes.mdx b/docs/content/rules/sort-array-includes.mdx
index 57ae2d215..2b3d1056a 100644
--- a/docs/content/rules/sort-array-includes.mdx
+++ b/docs/content/rules/sort-array-includes.mdx
@@ -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
diff --git a/docs/content/rules/sort-objects.mdx b/docs/content/rules/sort-objects.mdx
index 80048322b..369d6021c 100644
--- a/docs/content/rules/sort-objects.mdx
+++ b/docs/content/rules/sort-objects.mdx
@@ -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
@@ -271,9 +272,9 @@ Determines whether this rule should be applied to styled-components like librari
default: `[]`
-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
@@ -302,7 +303,7 @@ The `groups` attribute allows you to specify whether to use groups to sort destr
### useConfigurationIf
- type: `{ allNamesMatchPattern?: string }`
+ type: `{ allNamesMatchPattern?: string; callingFunctionNamePattern?: string }`
default: `{}`
@@ -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
diff --git a/rules/sort-array-includes.ts b/rules/sort-array-includes.ts
index 0463ef419..6fc52a676 100644
--- a/rules/sort-array-includes.ts
+++ b/rules/sort-array-includes.ts
@@ -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'
@@ -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',
@@ -174,7 +174,19 @@ export let sortArray = ({
.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,
diff --git a/rules/sort-array-includes.types.ts b/rules/sort-array-includes.types.ts
index bdaa5d22a..4ffd62cd5 100644
--- a/rules/sort-array-includes.types.ts
+++ b/rules/sort-array-includes.types.ts
@@ -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`}
*/
diff --git a/rules/sort-classes.ts b/rules/sort-classes.ts
index 6fe28edf0..db3fd280f 100644
--- a/rules/sort-classes.ts
+++ b/rules/sort-classes.ts
@@ -15,11 +15,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
@@ -694,10 +694,10 @@ export default createEslintRule({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-decorators.ts b/rules/sort-decorators.ts
index 26e5cea3f..a868e31d5 100644
--- a/rules/sort-decorators.ts
+++ b/rules/sort-decorators.ts
@@ -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'
@@ -116,10 +116,10 @@ export default createEslintRule, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-enums.ts b/rules/sort-enums.ts
index 8966ed305..3d3250c85 100644
--- a/rules/sort-enums.ts
+++ b/rules/sort-enums.ts
@@ -8,10 +8,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
@@ -282,9 +282,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-exports.ts b/rules/sort-exports.ts
index 04624aaf1..0a7aff295 100644
--- a/rules/sort-exports.ts
+++ b/rules/sort-exports.ts
@@ -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'
@@ -197,9 +197,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-heritage-clauses.ts b/rules/sort-heritage-clauses.ts
index b3a5c30be..d1454dcfa 100644
--- a/rules/sort-heritage-clauses.ts
+++ b/rules/sort-heritage-clauses.ts
@@ -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'
@@ -69,10 +69,10 @@ export default createEslintRule, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-imports.ts b/rules/sort-imports.ts
index 21bf981d0..5ca40c4d6 100644
--- a/rules/sort-imports.ts
+++ b/rules/sort-imports.ts
@@ -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'
@@ -635,10 +635,10 @@ export default createEslintRule, 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': {
diff --git a/rules/sort-intersection-types.ts b/rules/sort-intersection-types.ts
index fe14c2f94..aacdc0355 100644
--- a/rules/sort-intersection-types.ts
+++ b/rules/sort-intersection-types.ts
@@ -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'
@@ -302,10 +302,10 @@ export default createEslintRule({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-jsx-props.ts b/rules/sort-jsx-props.ts
index b667072f0..c84ed3244 100644
--- a/rules/sort-jsx-props.ts
+++ b/rules/sort-jsx-props.ts
@@ -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'
@@ -205,10 +205,10 @@ export default createEslintRule, MESSAGE_ID>({
customGroups: customGroupsJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-maps.ts b/rules/sort-maps.ts
index 84532115c..446fc51de 100644
--- a/rules/sort-maps.ts
+++ b/rules/sort-maps.ts
@@ -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'
@@ -199,9 +199,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-modules.ts b/rules/sort-modules.ts
index d1c511314..2edf0457e 100644
--- a/rules/sort-modules.ts
+++ b/rules/sort-modules.ts
@@ -17,11 +17,11 @@ import {
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
@@ -117,10 +117,10 @@ export default createEslintRule({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-named-exports.ts b/rules/sort-named-exports.ts
index de586374a..07a9d305e 100644
--- a/rules/sort-named-exports.ts
+++ b/rules/sort-named-exports.ts
@@ -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'
@@ -199,9 +199,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-named-imports.ts b/rules/sort-named-imports.ts
index ec17f4fd2..a3bdce20e 100644
--- a/rules/sort-named-imports.ts
+++ b/rules/sort-named-imports.ts
@@ -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'
@@ -212,9 +212,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-object-types.ts b/rules/sort-object-types.ts
index 3f6885cfa..18e3506bd 100644
--- a/rules/sort-object-types.ts
+++ b/rules/sort-object-types.ts
@@ -13,11 +13,11 @@ import {
newlinesBetweenJsonSchema,
customGroupsJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateGeneratedGroupsConfiguration } from '../utils/validate-generated-groups-configuration'
@@ -112,10 +112,10 @@ export let jsonSchema: JSONSchema4 = {
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts
index 82ea83306..767b28212 100644
--- a/rules/sort-objects.ts
+++ b/rules/sort-objects.ts
@@ -1,20 +1,20 @@
-import type { TSESTree } from '@typescript-eslint/types'
+import { TSESTree } from '@typescript-eslint/types'
import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies'
import {
+ buildUseConfigurationIfJsonSchema,
partitionByCommentJsonSchema,
partitionByNewLineJsonSchema,
- useConfigurationIfJsonSchema,
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
customGroupsJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
groupsJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
@@ -22,6 +22,7 @@ import {
} from '../utils/sort-nodes-by-dependencies'
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
+import { getFirstNodeParentWithType } from '../utils/get-first-node-parent-with-type'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { getMatchingContextOptions } from '../utils/get-matching-context-options'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
@@ -35,7 +36,6 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
-import { getNodeParent } from '../utils/get-node-parent'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isSortable } from '../utils/is-sortable'
@@ -48,9 +48,10 @@ import { matches } from '../utils/matches'
type Options = Partial<{
useConfigurationIf: {
+ callingFunctionNamePattern?: string
allNamesMatchPattern?: string
}
- type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
+ type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
destructuredObjects: { groups: boolean } | boolean
customGroups: Record
partitionByComment: string[] | boolean | string
@@ -102,6 +103,9 @@ let defaultOptions: Required = {
export default createEslintRule({
create: context => {
+ let settings = getSettings(context.settings)
+ let sourceCode = getSourceCode(context)
+
let sortObject = (
nodeObject: TSESTree.ObjectExpression | TSESTree.ObjectPattern,
): void => {
@@ -109,15 +113,42 @@ export default createEslintRule({
return
}
- let settings = getSettings(context.settings)
- let sourceCode = getSourceCode(context)
+ let objectParent = getObjectParent({ node: nodeObject })
+
let matchedContextOptions = getMatchingContextOptions({
nodeNames: nodeObject.properties
.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 options = complete(matchedContextOptions, settings, defaultOptions)
+
+ let completeOptions = complete(
+ matchedContextOptions,
+ settings,
+ defaultOptions,
+ )
+ let { type } = completeOptions
+ if (type === 'unsorted') {
+ return
+ }
+ let options = {
+ ...completeOptions,
+ type,
+ }
validateCustomSortConfiguration(options)
validateGroupsConfiguration(
options.groups,
@@ -135,39 +166,13 @@ export default createEslintRule({
return
}
- if (options.ignorePattern.length) {
- let variableParent = getNodeParent(nodeObject, [
- 'VariableDeclarator',
- 'Property',
- ])
- let parentId =
- variableParent?.type === 'VariableDeclarator'
- ? variableParent.id
- : (variableParent as TSESTree.Property | null)?.key
-
- let variableIdentifier =
- parentId?.type === 'Identifier' ? parentId.name : null
-
- let checkMatch = (identifier: string): boolean =>
- options.ignorePattern.some(pattern => matches(identifier, pattern))
-
- if (
- typeof variableIdentifier === 'string' &&
- checkMatch(variableIdentifier)
- ) {
- return
- }
-
- let callParent = getNodeParent(nodeObject, ['CallExpression'])
- let callIdentifier =
- callParent?.type === 'CallExpression' &&
- callParent.callee.type === 'Identifier'
- ? callParent.callee.name
- : null
-
- if (callIdentifier && checkMatch(callIdentifier)) {
- return
- }
+ if (
+ objectParent?.name &&
+ options.ignorePattern.some(pattern =>
+ matches(objectParent.name, pattern),
+ )
+ ) {
+ return
}
let isStyledCallExpression = (identifier: TSESTree.Expression): boolean =>
@@ -523,6 +528,13 @@ export default createEslintRule({
},
type: 'array',
},
+ useConfigurationIf: buildUseConfigurationIfJsonSchema({
+ additionalProperties: {
+ callingFunctionNamePattern: {
+ type: 'string',
+ },
+ },
+ }),
partitionByComment: {
...partitionByCommentJsonSchema,
description:
@@ -540,8 +552,8 @@ export default createEslintRule({
description: 'Controls whether to sort styled components.',
type: 'boolean',
},
+ type: buildTypeJsonSchema({ withUnsorted: true }),
partitionByNewLine: partitionByNewLineJsonSchema,
- useConfigurationIf: useConfigurationIfJsonSchema,
specialCharacters: specialCharactersJsonSchema,
newlinesBetween: newlinesBetweenJsonSchema,
customGroups: customGroupsJsonSchema,
@@ -550,7 +562,6 @@ export default createEslintRule({
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
@@ -601,3 +612,72 @@ let getNodeName = ({
}
return sourceCode.getText(property.key)
}
+
+let getObjectParent = ({
+ node,
+}: {
+ node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
+}): {
+ type: 'VariableDeclarator' | 'CallExpression'
+ name: string
+} | null => {
+ let variableParentName = getVariableParentName({ node })
+ if (variableParentName) {
+ return {
+ type: 'VariableDeclarator',
+ name: variableParentName,
+ }
+ }
+ let callParentName = getCallExpressionParentName({ node })
+ if (callParentName) {
+ return {
+ type: 'CallExpression',
+ name: callParentName,
+ }
+ }
+ return null
+}
+
+let getVariableParentName = ({
+ node,
+}: {
+ node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
+}): string | null => {
+ let variableParent = getFirstNodeParentWithType({
+ allowedTypes: [
+ TSESTree.AST_NODE_TYPES.VariableDeclarator,
+ TSESTree.AST_NODE_TYPES.Property,
+ ],
+ node,
+ })
+ if (!variableParent) {
+ return null
+ }
+ let parentId
+ if (variableParent.type === 'VariableDeclarator') {
+ parentId = variableParent.id
+ } else if ('key' in variableParent) {
+ parentId = variableParent.key
+ /* v8 ignore next 3 - Unsure if we can reach it */
+ } else {
+ return null
+ }
+
+ return parentId.type === 'Identifier' ? parentId.name : null
+}
+
+let getCallExpressionParentName = ({
+ node,
+}: {
+ node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
+}): string | null => {
+ let callParent = getFirstNodeParentWithType({
+ allowedTypes: [TSESTree.AST_NODE_TYPES.CallExpression],
+ node,
+ })
+ if (!callParent) {
+ return null
+ }
+
+ return callParent.callee.type === 'Identifier' ? callParent.callee.name : null
+}
diff --git a/rules/sort-switch-case.ts b/rules/sort-switch-case.ts
index bf41408bd..c9964e362 100644
--- a/rules/sort-switch-case.ts
+++ b/rules/sort-switch-case.ts
@@ -6,10 +6,10 @@ import type { SortingNode } from '../typings'
import {
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import { makeCommentAfterFixes } from '../utils/make-comment-after-fixes'
@@ -270,9 +270,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-union-types.ts b/rules/sort-union-types.ts
index 8fa1601d3..d10801eab 100644
--- a/rules/sort-union-types.ts
+++ b/rules/sort-union-types.ts
@@ -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'
@@ -301,10 +301,10 @@ export default createEslintRule({
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/rules/sort-variable-declarations.ts b/rules/sort-variable-declarations.ts
index a1d04d42e..872f13dcd 100644
--- a/rules/sort-variable-declarations.ts
+++ b/rules/sort-variable-declarations.ts
@@ -7,10 +7,10 @@ import {
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
ignoreCaseJsonSchema,
+ buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
orderJsonSchema,
- typeJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
@@ -289,9 +289,9 @@ export default createEslintRule({
specialCharacters: specialCharactersJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
+ type: buildTypeJsonSchema(),
locales: localesJsonSchema,
order: orderJsonSchema,
- type: typeJsonSchema,
},
additionalProperties: false,
type: 'object',
diff --git a/test/get-matching-context-options.test.ts b/test/get-matching-context-options.test.ts
index a673705f7..e6ea0506d 100644
--- a/test/get-matching-context-options.test.ts
+++ b/test/get-matching-context-options.test.ts
@@ -9,28 +9,30 @@ describe('get-matching-context-options', () => {
let contextOptions = [buildContextOptions('foo'), barContextOptions]
let nodeNames = ['bar1', 'bar2']
- expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual(
+ expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual([
barContextOptions,
- )
+ ])
})
- it('returns `undefined` if no configuration matches', () => {
+ it('returns [] if no configuration matches', () => {
let contextOptions = [buildContextOptions('foo')]
let nodeNames = ['bar1', 'bar2']
- expect(
- getMatchingContextOptions({ contextOptions, nodeNames }),
- ).toBeUndefined()
+ expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual(
+ [],
+ )
})
- it('returns the first context options if no filters are entered', () => {
+ it('returns all context options if no filters are entered', () => {
let emptyContextOptions = buildContextOptions()
+ let secondContextOptions = buildContextOptions()
let contextOptions = [emptyContextOptions, buildContextOptions()]
let nodeNames = ['bar1', 'bar2']
- expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual(
+ expect(getMatchingContextOptions({ contextOptions, nodeNames })).toEqual([
emptyContextOptions,
- )
+ secondContextOptions,
+ ])
})
})
diff --git a/test/sort-array-includes.test.ts b/test/sort-array-includes.test.ts
index 4aa82d86b..b90c4308c 100644
--- a/test/sort-array-includes.test.ts
+++ b/test/sort-array-includes.test.ts
@@ -1831,6 +1831,26 @@ describe(ruleName, () => {
})
describe(`${ruleName}: misc`, () => {
+ ruleTester.run(`${ruleName}: allows to use "unsorted" as type`, rule, {
+ valid: [
+ {
+ code: dedent`
+ [
+ 'b',
+ 'c',
+ 'a'
+ ].includes(value)
+ `,
+ options: [
+ {
+ type: 'unsorted',
+ },
+ ],
+ },
+ ],
+ invalid: [],
+ })
+
ruleTester.run(
`${ruleName}: sets alphabetical asc sorting as default`,
rule,
diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts
index ca7c7ee68..4b6d8df52 100644
--- a/test/sort-objects.test.ts
+++ b/test/sort-objects.test.ts
@@ -1918,6 +1918,84 @@ describe(ruleName, () => {
valid: [],
},
)
+
+ ruleTester.run(
+ `${ruleName}(${type}): allows to use 'callingFunctionNamePattern'`,
+ rule,
+ {
+ invalid: [
+ {
+ errors: [
+ {
+ data: {
+ rightGroup: 'g',
+ leftGroup: 'b',
+ right: 'g',
+ left: 'b',
+ },
+ messageId: 'unexpectedObjectsGroupOrder',
+ },
+ {
+ data: {
+ rightGroup: 'r',
+ leftGroup: 'g',
+ right: 'r',
+ left: 'g',
+ },
+ messageId: 'unexpectedObjectsGroupOrder',
+ },
+ ],
+ options: [
+ {
+ ...options,
+ useConfigurationIf: {
+ callingFunctionNamePattern: 'foo',
+ },
+ },
+ {
+ ...options,
+ customGroups: {
+ r: 'r',
+ g: 'g',
+ b: 'b',
+ },
+ useConfigurationIf: {
+ callingFunctionNamePattern: '^someFunction$',
+ },
+ groups: ['r', 'g', 'b'],
+ },
+ ],
+ output: dedent`
+ let obj = {
+ b,
+ g,
+ r
+ }
+
+ someFunction(true, {
+ r: string,
+ g: string,
+ b: string
+ })
+ `,
+ code: dedent`
+ let obj = {
+ b,
+ g,
+ r
+ }
+
+ someFunction(true, {
+ b: string,
+ g: string,
+ r: string
+ })
+ `,
+ },
+ ],
+ valid: [],
+ },
+ )
})
})
@@ -3701,14 +3779,24 @@ describe(ruleName, () => {
})
describe(`${ruleName}: misc`, () => {
- let ruleTesterJSX = new RuleTester({
- languageOptions: {
- parserOptions: {
- ecmaFeatures: {
- jsx: true,
- },
+ ruleTester.run(`${ruleName}: allows to use "unsorted" as type`, rule, {
+ valid: [
+ {
+ code: dedent`
+ let obj = {
+ b: 'b',
+ c: 'c',
+ a: 'a'
+ }
+ `,
+ options: [
+ {
+ type: 'unsorted',
+ },
+ ],
},
- },
+ ],
+ invalid: [],
})
ruleTester.run(
@@ -4408,6 +4496,15 @@ describe(ruleName, () => {
})
})
+ let ruleTesterJSX = new RuleTester({
+ languageOptions: {
+ parserOptions: {
+ ecmaFeatures: {
+ jsx: true,
+ },
+ },
+ },
+ })
ruleTesterJSX.run(
'allows to disable sorting object is style prop in jsx',
rule,
diff --git a/utils/common-json-schemas.ts b/utils/common-json-schemas.ts
index f3698bbc0..e2b3e5009 100644
--- a/utils/common-json-schemas.ts
+++ b/utils/common-json-schemas.ts
@@ -1,9 +1,17 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'
-export let typeJsonSchema: JSONSchema4 = {
- enum: ['alphabetical', 'natural', 'line-length', 'custom'],
- description: 'Specifies the sorting method.',
- type: 'string',
+export let buildTypeJsonSchema = ({
+ withUnsorted,
+}: { withUnsorted?: boolean } = {}): JSONSchema4 => {
+ let enumValues = ['alphabetical', 'natural', 'line-length', 'custom']
+ if (withUnsorted) {
+ enumValues.push('unsorted')
+ }
+ return {
+ description: 'Specifies the sorting method.',
+ enum: enumValues,
+ type: 'string',
+ }
}
export let orderJsonSchema: JSONSchema4 = {
@@ -111,15 +119,20 @@ export let newlinesBetweenJsonSchema: JSONSchema4 = {
type: 'string',
}
-export let useConfigurationIfJsonSchema: JSONSchema4 = {
+export let buildUseConfigurationIfJsonSchema = ({
+ additionalProperties,
+}: {
+ additionalProperties?: Record
+} = {}): JSONSchema4 => ({
properties: {
allNamesMatchPattern: {
type: 'string',
},
+ ...additionalProperties,
},
additionalProperties: false,
type: 'object',
-}
+})
let customGroupSortJsonSchema: Record = {
type: {
diff --git a/utils/get-first-node-parent-with-type.ts b/utils/get-first-node-parent-with-type.ts
new file mode 100644
index 000000000..ea057b9d0
--- /dev/null
+++ b/utils/get-first-node-parent-with-type.ts
@@ -0,0 +1,23 @@
+import type { TSESTree } from '@typescript-eslint/types'
+
+type NodeOfType = { type: Type } & TSESTree.Node
+
+export let getFirstNodeParentWithType = <
+ NodeType extends TSESTree.AST_NODE_TYPES,
+>({
+ allowedTypes,
+ node,
+}: {
+ allowedTypes: NodeType[]
+ node: TSESTree.Node
+}): NodeOfType | null => {
+ let { parent } = node
+ while (parent) {
+ if ((allowedTypes as string[]).includes(parent.type)) {
+ return parent as NodeOfType
+ }
+
+ ;({ parent } = parent)
+ }
+ return null
+}
diff --git a/utils/get-matching-context-options.ts b/utils/get-matching-context-options.ts
index 0237729c9..c65d9dda3 100644
--- a/utils/get-matching-context-options.ts
+++ b/utils/get-matching-context-options.ts
@@ -6,14 +6,14 @@ interface Options {
}
}
-export let getMatchingContextOptions = ({
+export let getMatchingContextOptions = ({
contextOptions,
nodeNames,
}: {
- contextOptions: Options[]
+ contextOptions: T[]
nodeNames: string[]
-}): undefined | Options =>
- contextOptions.find(options => {
+}): T[] =>
+ contextOptions.filter(options => {
let allNamesMatchPattern = options.useConfigurationIf?.allNamesMatchPattern
return (
!allNamesMatchPattern ||
diff --git a/utils/get-node-parent.ts b/utils/get-node-parent.ts
deleted file mode 100644
index 607383e19..000000000
--- a/utils/get-node-parent.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { TSESTree } from '@typescript-eslint/types'
-
-export let getNodeParent = (
- node: TSESTree.Node,
- type: string[] | string,
-): TSESTree.Node | null => {
- let types = type
- let { parent } = node as { parent: TSESTree.Node | null }
- while (parent) {
- if (types.includes(parent.type)) {
- return parent
- }
-
- ;({ parent } = parent as { parent: TSESTree.Node | null })
- }
- return null
-}