Skip to content

Commit

Permalink
feat: replace sort-objects custom ignore option with destructure only
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Aug 2, 2024
1 parent d07f5f7 commit f3906f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 94 deletions.
20 changes: 3 additions & 17 deletions docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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:
<sub>default: `false`</sub>

```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

Expand Down
24 changes: 9 additions & 15 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,6 +103,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
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.',
Expand All @@ -114,10 +115,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
type: 'array',
},
customIgnore: {
description: 'Specifies custom ignore functions.',
type: 'array',
},
groups: {
description: 'Specifies the order of the groups.',
type: 'array',
Expand Down Expand Up @@ -168,8 +165,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
partitionByComment: false,
partitionByNewLine: false,
styledComponents: true,
destructureOnly: false,
ignorePattern: [],
customIgnore: [],
groups: [],
customGroups: {},
},
Expand All @@ -182,22 +179,19 @@ export default createEslintRule<Options, MESSAGE_ID>({
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) {
Expand Down
64 changes: 2 additions & 62 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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',
},
},
],
},
],
})

Expand All @@ -2999,7 +2939,7 @@ describe(ruleName, () => {
`,
options: [
{
customIgnore: [node => node.type !== 'ObjectPattern'],
destructureOnly: true,
},
],
},
Expand All @@ -3026,7 +2966,7 @@ describe(ruleName, () => {
`,
options: [
{
customIgnore: [node => node.type !== 'ObjectPattern'],
destructureOnly: true,
},
],
errors: [
Expand Down

0 comments on commit f3906f9

Please sign in to comment.