From 036341653eb542658a93bf96f644b3e161abe614 Mon Sep 17 00:00:00 2001 From: "Azat S." Date: Tue, 16 Jul 2024 01:57:57 +0300 Subject: [PATCH] feat: use ignore pattern when sorting object for call expressions --- rules/sort-objects.ts | 30 +++++++++++++++++++++--------- test/sort-objects.test.ts | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) 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: [], + }) }) })