diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index 1c0163ce..f073f3d3 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -130,22 +130,34 @@ export default createEslintRule({ 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 } } diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index 5672faad..3875f824 100644 --- a/test/sort-objects.test.ts +++ b/test/sort-objects.test.ts @@ -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: [], + }) }) })