diff --git a/docs/content/rules/sort-objects.mdx b/docs/content/rules/sort-objects.mdx index 764d5242..a8e6dbc3 100644 --- a/docs/content/rules/sort-objects.mdx +++ b/docs/content/rules/sort-objects.mdx @@ -232,25 +232,11 @@ Allows you to specify names or patterns for object types that should be ignored You can specify their names or a glob pattern to ignore, for example: `'User*'` to ignore all object types whose names begin with the word “User”. -### customIgnore +### destructureOnly -default: `[]` - -If you need to ignore certain objects using a more complex algorithm, you can write your own function. - -This function takes two parameters as input: an object in the context of an abstract syntax tree and a filename. When writing this function, it is recommended to use [AST Explorer](https://astexplorer.net/) with the TypeScript ESLint Parser. The function should return `true` if the object should be ignored. - -For example, if you want to ignore objects that are not part of a destructuring pattern, you can use this function: +default: `false` -```js -{ - customIgnore: [ - (object, fileName) => { - return node.type !== 'ObjectPattern' - }, - ] -} -``` +Allows you to sort only objects that are part of a destructuring pattern. When set to `true`, the rule will apply sorting exclusively to destructured objects, leaving other object declarations unchanged. ### groups diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index 3eddb0cb..51617993 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -35,16 +35,13 @@ type SortingNodeWithPosition = { type Options = [ Partial<{ - customIgnore: (( - object: TSESTree.ObjectExpression | TSESTree.ObjectPattern, - filename: string, - ) => boolean)[] customGroups: { [key: string]: string[] | string } type: 'alphabetical' | 'line-length' | 'natural' partitionByComment: string[] | boolean | string groups: (string[] | string)[] partitionByNewLine: boolean styledComponents: boolean + destructureOnly: boolean ignorePattern: string[] order: 'desc' | 'asc' ignoreCase: boolean @@ -106,6 +103,10 @@ export default createEslintRule({ description: 'Controls whether to sort styled components.', type: 'boolean', }, + destructureOnly: { + description: 'Controls whether to sort only destructured objects.', + type: 'boolean', + }, ignorePattern: { description: 'Specifies names or patterns for nodes that should be ignored by rule.', @@ -114,10 +115,6 @@ export default createEslintRule({ }, type: 'array', }, - customIgnore: { - description: 'Specifies custom ignore functions.', - type: 'array', - }, groups: { description: 'Specifies the order of the groups.', type: 'array', @@ -168,8 +165,8 @@ export default createEslintRule({ partitionByComment: false, partitionByNewLine: false, styledComponents: true, + destructureOnly: false, ignorePattern: [], - customIgnore: [], groups: [], customGroups: {}, }, @@ -182,22 +179,19 @@ export default createEslintRule({ partitionByNewLine: false, partitionByComment: false, styledComponents: true, + destructureOnly: false, type: 'alphabetical', ignorePattern: [], ignoreCase: true, customGroups: {}, - customIgnore: [], order: 'asc', groups: [], } as const) let shouldIgnore = false - if ( - options.customIgnore.length && - options.customIgnore.some(fn => fn(node, context.filename)) - ) { - shouldIgnore = true + if (options.destructureOnly) { + shouldIgnore = node.type !== 'ObjectPattern' } if (!shouldIgnore && options.ignorePattern.length) { diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index 03abb30f..953738f7 100644 --- a/test/sort-objects.test.ts +++ b/test/sort-objects.test.ts @@ -2868,32 +2868,6 @@ describe(ruleName, () => { }, ], }, - { - code: dedent` - const buttonStyles = { - background: "palevioletred", - display: 'flex', - flexDirection: 'column', - width: "50px", - height: "50px", - } - `, - options: [ - { - customIgnore: [ - node => { - if ( - node.parent.type === 'VariableDeclarator' && - node.parent.id.type === 'Identifier' - ) { - return node.parent.id.name === 'buttonStyles' - } - return false - }, - ], - }, - ], - }, ], invalid: [ { @@ -2948,40 +2922,6 @@ describe(ruleName, () => { }, ], }, - { - code: dedent` - const buttonStyles = { - background: "palevioletred", - display: 'flex', - flexDirection: 'column', - width: "50px", - height: "50px", - } - `, - output: dedent` - const buttonStyles = { - background: "palevioletred", - display: 'flex', - flexDirection: 'column', - height: "50px", - width: "50px", - } - `, - options: [ - { - customIgnore: [() => false], - }, - ], - errors: [ - { - messageId: 'unexpectedObjectsOrder', - data: { - left: 'width', - right: 'height', - }, - }, - ], - }, ], }) @@ -2999,7 +2939,7 @@ describe(ruleName, () => { `, options: [ { - customIgnore: [node => node.type !== 'ObjectPattern'], + destructureOnly: true, }, ], }, @@ -3026,7 +2966,7 @@ describe(ruleName, () => { `, options: [ { - customIgnore: [node => node.type !== 'ObjectPattern'], + destructureOnly: true, }, ], errors: [