From baf2b7e9574c8054e3645a089efbb291731a50db Mon Sep 17 00:00:00 2001 From: "Azat S." Date: Thu, 19 Dec 2024 23:51:34 +0300 Subject: [PATCH] refactor: fix by code review --- rules/sort-array-includes.ts | 20 +++---- rules/sort-classes.ts | 4 +- rules/sort-enums.ts | 15 ++---- rules/sort-imports.ts | 8 ++- rules/sort-intersection-types.ts | 92 ++++++++++++++++++++------------ rules/sort-modules.ts | 19 ++++--- rules/sort-object-types-utils.ts | 4 +- rules/sort-object-types.ts | 2 +- rules/sort-union-types.ts | 92 ++++++++++++++++++++------------ utils/create-node-index-map.ts | 8 +-- 10 files changed, 149 insertions(+), 115 deletions(-) diff --git a/rules/sort-array-includes.ts b/rules/sort-array-includes.ts index 64eb8e15c..eb9ec70ac 100644 --- a/rules/sort-array-includes.ts +++ b/rules/sort-array-includes.ts @@ -294,24 +294,18 @@ export let sortArray = ({ } for (let nodes of formattedMembers) { - let groupedNodesByKind = groupKindOrder.reduce< - Record<'literal' | 'spread' | 'any', SortArrayIncludesSortingNode[]> - >( - (accumulator, groupKind) => { - accumulator[groupKind] = nodes.filter( - currentNode => - groupKind === 'any' || currentNode.groupKind === groupKind, - ) - return accumulator - }, - { literal: [], spread: [], any: [] }, + let filteredGroupKindNodes = groupKindOrder.map(groupKind => + nodes.filter( + currentNode => + groupKind === 'any' || currentNode.groupKind === groupKind, + ), ) let sortNodesIgnoringEslintDisabledNodes = ( ignoreEslintDisabledNodes: boolean, ): SortArrayIncludesSortingNode[] => - groupKindOrder.flatMap(groupKind => - sortNodesByGroups(groupedNodesByKind[groupKind], options, { + filteredGroupKindNodes.flatMap(groupedNodes => + sortNodesByGroups(groupedNodes, options, { getGroupCompareOptions: groupNumber => getCustomGroupsCompareOptions(options, groupNumber), ignoreEslintDisabledNodes, diff --git a/rules/sort-classes.ts b/rules/sort-classes.ts index 30a725ba5..663b279f6 100644 --- a/rules/sort-classes.ts +++ b/rules/sort-classes.ts @@ -500,8 +500,8 @@ export default createEslintRule({ modifiers, }) - for (let officialGroup of predefinedGroups) { - defineGroup(officialGroup) + for (let predefinedGroup of predefinedGroups) { + defineGroup(predefinedGroup) } for (let customGroup of options.customGroups) { diff --git a/rules/sort-enums.ts b/rules/sort-enums.ts index e444aa925..3386f943a 100644 --- a/rules/sort-enums.ts +++ b/rules/sort-enums.ts @@ -175,16 +175,11 @@ export default createEslintRule({ let sortingNodes = formattedMembers.flat() - let isNumericEnum = true - for (let sortingNode of sortingNodes) { - if ( - sortingNode.numericValue === null || - Number.isNaN(sortingNode.numericValue) - ) { - isNumericEnum = false - break - } - } + let isNumericEnum = sortingNodes.every( + sortingNode => + sortingNode.numericValue !== null && + !Number.isNaN(sortingNode.numericValue), + ) let compareOptions: CompareOptions = { // Get the enum value rather than the name if needed diff --git a/rules/sort-imports.ts b/rules/sort-imports.ts index 74539a911..4513c8827 100644 --- a/rules/sort-imports.ts +++ b/rules/sort-imports.ts @@ -217,7 +217,7 @@ export default createEslintRule, MESSAGE_ID>({ /* Avoid matching on named imports without specifiers */ !/\}\s*from\s+/u.test(sourceCode.getText(node)) - let styleExtensions = new Set([ + let styleExtensions = [ '.less', '.scss', '.sass', @@ -225,12 +225,10 @@ export default createEslintRule, MESSAGE_ID>({ '.pcss', '.css', '.sss', - ]) + ] let isStyle = (value: string): boolean => { let [cleanedValue] = value.split('?') - return [...styleExtensions].some(extension => - cleanedValue.endsWith(extension), - ) + return styleExtensions.some(extension => cleanedValue.endsWith(extension)) } let flatGroups = new Set(options.groups.flat()) diff --git a/rules/sort-intersection-types.ts b/rules/sort-intersection-types.ts index 9f2184fe9..068618db7 100644 --- a/rules/sort-intersection-types.ts +++ b/rules/sort-intersection-types.ts @@ -118,45 +118,67 @@ export default createEslintRule({ sourceCode, }) - let typeToGroupMap: Record = { - TSIntersectionType: 'intersection', - TSTemplateLiteralType: 'literal', - TSConditionalType: 'conditional', - TSUndefinedKeyword: 'nullish', - TSConstructorType: 'function', - TSIndexedAccessType: 'named', - TSBooleanKeyword: 'keyword', - TSUnknownKeyword: 'keyword', - TSFunctionType: 'function', - TSBigIntKeyword: 'keyword', - TSNumberKeyword: 'keyword', - TSObjectKeyword: 'keyword', - TSStringKeyword: 'keyword', - TSSymbolKeyword: 'keyword', - TSTypeOperator: 'operator', - TSNeverKeyword: 'keyword', - TSLiteralType: 'literal', - TSTypeReference: 'named', - TSQualifiedName: 'named', - TSNullKeyword: 'nullish', - TSVoidKeyword: 'nullish', - TSAnyKeyword: 'keyword', - TSTypeQuery: 'operator', - TSTypeLiteral: 'object', - TSMappedType: 'object', - TSImportType: 'import', - TSThisType: 'keyword', - TSArrayType: 'named', - TSInferType: 'named', - TSTupleType: 'tuple', - TSUnionType: 'union', - } - let formattedMembers: SortingNode[][] = node.types.reduce( (accumulator: SortingNode[][], type) => { let { defineGroup, getGroup } = useGroups(options) - defineGroup(typeToGroupMap[type.type]) + switch (type.type) { + case 'TSTemplateLiteralType': + case 'TSLiteralType': + defineGroup('literal') + break + case 'TSIndexedAccessType': + case 'TSTypeReference': + case 'TSQualifiedName': + case 'TSArrayType': + case 'TSInferType': + defineGroup('named') + break + case 'TSIntersectionType': + defineGroup('intersection') + break + case 'TSUndefinedKeyword': + case 'TSNullKeyword': + case 'TSVoidKeyword': + defineGroup('nullish') + break + case 'TSConditionalType': + defineGroup('conditional') + break + case 'TSConstructorType': + case 'TSFunctionType': + defineGroup('function') + break + case 'TSBooleanKeyword': + case 'TSUnknownKeyword': + case 'TSBigIntKeyword': + case 'TSNumberKeyword': + case 'TSObjectKeyword': + case 'TSStringKeyword': + case 'TSSymbolKeyword': + case 'TSNeverKeyword': + case 'TSAnyKeyword': + case 'TSThisType': + defineGroup('keyword') + break + case 'TSTypeOperator': + case 'TSTypeQuery': + defineGroup('operator') + break + case 'TSTypeLiteral': + case 'TSMappedType': + defineGroup('object') + break + case 'TSImportType': + defineGroup('import') + break + case 'TSTupleType': + defineGroup('tuple') + break + case 'TSUnionType': + defineGroup('union') + break + } let lastGroup = accumulator.at(-1) let lastSortingNode = lastGroup?.at(-1) diff --git a/rules/sort-modules.ts b/rules/sort-modules.ts index dc671c300..867c0a3fc 100644 --- a/rules/sort-modules.ts +++ b/rules/sort-modules.ts @@ -210,7 +210,7 @@ let analyzeModule = ({ | TSESTree.DefaultExportDeclarations | TSESTree.NamedExportDeclarations | TSESTree.ProgramStatement, - ): boolean => { + ): void => { if ('declare' in nodeToParse && nodeToParse.declare) { modifiers.push('declare') } @@ -297,12 +297,11 @@ let analyzeModule = ({ break default: } - - return !!selector && !!name } /* eslint-enable @typescript-eslint/no-loop-func */ + parseNode(node) - if (!parseNode(node)) { + if (!selector || !name) { continue } @@ -316,18 +315,18 @@ let analyzeModule = ({ } let { defineGroup, getGroup } = useGroups(options) - for (let officialGroup of generatePredefinedGroups({ + for (let predefinedGroup of generatePredefinedGroups({ cache: cachedGroupsByModifiersAndSelectors, - selectors: [selector!], + selectors: [selector], modifiers, })) { - defineGroup(officialGroup) + defineGroup(predefinedGroup) } for (let customGroup of options.customGroups) { if ( customGroupMatches({ - selectors: [selector!], - elementName: name!, + selectors: [selector], + elementName: name, customGroup, decorators, modifiers, @@ -347,7 +346,7 @@ let analyzeModule = ({ dependencyName: name, group: getGroup(), dependencies, - name: name!, + name, node, } let lastSortingNode = formattedNodes.at(-1)?.at(-1) diff --git a/rules/sort-object-types-utils.ts b/rules/sort-object-types-utils.ts index 6bc97bed2..0dd7bd114 100644 --- a/rules/sort-object-types-utils.ts +++ b/rules/sort-object-types-utils.ts @@ -51,7 +51,9 @@ export let customGroupMatches = (props: CustomGroupMatchesProps): boolean => { props.elementName, props.customGroup.elementNamePattern, ) - return matchesElementNamePattern + if (!matchesElementNamePattern) { + return false + } } return true diff --git a/rules/sort-object-types.ts b/rules/sort-object-types.ts index 8a15a1fa3..04eadabf7 100644 --- a/rules/sort-object-types.ts +++ b/rules/sort-object-types.ts @@ -275,7 +275,7 @@ export let sortObjectTypeElements = ({ } if ( - !(['index-signature', 'method'] as Selector[]).some(selector => + !(['index-signature', 'method']).some(selector => selectors.includes(selector), ) ) { diff --git a/rules/sort-union-types.ts b/rules/sort-union-types.ts index 57534f71c..d7521faed 100644 --- a/rules/sort-union-types.ts +++ b/rules/sort-union-types.ts @@ -118,45 +118,67 @@ export default createEslintRule({ sourceCode, }) - let typeToGroupMap: Record = { - TSIntersectionType: 'intersection', - TSTemplateLiteralType: 'literal', - TSConditionalType: 'conditional', - TSUndefinedKeyword: 'nullish', - TSConstructorType: 'function', - TSIndexedAccessType: 'named', - TSBooleanKeyword: 'keyword', - TSUnknownKeyword: 'keyword', - TSFunctionType: 'function', - TSBigIntKeyword: 'keyword', - TSNumberKeyword: 'keyword', - TSObjectKeyword: 'keyword', - TSStringKeyword: 'keyword', - TSSymbolKeyword: 'keyword', - TSTypeOperator: 'operator', - TSNeverKeyword: 'keyword', - TSLiteralType: 'literal', - TSTypeReference: 'named', - TSQualifiedName: 'named', - TSNullKeyword: 'nullish', - TSVoidKeyword: 'nullish', - TSAnyKeyword: 'keyword', - TSTypeQuery: 'operator', - TSTypeLiteral: 'object', - TSMappedType: 'object', - TSImportType: 'import', - TSThisType: 'keyword', - TSArrayType: 'named', - TSInferType: 'named', - TSTupleType: 'tuple', - TSUnionType: 'union', - } - let formattedMembers: SortingNode[][] = node.types.reduce( (accumulator: SortingNode[][], type) => { let { defineGroup, getGroup } = useGroups(options) - defineGroup(typeToGroupMap[type.type]) + switch (type.type) { + case 'TSTemplateLiteralType': + case 'TSLiteralType': + defineGroup('literal') + break + case 'TSIndexedAccessType': + case 'TSTypeReference': + case 'TSQualifiedName': + case 'TSArrayType': + case 'TSInferType': + defineGroup('named') + break + case 'TSIntersectionType': + defineGroup('intersection') + break + case 'TSUndefinedKeyword': + case 'TSNullKeyword': + case 'TSVoidKeyword': + defineGroup('nullish') + break + case 'TSConditionalType': + defineGroup('conditional') + break + case 'TSConstructorType': + case 'TSFunctionType': + defineGroup('function') + break + case 'TSBooleanKeyword': + case 'TSUnknownKeyword': + case 'TSBigIntKeyword': + case 'TSNumberKeyword': + case 'TSObjectKeyword': + case 'TSStringKeyword': + case 'TSSymbolKeyword': + case 'TSNeverKeyword': + case 'TSAnyKeyword': + case 'TSThisType': + defineGroup('keyword') + break + case 'TSTypeOperator': + case 'TSTypeQuery': + defineGroup('operator') + break + case 'TSTypeLiteral': + case 'TSMappedType': + defineGroup('object') + break + case 'TSImportType': + defineGroup('import') + break + case 'TSTupleType': + defineGroup('tuple') + break + case 'TSUnionType': + defineGroup('union') + break + } let lastGroup = accumulator.at(-1) let lastSortingNode = lastGroup?.at(-1) diff --git a/utils/create-node-index-map.ts b/utils/create-node-index-map.ts index 5ce70e59c..f606f57a4 100644 --- a/utils/create-node-index-map.ts +++ b/utils/create-node-index-map.ts @@ -1,9 +1,11 @@ +import type { TSESTree } from '@typescript-eslint/types' + import type { SortingNode } from '../typings' -export let createNodeIndexMap = ( - nodes: SortingNode[], +export let createNodeIndexMap = ( + nodes: SortingNode[], ): Map => { - let nodeIndexMap = new Map() + let nodeIndexMap = new Map, number>() for (let [index, node] of nodes.entries()) { nodeIndexMap.set(node, index) }