Skip to content

Commit

Permalink
refactor: fix by code review
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Dec 19, 2024
1 parent 214ec82 commit baf2b7e
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 115 deletions.
20 changes: 7 additions & 13 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,18 @@ export let sortArray = <MessageIds extends string>({
}

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,
Expand Down
4 changes: 2 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
modifiers,
})

for (let officialGroup of predefinedGroups) {
defineGroup(officialGroup)
for (let predefinedGroup of predefinedGroups) {
defineGroup(predefinedGroup)
}

for (let customGroup of options.customGroups) {
Expand Down
15 changes: 5 additions & 10 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,11 @@ export default createEslintRule<Options, MESSAGE_ID>({

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<SortEnumsSortingNode> = {
// Get the enum value rather than the name if needed
Expand Down
8 changes: 3 additions & 5 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,18 @@ export default createEslintRule<Options<string[]>, 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',
'.styl',
'.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())
Expand Down
92 changes: 57 additions & 35 deletions rules/sort-intersection-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,45 +118,67 @@ export default createEslintRule<Options, MESSAGE_ID>({
sourceCode,
})

let typeToGroupMap: Record<string, Group> = {
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)
Expand Down
19 changes: 9 additions & 10 deletions rules/sort-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ let analyzeModule = ({
| TSESTree.DefaultExportDeclarations
| TSESTree.NamedExportDeclarations
| TSESTree.ProgramStatement,
): boolean => {
): void => {
if ('declare' in nodeToParse && nodeToParse.declare) {
modifiers.push('declare')
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -347,7 +346,7 @@ let analyzeModule = ({
dependencyName: name,
group: getGroup(),
dependencies,
name: name!,
name,
node,
}
let lastSortingNode = formattedNodes.at(-1)?.at(-1)
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-object-types-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export let customGroupMatches = (props: CustomGroupMatchesProps): boolean => {
props.elementName,
props.customGroup.elementNamePattern,
)
return matchesElementNamePattern
if (!matchesElementNamePattern) {
return false
}
}

return true
Expand Down
2 changes: 1 addition & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export let sortObjectTypeElements = <MessageIds extends string>({
}

if (
!(['index-signature', 'method'] as Selector[]).some(selector =>
!(<Selector[]>['index-signature', 'method']).some(selector =>
selectors.includes(selector),
)
) {
Expand Down
92 changes: 57 additions & 35 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,45 +118,67 @@ export default createEslintRule<Options, MESSAGE_ID>({
sourceCode,
})

let typeToGroupMap: Record<string, Group> = {
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)
Expand Down
8 changes: 5 additions & 3 deletions utils/create-node-index-map.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { TSESTree } from '@typescript-eslint/types'

import type { SortingNode } from '../typings'

export let createNodeIndexMap = (
nodes: SortingNode[],
export let createNodeIndexMap = <Node extends TSESTree.Node>(
nodes: SortingNode<Node>[],
): Map<SortingNode, number> => {
let nodeIndexMap = new Map<SortingNode, number>()
let nodeIndexMap = new Map<SortingNode<Node>, number>()
for (let [index, node] of nodes.entries()) {
nodeIndexMap.set(node, index)
}
Expand Down

0 comments on commit baf2b7e

Please sign in to comment.