Skip to content

Commit 471de40

Browse files
committed
feat: use ignore pattern when sorting object for call expressions
1 parent d4e8011 commit 471de40

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

rules/sort-objects.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,34 @@ export default createEslintRule<Options, MESSAGE_ID>({
130130

131131
let shouldIgnore = false
132132
if (options.ignorePattern.length) {
133-
let parent = getNodeParent(node, ['VariableDeclarator', 'Property'])
133+
let varParent = getNodeParent(node, ['VariableDeclarator', 'Property'])
134134
let parentId =
135-
parent?.type === 'VariableDeclarator'
136-
? parent.id
137-
: (parent as TSESTree.Property | null)?.key
138-
let variableIdentifier =
135+
varParent?.type === 'VariableDeclarator'
136+
? varParent.id
137+
: (varParent as TSESTree.Property | null)?.key
138+
139+
let varIdentifier =
139140
parentId?.type === 'Identifier' ? parentId.name : null
140141

141-
if (
142-
typeof variableIdentifier === 'string' &&
142+
let checkMatch = (identifier: string) =>
143143
options.ignorePattern.some(pattern =>
144-
minimatch(variableIdentifier, pattern, {
144+
minimatch(identifier, pattern, {
145145
nocomment: true,
146146
}),
147147
)
148-
) {
148+
149+
if (typeof varIdentifier === 'string' && checkMatch(varIdentifier)) {
150+
shouldIgnore = true
151+
}
152+
153+
let callParent = getNodeParent(node, ['CallExpression'])
154+
let callIdentifier =
155+
callParent?.type === 'CallExpression' &&
156+
callParent.callee.type === 'Identifier'
157+
? callParent.callee.name
158+
: null
159+
160+
if (callIdentifier && checkMatch(callIdentifier)) {
149161
shouldIgnore = true
150162
}
151163
}

test/sort-objects.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -2849,5 +2849,25 @@ describe(RULE_NAME, () => {
28492849
},
28502850
],
28512851
})
2852+
2853+
ruleTester.run(`${RULE_NAME}: allow to ignore pattern`, rule, {
2854+
valid: [
2855+
{
2856+
code: dedent`
2857+
ignore({
2858+
c: 'c',
2859+
b: 'bb',
2860+
a: 'aaa',
2861+
})
2862+
`,
2863+
options: [
2864+
{
2865+
ignorePattern: ['ignore'],
2866+
},
2867+
],
2868+
},
2869+
],
2870+
invalid: [],
2871+
})
28522872
})
28532873
})

0 commit comments

Comments
 (0)