Skip to content

Commit ac5b240

Browse files
committedNov 27, 2023
feat: add ignore-pattern option for sort-objects rule
1 parent bb542bc commit ac5b240

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed
 

‎docs/rules/sort-objects.md

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ interface Options {
8686
groups?: (string | string[])[]
8787
'custom-groups'?: { [key: string]: string[] | string }
8888
'styled-components'?: boolean
89+
'ignore-pattern'?: string[]
8990
'partition-by-comment'?: string[] | string | boolean
9091
'partition-by-new-line'?: boolean
9192
}
@@ -140,6 +141,14 @@ Example:
140141

141142
When `false`, this rule will be disabled for the styled-components like libraries.
142143

144+
### ignore-pattern
145+
146+
<sub>(default: `[]`)</sub>
147+
148+
If you need to ignore a rule for some interfaces, you can specify their names or a pattern to ignore, for example: `'Component*'` to ignore all interfaces whose names begin with the word Component.
149+
150+
The [minimatch](https://github.com/isaacs/minimatch) library is used for pattern matching.
151+
143152
### partition-by-comment
144153

145154
<sub>(default: `false`)</sub>

‎rules/sort-objects.ts

+38-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { TSESTree } from '@typescript-eslint/types'
22
import type { TSESLint } from '@typescript-eslint/utils'
33

4+
import { minimatch } from 'minimatch'
5+
46
import type { PartitionComment, SortingNode } from '../typings'
57

68
import { isPartitionComment } from '../utils/is-partition-comment'
@@ -37,6 +39,7 @@ type Options = [
3739
'partition-by-new-line': boolean
3840
groups: (string[] | string)[]
3941
'styled-components': boolean
42+
'ignore-pattern': string[]
4043
'ignore-case': boolean
4144
order: SortOrder
4245
type: SortType
@@ -90,6 +93,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
9093
type: 'boolean',
9194
default: false,
9295
},
96+
'ignore-pattern': {
97+
items: {
98+
type: 'string',
99+
},
100+
type: 'array',
101+
},
93102
groups: {
94103
type: 'array',
95104
},
@@ -111,18 +120,35 @@ export default createEslintRule<Options, MESSAGE_ID>({
111120
let sortObject = (
112121
node: TSESTree.ObjectExpression | TSESTree.ObjectPattern,
113122
) => {
114-
if (node.properties.length > 1) {
115-
let options = complete(context.options.at(0), {
116-
'partition-by-new-line': false,
117-
'partition-by-comment': false,
118-
type: SortType.alphabetical,
119-
'styled-components': true,
120-
'ignore-case': false,
121-
order: SortOrder.asc,
122-
'custom-groups': {},
123-
groups: [],
124-
})
125-
123+
let options = complete(context.options.at(0), {
124+
'partition-by-new-line': false,
125+
'partition-by-comment': false,
126+
type: SortType.alphabetical,
127+
'styled-components': true,
128+
'ignore-case': false,
129+
'ignore-pattern': [],
130+
order: SortOrder.asc,
131+
'custom-groups': {},
132+
groups: [],
133+
})
134+
135+
let variableIdentifier =
136+
node.parent.type === 'VariableDeclarator' &&
137+
node.parent.id.type === 'Identifier'
138+
? node.parent.id.name
139+
: null
140+
141+
let shouldIgnore =
142+
options['ignore-pattern'].length &&
143+
typeof variableIdentifier === 'string'
144+
? options['ignore-pattern'].some(pattern =>
145+
minimatch(variableIdentifier!, pattern, {
146+
nocomment: true,
147+
}),
148+
)
149+
: false
150+
151+
if (!shouldIgnore && node.properties.length > 1) {
126152
let isStyledCallExpression = (identifier: TSESTree.Expression) =>
127153
identifier.type === 'Identifier' && identifier.name === 'styled'
128154

‎test/sort-objects.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -2651,5 +2651,27 @@ describe(RULE_NAME, () => {
26512651
invalid: [],
26522652
},
26532653
)
2654+
2655+
ruleTester.run(`${RULE_NAME}: allow to ignore pattern`, rule, {
2656+
valid: [
2657+
{
2658+
code: dedent`
2659+
const buttonStyles = {
2660+
background: "palevioletred",
2661+
display: 'flex',
2662+
flexDirection: 'column',
2663+
width: "50px",
2664+
height: "50px",
2665+
}
2666+
`,
2667+
options: [
2668+
{
2669+
'ignore-pattern': ['*Styles'],
2670+
},
2671+
],
2672+
},
2673+
],
2674+
invalid: [],
2675+
})
26542676
})
26552677
})

0 commit comments

Comments
 (0)
Please sign in to comment.