@@ -16,6 +16,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
16
16
declaration : AcceptedDeclaration ;
17
17
fieldName : AcceptedNameType ;
18
18
accessorName : AcceptedNameType ;
19
+ originalName : AcceptedNameType ;
19
20
}
20
21
21
22
function getAvailableActions ( context : RefactorContext ) : ApplicableRefactorInfo [ ] | undefined {
@@ -42,7 +43,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
42
43
43
44
const isJS = isSourceFileJavaScript ( file ) ;
44
45
const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
45
- const { isStatic, isReadonly, fieldName, accessorName, type, container, declaration } = fieldInfo ;
46
+ const { isStatic, isReadonly, fieldName, accessorName, originalName , type, container, declaration } = fieldInfo ;
46
47
47
48
suppressLeadingAndTrailingTrivia ( fieldName ) ;
48
49
suppressLeadingAndTrailingTrivia ( declaration ) ;
@@ -68,7 +69,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
68
69
// readonly modifier only existed in classLikeDeclaration
69
70
const constructor = getFirstConstructorWithBody ( < ClassLikeDeclaration > container ) ;
70
71
if ( constructor ) {
71
- updateReadonlyPropertyInitializerStatementConstructor ( changeTracker , file , constructor , accessorName , fieldName ) ;
72
+ updateReadonlyPropertyInitializerStatementConstructor ( changeTracker , context , constructor , fieldName , originalName ) ;
72
73
}
73
74
}
74
75
else {
@@ -123,6 +124,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
123
124
isReadonly : hasReadonlyModifier ( declaration ) ,
124
125
type : getTypeAnnotationNode ( declaration ) ,
125
126
container : declaration . kind === SyntaxKind . Parameter ? declaration . parent . parent : declaration . parent ,
127
+ originalName : < AcceptedNameType > declaration . name ,
126
128
declaration,
127
129
fieldName,
128
130
accessorName,
@@ -205,37 +207,23 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
205
207
: changeTracker . insertNodeAfter ( file , declaration , accessor ) ;
206
208
}
207
209
208
- function updateReadonlyPropertyInitializerStatementConstructor ( changeTracker : textChanges . ChangeTracker , file : SourceFile , constructor : ConstructorDeclaration , accessorName : AcceptedNameType , fieldName : AcceptedNameType ) {
209
- if ( constructor . body ) {
210
- const initializerStatement = find ( constructor . body . statements , ( stmt =>
211
- isExpressionStatement ( stmt ) &&
212
- isAssignmentExpression ( stmt . expression ) &&
213
- stmt . expression . operatorToken . kind === SyntaxKind . EqualsToken &&
214
- ( isPropertyAccessExpression ( stmt . expression . left ) || isElementAccessExpression ( stmt . expression . left ) ) &&
215
- isThis ( stmt . expression . left . expression ) &&
216
- ( isPropertyAccessExpression ( stmt . expression . left )
217
- ? ( getNameFromPropertyName ( stmt . expression . left . name ) === accessorName . text )
218
- : ( isPropertyName ( stmt . expression . left . argumentExpression ) && isConvertableName ( stmt . expression . left . argumentExpression ) && getNameFromPropertyName ( stmt . expression . left . argumentExpression ) === accessorName . text
219
- ) )
220
- ) ) ;
221
- if ( initializerStatement ) {
222
- const initializerLeftHead = ( < PropertyAccessExpression | ElementAccessExpression > ( < AssignmentExpression < Token < SyntaxKind . EqualsToken > > > ( < ExpressionStatement > initializerStatement ) . expression ) . left ) ;
223
-
224
- if ( isPropertyAccessExpression ( initializerLeftHead ) ) {
225
- changeTracker . replaceNode ( file , initializerLeftHead , updatePropertyAccess (
226
- initializerLeftHead ,
227
- initializerLeftHead . expression ,
228
- createIdentifier ( fieldName . text )
229
- ) ) ;
230
- }
231
- else {
232
- changeTracker . replaceNode ( file , initializerLeftHead , updateElementAccess (
233
- initializerLeftHead ,
234
- initializerLeftHead . expression ,
235
- createPropertyName ( fieldName . text , < AcceptedNameType > initializerLeftHead . argumentExpression )
236
- ) ) ;
237
- }
238
- }
239
- }
210
+ function updateReadonlyPropertyInitializerStatementConstructor ( changeTracker : textChanges . ChangeTracker , context : RefactorContext , constructor : ConstructorDeclaration , fieldName : AcceptedNameType , originalName : AcceptedNameType ) {
211
+ if ( ! constructor . body ) return ;
212
+ const { file, program, cancellationToken } = context ;
213
+
214
+ const referenceEntries = mapDefined ( FindAllReferences . getReferenceEntriesForNode ( - 1 , originalName , program , [ file ] , cancellationToken ) , entry => (
215
+ ( entry . type === "node" && rangeContainsRange ( constructor , entry . node ) && isIdentifier ( entry . node ) && isWriteAccess ( entry . node ) ) ? entry . node : undefined
216
+ ) ) ;
217
+
218
+ forEach ( referenceEntries , entry => {
219
+ const parent = entry . parent ;
220
+ const accessorName = createIdentifier ( fieldName . text ) ;
221
+ const node = isBinaryExpression ( parent )
222
+ ? updateBinary ( parent , accessorName , parent . right , parent . operatorToken )
223
+ : isPropertyAccessExpression ( parent )
224
+ ? updatePropertyAccess ( parent , parent . expression , accessorName )
225
+ : Debug . fail ( "Unexpected write access token" ) ;
226
+ changeTracker . replaceNode ( file , parent , node ) ;
227
+ } ) ;
240
228
}
241
229
}
0 commit comments