@@ -17,6 +17,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
17
17
fieldName : AcceptedNameType ;
18
18
accessorName : AcceptedNameType ;
19
19
originalName : AcceptedNameType ;
20
+ renameAccessor : boolean ;
20
21
}
21
22
22
23
function getAvailableActions ( context : RefactorContext ) : ApplicableRefactorInfo [ ] | undefined {
@@ -43,7 +44,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
43
44
44
45
const isJS = isSourceFileJavaScript ( file ) ;
45
46
const changeTracker = textChanges . ChangeTracker . fromContext ( context ) ;
46
- const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration } = fieldInfo ;
47
+ const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration, renameAccessor } = fieldInfo ;
47
48
48
49
suppressLeadingAndTrailingTrivia ( fieldName ) ;
49
50
suppressLeadingAndTrailingTrivia ( declaration ) ;
@@ -80,8 +81,10 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
80
81
81
82
const edits = changeTracker . getChanges ( ) ;
82
83
const renameFilename = file . fileName ;
83
- const renameLocationOffset = isIdentifier ( fieldName ) ? 0 : - 1 ;
84
- const renameLocation = renameLocationOffset + getRenameLocation ( edits , renameFilename , fieldName . text , /*preferLastLocation*/ isParameter ( declaration ) ) ;
84
+
85
+ const nameNeedRename = renameAccessor ? accessorName : fieldName ;
86
+ const renameLocationOffset = isIdentifier ( nameNeedRename ) ? 0 : - 1 ;
87
+ const renameLocation = renameLocationOffset + getRenameLocation ( edits , renameFilename , nameNeedRename . text , /*preferLastLocation*/ isParameter ( declaration ) ) ;
85
88
return { renameFilename, renameLocation, edits } ;
86
89
}
87
90
@@ -110,15 +113,21 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
110
113
return modifiers && createNodeArray ( modifiers ) ;
111
114
}
112
115
116
+ function startsWithUnderscore ( name : string ) : boolean {
117
+ return name . charCodeAt ( 0 ) === CharacterCodes . _ ;
118
+ }
119
+
113
120
function getConvertibleFieldAtPosition ( file : SourceFile , startPosition : number ) : Info | undefined {
114
121
const node = getTokenAtPosition ( file , startPosition , /*includeJsDocComment*/ false ) ;
115
122
const declaration = findAncestor ( node . parent , isAcceptedDeclaration ) ;
116
123
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
117
124
const meaning = ModifierFlags . AccessibilityModifier | ModifierFlags . Static | ModifierFlags . Readonly ;
118
125
if ( ! declaration || ! isConvertableName ( declaration . name ) || ( getModifierFlags ( declaration ) | meaning ) !== meaning ) return undefined ;
119
126
120
- const fieldName = createPropertyName ( getUniqueName ( `_${ declaration . name . text } ` , file . text ) , declaration . name ) ;
121
- const accessorName = createPropertyName ( declaration . name . text , declaration . name ) ;
127
+ const name = declaration . name . text ;
128
+ const startWithUnderscore = startsWithUnderscore ( name ) ;
129
+ const fieldName = createPropertyName ( startWithUnderscore ? name : getUniqueName ( `_${ name } ` , file . text ) , declaration . name ) ;
130
+ const accessorName = createPropertyName ( startWithUnderscore ? getUniqueName ( name . substring ( 1 ) , file . text ) : name , declaration . name ) ;
122
131
return {
123
132
isStatic : hasStaticModifier ( declaration ) ,
124
133
isReadonly : hasReadonlyModifier ( declaration ) ,
@@ -128,6 +137,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
128
137
declaration,
129
138
fieldName,
130
139
accessorName,
140
+ renameAccessor : startWithUnderscore
131
141
} ;
132
142
}
133
143
0 commit comments