Skip to content

Commit

Permalink
feat: use ignore pattern when sorting object for call expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jul 22, 2024
1 parent d4e8011 commit 471de40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
30 changes: 21 additions & 9 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,34 @@ export default createEslintRule<Options, MESSAGE_ID>({

let shouldIgnore = false
if (options.ignorePattern.length) {
let parent = getNodeParent(node, ['VariableDeclarator', 'Property'])
let varParent = getNodeParent(node, ['VariableDeclarator', 'Property'])
let parentId =
parent?.type === 'VariableDeclarator'
? parent.id
: (parent as TSESTree.Property | null)?.key
let variableIdentifier =
varParent?.type === 'VariableDeclarator'
? varParent.id
: (varParent as TSESTree.Property | null)?.key

let varIdentifier =
parentId?.type === 'Identifier' ? parentId.name : null

if (
typeof variableIdentifier === 'string' &&
let checkMatch = (identifier: string) =>
options.ignorePattern.some(pattern =>
minimatch(variableIdentifier, pattern, {
minimatch(identifier, pattern, {
nocomment: true,
}),
)
) {

if (typeof varIdentifier === 'string' && checkMatch(varIdentifier)) {
shouldIgnore = true
}

let callParent = getNodeParent(node, ['CallExpression'])
let callIdentifier =
callParent?.type === 'CallExpression' &&
callParent.callee.type === 'Identifier'
? callParent.callee.name
: null

if (callIdentifier && checkMatch(callIdentifier)) {
shouldIgnore = true
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2849,5 +2849,25 @@ describe(RULE_NAME, () => {
},
],
})

ruleTester.run(`${RULE_NAME}: allow to ignore pattern`, rule, {
valid: [
{
code: dedent`
ignore({
c: 'c',
b: 'bb',
a: 'aaa',
})
`,
options: [
{
ignorePattern: ['ignore'],
},
],
},
],
invalid: [],
})
})
})

0 comments on commit 471de40

Please sign in to comment.