Skip to content

Commit eeaa8bb

Browse files
authored
Merge pull request #28533 from Microsoft/generateGetAndSet_noFindAllRefs
generateGetAccessorAndSetAccessor: Don't use FindAllReferences
2 parents d7e0866 + 643678b commit eeaa8bb

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
1616
readonly declaration: AcceptedDeclaration;
1717
readonly fieldName: AcceptedNameType;
1818
readonly accessorName: AcceptedNameType;
19-
readonly originalName: AcceptedNameType;
19+
readonly originalName: string;
2020
readonly renameAccessor: boolean;
2121
}
2222

@@ -69,7 +69,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
6969
// readonly modifier only existed in classLikeDeclaration
7070
const constructor = getFirstConstructorWithBody(<ClassLikeDeclaration>container);
7171
if (constructor) {
72-
updateReadonlyPropertyInitializerStatementConstructor(changeTracker, context, constructor, fieldName, originalName);
72+
updateReadonlyPropertyInitializerStatementConstructor(changeTracker, file, constructor, fieldName.text, originalName);
7373
}
7474
}
7575
else {
@@ -135,7 +135,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
135135
isReadonly: hasReadonlyModifier(declaration),
136136
type: getTypeAnnotationNode(declaration),
137137
container: declaration.kind === SyntaxKind.Parameter ? declaration.parent.parent : declaration.parent,
138-
originalName: <AcceptedNameType>declaration.name,
138+
originalName: (<AcceptedNameType>declaration.name).text,
139139
declaration,
140140
fieldName,
141141
accessorName,
@@ -221,22 +221,22 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
221221
: changeTracker.insertNodeAfter(file, declaration, accessor);
222222
}
223223

224-
function updateReadonlyPropertyInitializerStatementConstructor(changeTracker: textChanges.ChangeTracker, context: RefactorContext, constructor: ConstructorDeclaration, fieldName: AcceptedNameType, originalName: AcceptedNameType) {
224+
function updateReadonlyPropertyInitializerStatementConstructor(changeTracker: textChanges.ChangeTracker, file: SourceFile, constructor: ConstructorDeclaration, fieldName: string, originalName: string) {
225225
if (!constructor.body) return;
226-
const { file, program, cancellationToken } = context;
227-
228-
const referenceEntries = mapDefined(FindAllReferences.getReferenceEntriesForNode(originalName.parent.pos, originalName, program, [file], cancellationToken!), entry => // TODO: GH#18217
229-
(entry.kind !== FindAllReferences.EntryKind.Span && rangeContainsRange(constructor, entry.node) && isIdentifier(entry.node) && isWriteAccess(entry.node)) ? entry.node : undefined);
230-
231-
forEach(referenceEntries, entry => {
232-
const parent = entry.parent;
233-
const accessorName = createIdentifier(fieldName.text);
234-
const node = isBinaryExpression(parent)
235-
? updateBinary(parent, accessorName, parent.right, parent.operatorToken)
236-
: isPropertyAccessExpression(parent)
237-
? updatePropertyAccess(parent, parent.expression, accessorName)
238-
: Debug.fail("Unexpected write access token");
239-
changeTracker.replaceNode(file, parent, node);
226+
constructor.body.forEachChild(function recur(node) {
227+
if (isElementAccessExpression(node) &&
228+
node.expression.kind === SyntaxKind.ThisKeyword &&
229+
isStringLiteral(node.argumentExpression) &&
230+
node.argumentExpression.text === originalName &&
231+
isWriteAccess(node)) {
232+
changeTracker.replaceNode(file, node.argumentExpression, createStringLiteral(fieldName));
233+
}
234+
if (isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword && node.name.text === originalName && isWriteAccess(node)) {
235+
changeTracker.replaceNode(file, node.name, createIdentifier(fieldName));
236+
}
237+
if (!isFunctionLike(node) && !isClassLike(node)) {
238+
node.forEachChild(recur);
239+
}
240240
});
241241
}
242242
}

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess33.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//// public b: number;
66
//// constructor () {
77
//// this.a = 1; // convert
8-
//// this.a++; // convert
8+
//// this["a"]++; // convert
99
//// ++this.a; // convert
1010
//// if (Math.random()) {
1111
//// this.a = 2; // convert
@@ -29,7 +29,7 @@ edit.applyRefactor({
2929
public b: number;
3030
constructor () {
3131
this._a = 1; // convert
32-
this._a++; // convert
32+
this["_a"]++; // convert
3333
++this._a; // convert
3434
if (Math.random()) {
3535
this._a = 2; // convert

0 commit comments

Comments
 (0)