Skip to content

Commit

Permalink
refactor: improve folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Dec 20, 2024
1 parent 5576614 commit eba4394
Show file tree
Hide file tree
Showing 88 changed files with 414 additions and 391 deletions.
14 changes: 8 additions & 6 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
import type { TSESTree } from '@typescript-eslint/types'
import type { TSESLint } from '@typescript-eslint/utils'

import type { Selector, Options } from './sort-array-includes.types'
import type { SortingNode } from '../typings'
import type { Selector, Options } from './sort-array-includes/types'
import type { SortingNode } from '../types'

import {
buildUseConfigurationIfJsonSchema,
Expand All @@ -20,20 +20,22 @@ import {
orderJsonSchema,
} from '../utils/common-json-schemas'
import { validateGeneratedGroupsConfiguration } from '../utils/validate-generated-groups-configuration'
import {
singleCustomGroupJsonSchema,
allSelectors,
} from './sort-array-includes/types'
import { getCustomGroupsCompareOptions } from '../utils/get-custom-groups-compare-options'
import { doesCustomGroupMatch } from './sort-array-includes/does-custom-group-match'
import { getMatchingContextOptions } from '../utils/get-matching-context-options'
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { singleCustomGroupJsonSchema } from './sort-array-includes.types'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { createNodeIndexMap } from '../utils/create-node-index-map'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
import { customGroupMatches } from './sort-array-includes-utils'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { allSelectors } from './sort-array-includes.types'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
Expand Down Expand Up @@ -235,7 +237,7 @@ export let sortArray = <MessageIds extends string>({
let name = getNodeName({ sourceCode, element })
for (let customGroup of options.customGroups) {
if (
customGroupMatches({
doesCustomGroupMatch({
selectors: [selector],
elementName: name,
customGroup,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import type {
SingleCustomGroup,
AnyOfCustomGroup,
Selector,
} from './sort-array-includes.types'
import type { SingleCustomGroup, AnyOfCustomGroup, Selector } from './types'

import { matches } from '../utils/matches'
import { matches } from '../../utils/matches'

interface CustomGroupMatchesProps {
interface DoesCustomGroupMatchProps {
customGroup: SingleCustomGroup | AnyOfCustomGroup
selectors: Selector[]
elementName: string
}

/**
* Determines whether a custom group matches the given properties.
* @param {CustomGroupMatchesProps} props - The properties to compare with the
* @param {DoesCustomGroupMatchProps} props - The properties to compare with the
* custom group, including selectors, modifiers, and element name.
* @returns {boolean} `true` if the custom group matches the properties;
* otherwise, `false`.
*/
export let customGroupMatches = (props: CustomGroupMatchesProps): boolean => {
export let doesCustomGroupMatch = (
props: DoesCustomGroupMatchProps,
): boolean => {
if ('anyOf' in props.customGroup) {
// At least one subgroup must match
return props.customGroup.anyOf.some(subgroup =>
customGroupMatches({ ...props, customGroup: subgroup }),
doesCustomGroupMatch({ ...props, customGroup: subgroup }),
)
}
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'
import {
buildCustomGroupSelectorJsonSchema,
elementNamePatternJsonSchema,
} from '../utils/common-json-schemas'
} from '../../utils/common-json-schemas'

export type Options = Partial<{
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
Expand Down
135 changes: 0 additions & 135 deletions rules/sort-classes-utils.ts

This file was deleted.

12 changes: 5 additions & 7 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
SortClassesOptions,
Modifier,
Selector,
} from './sort-classes.types'
} from './sort-classes/types'
import type { SortingNodeWithDependencies } from '../utils/sort-nodes-by-dependencies'

import {
Expand All @@ -31,14 +31,12 @@ import {
singleCustomGroupJsonSchema,
allModifiers,
allSelectors,
} from './sort-classes.types'
} from './sort-classes/types'
import { validateCustomSortConfiguration } from '../utils/validate-custom-sort-configuration'
import {
getOverloadSignatureGroups,
customGroupMatches,
} from './sort-classes-utils'
import { getCustomGroupsCompareOptions } from '../utils/get-custom-groups-compare-options'
import { getOverloadSignatureGroups } from './sort-classes/get-overload-signature-groups'
import { generatePredefinedGroups } from '../utils/generate-predefined-groups'
import { doesCustomGroupMatch } from './sort-classes/does-custom-group-match'
import { getEslintDisabledLines } from '../utils/get-eslint-disabled-lines'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { hasPartitionComment } from '../utils/is-partition-comment'
Expand Down Expand Up @@ -506,7 +504,7 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({

for (let customGroup of options.customGroups) {
if (
customGroupMatches({
doesCustomGroupMatch({
elementValue: memberValue,
elementName: name,
customGroup,
Expand Down
91 changes: 91 additions & 0 deletions rules/sort-classes/does-custom-group-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import type {
SingleCustomGroup,
AnyOfCustomGroup,
Modifier,
Selector,
} from './types'

import { matches } from '../../utils/matches'

interface DoesCustomGroupMatchProps {
customGroup: SingleCustomGroup | AnyOfCustomGroup
elementValue: undefined | string
selectors: Selector[]
modifiers: Modifier[]
decorators: string[]
elementName: string
}

/**
* Determines whether a custom group matches the given properties.
* @param {DoesCustomGroupMatchProps} props - The properties to match against
* the custom group, including selectors, modifiers, decorators, and element
* names.
* @returns {boolean} `true` if the custom group matches the properties;
* otherwise, `false`.
*/
export let doesCustomGroupMatch = (
props: DoesCustomGroupMatchProps,
): boolean => {
if ('anyOf' in props.customGroup) {
// At least one subgroup must match
return props.customGroup.anyOf.some(subgroup =>
doesCustomGroupMatch({ ...props, customGroup: subgroup }),
)
}
if (
props.customGroup.selector &&
!props.selectors.includes(props.customGroup.selector)
) {
return false
}

if (props.customGroup.modifiers) {
for (let modifier of props.customGroup.modifiers) {
if (!props.modifiers.includes(modifier)) {
return false
}
}
}

if (
'elementNamePattern' in props.customGroup &&
props.customGroup.elementNamePattern
) {
let matchesElementNamePattern: boolean = matches(
props.elementName,
props.customGroup.elementNamePattern,
)
if (!matchesElementNamePattern) {
return false
}
}

if (
'elementValuePattern' in props.customGroup &&
props.customGroup.elementValuePattern
) {
let matchesElementValuePattern: boolean = matches(
props.elementValue ?? '',
props.customGroup.elementValuePattern,
)
if (!matchesElementValuePattern) {
return false
}
}

if (
'decoratorNamePattern' in props.customGroup &&
props.customGroup.decoratorNamePattern
) {
let decoratorPattern = props.customGroup.decoratorNamePattern
let matchesDecoratorNamePattern: boolean = props.decorators.some(
decorator => matches(decorator, decoratorPattern),
)
if (!matchesDecoratorNamePattern) {
return false
}
}

return true
}
Loading

0 comments on commit eba4394

Please sign in to comment.