diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index 1c0163ce8..7aee29cb2 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -130,24 +130,38 @@ 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' && - options.ignorePattern.some(pattern => - minimatch(variableIdentifier, pattern, { - nocomment: true, - }), - ) - ) { - shouldIgnore = true + let checkMatch = (identifier: string | null) => { + if ( + typeof identifier === 'string' && + options.ignorePattern.some(pattern => + minimatch(identifier, pattern, { + nocomment: true, + }), + ) + ) { + shouldIgnore = true + } } + + checkMatch(varIdentifier) + + let callParent = getNodeParent(node, ['CallExpression']) + let callIdentifier = + callParent?.type === 'CallExpression' && + callParent.callee.type === 'Identifier' + ? callParent.callee.name + : null + + checkMatch(callIdentifier) } if (!shouldIgnore && node.properties.length > 1) { diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index 5672faadb..3875f824c 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: [], + }) }) })