|
| 1 | +import { findChildContainingExactPosition, getChangesTracker, getPositionHighlights, isValidInitializerForDestructure, makeUniqueName } from '../../../utils' |
| 2 | + |
| 3 | +export default (node: ts.Node, sourceFile: ts.SourceFile, formatOptions: ts.FormatCodeSettings | undefined, languageService: ts.LanguageService) => { |
| 4 | + const isValidInitializer = ts.isVariableDeclaration(node.parent) && node.parent.initializer && isValidInitializerForDestructure(node.parent.initializer) |
| 5 | + |
| 6 | + // Make sure it only triggers on the destructuring object or parameter |
| 7 | + if (!ts.isIdentifier(node) || !(isValidInitializer || ts.isParameter(node.parent))) return |
| 8 | + |
| 9 | + const highlightPositions = getPositionHighlights(node.getStart(), sourceFile, languageService) |
| 10 | + |
| 11 | + if (!highlightPositions) return |
| 12 | + const tracker = getChangesTracker(formatOptions ?? {}) |
| 13 | + |
| 14 | + const propertyNames: Array<{ initial: string; unique: string | undefined }> = [] |
| 15 | + let nodeToReplaceWithBindingPattern: ts.Identifier | undefined |
| 16 | + |
| 17 | + for (const pos of highlightPositions) { |
| 18 | + const highlightedNode = findChildContainingExactPosition(sourceFile, pos) |
| 19 | + |
| 20 | + if (!highlightedNode) continue |
| 21 | + |
| 22 | + if ( |
| 23 | + ts.isElementAccessExpression(highlightedNode.parent) || |
| 24 | + ts.isCallExpression(highlightedNode.parent.parent) || |
| 25 | + ts.isTypeQueryNode(highlightedNode.parent) |
| 26 | + ) |
| 27 | + return |
| 28 | + |
| 29 | + if (ts.isIdentifier(highlightedNode) && ts.isPropertyAccessExpression(highlightedNode.parent)) { |
| 30 | + const accessorName = highlightedNode.parent.name.getText() |
| 31 | + |
| 32 | + if (!accessorName) continue |
| 33 | + |
| 34 | + const uniqueName = makeUniqueName(accessorName, node, languageService, sourceFile) |
| 35 | + |
| 36 | + propertyNames.push({ initial: accessorName, unique: uniqueName === accessorName ? undefined : uniqueName }) |
| 37 | + const range = |
| 38 | + ts.isPropertyAssignment(highlightedNode.parent.parent) && highlightedNode.parent.parent.name.getText() === accessorName |
| 39 | + ? { |
| 40 | + pos: highlightedNode.parent.parent.pos + highlightedNode.parent.parent.getLeadingTriviaWidth(), |
| 41 | + end: highlightedNode.parent.parent.end, |
| 42 | + } |
| 43 | + : { pos, end: highlightedNode.parent.end } |
| 44 | + |
| 45 | + tracker.replaceRangeWithText(sourceFile, range, uniqueName) |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + if (ts.isIdentifier(highlightedNode) && (ts.isVariableDeclaration(highlightedNode.parent) || ts.isParameter(highlightedNode.parent))) { |
| 50 | + // Already met a target node - abort as we encountered direct use of the potential destructured variable |
| 51 | + if (nodeToReplaceWithBindingPattern) return |
| 52 | + nodeToReplaceWithBindingPattern = highlightedNode |
| 53 | + continue |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (!nodeToReplaceWithBindingPattern || propertyNames.length === 0) return |
| 58 | + |
| 59 | + const bindings = propertyNames.map(({ initial, unique }) => { |
| 60 | + return ts.factory.createBindingElement(undefined, unique ? initial : undefined, unique ?? initial) |
| 61 | + }) |
| 62 | + |
| 63 | + const bindingPattern = ts.factory.createObjectBindingPattern(bindings) |
| 64 | + const { pos, end } = nodeToReplaceWithBindingPattern |
| 65 | + |
| 66 | + tracker.replaceRange( |
| 67 | + sourceFile, |
| 68 | + { |
| 69 | + pos: pos + nodeToReplaceWithBindingPattern.getLeadingTriviaWidth(), |
| 70 | + end, |
| 71 | + }, |
| 72 | + bindingPattern, |
| 73 | + ) |
| 74 | + |
| 75 | + const changes = tracker.getChanges() |
| 76 | + if (!changes) return undefined |
| 77 | + return { |
| 78 | + edits: [ |
| 79 | + { |
| 80 | + fileName: sourceFile.fileName, |
| 81 | + textChanges: changes[0]!.textChanges, |
| 82 | + }, |
| 83 | + ], |
| 84 | + } |
| 85 | +} |
0 commit comments