|
1 |
| -import { |
2 |
| - findChildContainingExactPosition, |
3 |
| - getChangesTracker, |
4 |
| - getPositionHighlights, |
5 |
| - isValidInitializerForDestructure, |
6 |
| - isNameUniqueAtNodeClosestScope, |
7 |
| -} from '../../utils' |
| 1 | +import { findChildContainingExactPosition, getChangesTracker, getPositionHighlights, isValidInitializerForDestructure, makeUniqueName } from '../../utils' |
8 | 2 | import { CodeAction } from '../getCodeActions'
|
9 | 3 |
|
10 | 4 | const createDestructuredDeclaration = (initializer: ts.Expression, type: ts.TypeNode | undefined, declarationName: ts.BindingName) => {
|
@@ -32,46 +26,86 @@ const addDestructureToVariableWithSplittedPropertyAccessors = (
|
32 | 26 | formatOptions: ts.FormatCodeSettings | undefined,
|
33 | 27 | languageService: ts.LanguageService,
|
34 | 28 | ) => {
|
35 |
| - if (!ts.isIdentifier(node) && !(ts.isPropertyAccessExpression(node.parent) || ts.isParameter(node.parent))) return |
| 29 | + if (!ts.isIdentifier(node) && !(ts.isPropertyAccessExpression(node.parent) || ts.isParameter(node.parent) || !ts.isElementAccessExpression(node.parent))) |
| 30 | + return |
36 | 31 |
|
37 | 32 | const highlightPositions = getPositionHighlights(node.getStart(), sourceFile, languageService)
|
38 | 33 |
|
39 | 34 | if (!highlightPositions) return
|
40 | 35 | const tracker = getChangesTracker(formatOptions ?? {})
|
41 | 36 |
|
42 |
| - const propertyNames: Array<{ initial: string; unique: string | undefined }> = [] |
| 37 | + const propertyNames: Array<{ initial: string; unique: string | undefined; dotDotDotToken?: ts.DotDotDotToken }> = [] |
43 | 38 | let nodeToReplaceWithBindingPattern: ts.Identifier | undefined
|
44 | 39 |
|
45 | 40 | for (const pos of highlightPositions) {
|
46 | 41 | const highlightedNode = findChildContainingExactPosition(sourceFile, pos)
|
47 | 42 |
|
48 | 43 | if (!highlightedNode) continue
|
49 | 44 |
|
50 |
| - if (ts.isIdentifier(highlightedNode) && ts.isPropertyAccessExpression(highlightedNode.parent)) { |
51 |
| - const propertyAccessorName = highlightedNode.parent.name.getText() |
| 45 | + if ( |
| 46 | + ts.isIdentifier(highlightedNode) && |
| 47 | + (ts.isPropertyAccessExpression(highlightedNode.parent) || ts.isElementAccessExpression(highlightedNode.parent)) |
| 48 | + ) { |
| 49 | + if (ts.isElementAccessExpression(highlightedNode.parent) && ts.isIdentifier(highlightedNode.parent.argumentExpression)) { |
| 50 | + const uniqueName = makeUniqueName('newVariable', node, languageService, sourceFile) |
52 | 51 |
|
53 |
| - const uniquePropertyName = isNameUniqueAtNodeClosestScope(propertyAccessorName, node, languageService.getProgram()!.getTypeChecker()) |
54 |
| - ? undefined |
55 |
| - : tsFull.getUniqueName(propertyAccessorName, sourceFile as any) |
| 52 | + propertyNames.push({ |
| 53 | + initial: 'newVariable', |
| 54 | + unique: uniqueName === 'newVariable' ? undefined : uniqueName, |
| 55 | + dotDotDotToken: ts.factory.createToken(ts.SyntaxKind.DotDotDotToken), |
| 56 | + }) |
56 | 57 |
|
57 |
| - propertyNames.push({ initial: propertyAccessorName, unique: uniquePropertyName }) |
| 58 | + tracker.replaceRangeWithText(sourceFile, { pos, end: highlightedNode.end }, uniqueName) |
58 | 59 |
|
59 |
| - tracker.replaceRangeWithText(sourceFile, { pos, end: highlightedNode.parent.end }, uniquePropertyName ?? propertyAccessorName) |
| 60 | + continue |
| 61 | + } |
| 62 | + const indexedAccessorName = |
| 63 | + ts.isElementAccessExpression(highlightedNode.parent) && ts.isStringLiteral(highlightedNode.parent.argumentExpression) |
| 64 | + ? highlightedNode.parent.argumentExpression.text |
| 65 | + : undefined |
| 66 | + |
| 67 | + const accessorName = ts.isPropertyAccessExpression(highlightedNode.parent) ? highlightedNode.parent.name.getText() : indexedAccessorName |
| 68 | + |
| 69 | + if (!accessorName) continue |
| 70 | + |
| 71 | + const uniqueName = makeUniqueName(accessorName, node, languageService, sourceFile) |
| 72 | + |
| 73 | + propertyNames.push({ initial: accessorName, unique: uniqueName === accessorName ? undefined : uniqueName }) |
| 74 | + |
| 75 | + // Replace both variable and property access expression `a.fo|o` -> `foo` |
| 76 | + // if (ts.isIdentifier(highlightedNode.parent.expression)) { |
| 77 | + // tracker.replaceRangeWithText( |
| 78 | + // sourceFile, |
| 79 | + // { pos: highlightedNode.parent.end, end: highlightedNode.parent.expression.end }, |
| 80 | + // uniquePropertyName || propertyAccessorName, |
| 81 | + // ) |
| 82 | + // continue |
| 83 | + // } |
| 84 | + |
| 85 | + tracker.replaceRangeWithText(sourceFile, { pos, end: highlightedNode.parent.end }, uniqueName) |
60 | 86 | continue
|
61 | 87 | }
|
62 | 88 |
|
63 | 89 | if (ts.isIdentifier(highlightedNode) && (ts.isVariableDeclaration(highlightedNode.parent) || ts.isParameter(highlightedNode.parent))) {
|
64 | 90 | nodeToReplaceWithBindingPattern = highlightedNode
|
65 | 91 | continue
|
66 | 92 | }
|
| 93 | + // Support for `const a = { foo: 1 }; a.fo|o` refactor activation |
| 94 | + // if (ts.isIdentifier(highlightedNode) && ts.isPropertyAssignment(highlightedNode.parent)) { |
| 95 | + // const closestParent = ts.findAncestor(highlightedNode.parent, n => ts.isVariableDeclaration(n)) |
| 96 | + |
| 97 | + // if (!closestParent || !ts.isVariableDeclaration(closestParent) || !ts.isIdentifier(closestParent.name)) continue |
| 98 | + // nodeToReplaceWithBindingPattern = closestParent.name |
| 99 | + // } |
67 | 100 | }
|
68 | 101 |
|
69 | 102 | if (!nodeToReplaceWithBindingPattern || propertyNames.length === 0) return
|
70 | 103 |
|
71 |
| - const bindings = propertyNames.map(({ initial, unique }) => { |
72 |
| - return ts.factory.createBindingElement(undefined, unique ? initial : undefined, unique ?? initial) |
| 104 | + const bindings = propertyNames.map(({ initial, unique, dotDotDotToken }) => { |
| 105 | + return ts.factory.createBindingElement(dotDotDotToken, unique ? initial : undefined, unique ?? initial) |
73 | 106 | })
|
74 |
| - const bindingPattern = ts.factory.createObjectBindingPattern(bindings) |
| 107 | + const bindingsWithRestLast = bindings.sort((a, b) => (!a.dotDotDotToken && !b.dotDotDotToken ? 0 : -1)) |
| 108 | + const bindingPattern = ts.factory.createObjectBindingPattern(bindingsWithRestLast) |
75 | 109 | const { pos, end } = nodeToReplaceWithBindingPattern
|
76 | 110 |
|
77 | 111 | tracker.replaceRange(
|
|
0 commit comments