|
| 1 | +import type { TSESLint } from '@typescript-eslint/utils' |
| 2 | + |
| 3 | +import type { |
| 4 | + DeprecatedCustomGroupsOption, |
| 5 | + CustomGroupsOption, |
| 6 | + GroupsOptions, |
| 7 | +} from '../types/common-options' |
| 8 | +import type { SortingNode } from '../types/sorting-node' |
| 9 | + |
| 10 | +import { getCommentAboveThatShouldExist } from './get-comment-above-that-should-exist' |
| 11 | +import { isCommentAboveOption } from './is-comment-above-option' |
| 12 | +import { getCommentsBefore } from './get-comments-before' |
| 13 | +import { getGroupIndex } from './get-group-index' |
| 14 | + |
| 15 | +interface MakeCommentAboveFixesParameters { |
| 16 | + options: { |
| 17 | + customGroups?: DeprecatedCustomGroupsOption | CustomGroupsOption |
| 18 | + groups: GroupsOptions<string> |
| 19 | + } |
| 20 | + sourceCode: TSESLint.SourceCode |
| 21 | + sortedNodes: SortingNode[] |
| 22 | + fixer: TSESLint.RuleFixer |
| 23 | +} |
| 24 | + |
| 25 | +export let makeCommentAboveFixes = ({ |
| 26 | + sortedNodes, |
| 27 | + sourceCode, |
| 28 | + options, |
| 29 | + fixer, |
| 30 | +}: MakeCommentAboveFixesParameters): TSESLint.RuleFix[] => { |
| 31 | + let allAutoAddedComments = new Set( |
| 32 | + options.groups |
| 33 | + .filter(group => isCommentAboveOption(group)) |
| 34 | + .map(({ commentAbove }) => commentAbove), |
| 35 | + ) |
| 36 | + |
| 37 | + let fixes: TSESLint.RuleFix[] = [] |
| 38 | + |
| 39 | + let firstNodeFixes = makeCommentAboveFix({ |
| 40 | + nextSortedSortingNode: sortedNodes[0]!, |
| 41 | + sortedSortingNode: null, |
| 42 | + allAutoAddedComments, |
| 43 | + sourceCode, |
| 44 | + options, |
| 45 | + fixer, |
| 46 | + }) |
| 47 | + fixes.push(...firstNodeFixes) |
| 48 | + |
| 49 | + for (let i = 0; i < sortedNodes.length - 1; i++) { |
| 50 | + let sortedSortingNode = sortedNodes.at(i) ?? null |
| 51 | + let nextSortedSortingNode = sortedNodes.at(i + 1)! |
| 52 | + |
| 53 | + let nodeFixes = makeCommentAboveFix({ |
| 54 | + nextSortedSortingNode, |
| 55 | + allAutoAddedComments, |
| 56 | + sortedSortingNode, |
| 57 | + sourceCode, |
| 58 | + options, |
| 59 | + fixer, |
| 60 | + }) |
| 61 | + fixes.push(...nodeFixes) |
| 62 | + } |
| 63 | + |
| 64 | + return fixes |
| 65 | +} |
| 66 | + |
| 67 | +let makeCommentAboveFix = ({ |
| 68 | + nextSortedSortingNode, |
| 69 | + allAutoAddedComments, |
| 70 | + sortedSortingNode, |
| 71 | + sourceCode, |
| 72 | + options, |
| 73 | + fixer, |
| 74 | +}: { |
| 75 | + sortedSortingNode: SortingNode | null |
| 76 | + nextSortedSortingNode: SortingNode |
| 77 | + allAutoAddedComments: Set<string> |
| 78 | +} & Pick< |
| 79 | + MakeCommentAboveFixesParameters, |
| 80 | + 'sourceCode' | 'options' | 'fixer' |
| 81 | +>): TSESLint.RuleFix[] => { |
| 82 | + let leftGroupIndex = sortedSortingNode |
| 83 | + ? getGroupIndex(options.groups, sortedSortingNode) |
| 84 | + : -1 |
| 85 | + let rightGroupIndex = getGroupIndex(options.groups, nextSortedSortingNode) |
| 86 | + |
| 87 | + let commentAboveThatShouldExist = getCommentAboveThatShouldExist({ |
| 88 | + options: { |
| 89 | + ...options, |
| 90 | + groups: options.groups, |
| 91 | + }, |
| 92 | + sortingNode: nextSortedSortingNode, |
| 93 | + rightGroupIndex, |
| 94 | + leftGroupIndex, |
| 95 | + sourceCode, |
| 96 | + }) |
| 97 | + let commentsBefore = getCommentsBefore({ |
| 98 | + node: nextSortedSortingNode.node, |
| 99 | + sourceCode, |
| 100 | + }) |
| 101 | + |
| 102 | + let autoAddedCommentsAboveToRemove = commentsBefore |
| 103 | + .filter( |
| 104 | + comment => |
| 105 | + !commentAboveThatShouldExist?.comment || |
| 106 | + comment.value.slice(1) !== commentAboveThatShouldExist.comment, |
| 107 | + ) |
| 108 | + .filter( |
| 109 | + comment => |
| 110 | + comment.type === 'Line' && |
| 111 | + allAutoAddedComments.has(comment.value.slice(1)), |
| 112 | + ) |
| 113 | + |
| 114 | + let fixes: TSESLint.RuleFix[] = [] |
| 115 | + for (let autoAddedCommentAboveToRemove of autoAddedCommentsAboveToRemove) { |
| 116 | + let nextToken = sourceCode.getTokenAfter(autoAddedCommentAboveToRemove) |
| 117 | + let rangeEnd = nextToken |
| 118 | + ? nextToken.range[0] |
| 119 | + : autoAddedCommentAboveToRemove.range[1] |
| 120 | + fixes.push( |
| 121 | + fixer.removeRange([autoAddedCommentAboveToRemove.range[0], rangeEnd]), |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + if (commentAboveThatShouldExist && !commentAboveThatShouldExist.exists) { |
| 126 | + let nodeToPutCommentBefore = commentsBefore[0] ?? nextSortedSortingNode.node |
| 127 | + fixes.push( |
| 128 | + fixer.insertTextBeforeRange( |
| 129 | + nodeToPutCommentBefore.range, |
| 130 | + `// ${commentAboveThatShouldExist.comment}\n`, |
| 131 | + ), |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + return fixes |
| 136 | +} |
0 commit comments