|
| 1 | +import type { Alternative } from '@eslint-community/regexpp/ast' |
| 2 | +import type { TSESLint } from '@typescript-eslint/utils' |
| 3 | +import type { TSESTree } from '@typescript-eslint/types' |
| 4 | + |
| 5 | +import type { SortingNode } from '../../types/sorting-node' |
| 6 | +import type { Options } from './types' |
| 7 | + |
| 8 | +import { doesCustomGroupMatch } from '../../utils/does-custom-group-match' |
| 9 | +import { isNodeEslintDisabled } from '../../utils/is-node-eslint-disabled' |
| 10 | +import { createPseudoLiteralNode } from './create-pseudo-literal-node' |
| 11 | +import { getAlternativeAlias } from './get-alternative-alias' |
| 12 | +import { getSortingNodeName } from './get-sorting-node-name' |
| 13 | +import { computeGroup } from '../../utils/compute-group' |
| 14 | +import { getSelector } from './get-selector' |
| 15 | + |
| 16 | +interface CreateSortingNodeParameters { |
| 17 | + sourceCode: TSESLint.SourceCode |
| 18 | + literalNode: TSESTree.Literal |
| 19 | + eslintDisabledLines: number[] |
| 20 | + alternative: Alternative |
| 21 | + options: ResolvedOptions |
| 22 | +} |
| 23 | + |
| 24 | +type ResolvedOptions = Required<Options[0]> |
| 25 | + |
| 26 | +/** |
| 27 | + * Builds a sortable node representation for a regex alternative. |
| 28 | + * |
| 29 | + * @param parameters - Alternative context with rule settings. |
| 30 | + * @returns Sorting node ready for ordering logic. |
| 31 | + */ |
| 32 | +export function createSortingNode({ |
| 33 | + eslintDisabledLines, |
| 34 | + literalNode, |
| 35 | + alternative, |
| 36 | + sourceCode, |
| 37 | + options, |
| 38 | +}: CreateSortingNodeParameters): SortingNode<TSESTree.Literal> { |
| 39 | + let alternativeAlias = getAlternativeAlias(alternative) |
| 40 | + let selector = getSelector({ alternativeAlias }) |
| 41 | + let name = getSortingNodeName({ |
| 42 | + alternativeAlias, |
| 43 | + alternative, |
| 44 | + options, |
| 45 | + }) |
| 46 | + |
| 47 | + let group = computeGroup({ |
| 48 | + customGroupMatcher: customGroup => |
| 49 | + doesCustomGroupMatch({ |
| 50 | + elementValue: alternative.raw, |
| 51 | + selectors: [selector], |
| 52 | + elementName: name, |
| 53 | + modifiers: [], |
| 54 | + customGroup, |
| 55 | + }), |
| 56 | + predefinedGroups: [selector], |
| 57 | + options, |
| 58 | + }) |
| 59 | + |
| 60 | + let pseudoNode = createPseudoLiteralNode({ |
| 61 | + literalNode, |
| 62 | + alternative, |
| 63 | + sourceCode, |
| 64 | + }) |
| 65 | + |
| 66 | + return { |
| 67 | + isEslintDisabled: isNodeEslintDisabled(literalNode, eslintDisabledLines), |
| 68 | + size: pseudoNode.range[1] - pseudoNode.range[0], |
| 69 | + node: pseudoNode, |
| 70 | + partitionId: 0, |
| 71 | + group, |
| 72 | + name, |
| 73 | + } |
| 74 | +} |
0 commit comments