@@ -79,9 +79,8 @@ namespace ts.codefix {
79
79
* In such cases, we assume the declaration to be a `PropertySignature`.
80
80
*/
81
81
const kind = declaration ?. kind ?? SyntaxKind . PropertySignature ;
82
- const name = getSynthesizedDeepClone ( getNameOfDeclaration ( declaration ) , /*includeTrivia*/ false ) as PropertyName ;
82
+ const declarationName = getNameOfDeclaration ( declaration ) as PropertyName ;
83
83
const visibilityModifier = createVisibilityModifier ( declaration ? getEffectiveModifierFlags ( declaration ) : ModifierFlags . None ) ;
84
- const modifiers = visibilityModifier ? factory . createNodeArray ( [ visibilityModifier ] ) : undefined ;
85
84
const type = checker . getWidenedType ( checker . getTypeOfSymbolAtLocation ( symbol , enclosingDeclaration ) ) ;
86
85
const optional = ! ! ( symbol . flags & SymbolFlags . Optional ) ;
87
86
const ambient = ! ! ( enclosingDeclaration . flags & NodeFlags . Ambient ) || isAmbient ;
@@ -100,8 +99,8 @@ namespace ts.codefix {
100
99
}
101
100
}
102
101
addClassElement ( factory . createPropertyDeclaration (
103
- modifiers ,
104
- declaration ? name : symbol . getName ( ) ,
102
+ createModifiers ( visibilityModifier ) ,
103
+ declaration ? createName ( declarationName ) : symbol . getName ( ) ,
105
104
optional && ( preserveOptional & PreserveOptionalFlags . Property ) ? factory . createToken ( SyntaxKind . QuestionToken ) : undefined ,
106
105
typeNode ,
107
106
/*initializer*/ undefined ) ) ;
@@ -124,21 +123,21 @@ namespace ts.codefix {
124
123
for ( const accessor of orderedAccessors ) {
125
124
if ( isGetAccessorDeclaration ( accessor ) ) {
126
125
addClassElement ( factory . createGetAccessorDeclaration (
127
- modifiers ,
128
- name ,
126
+ createModifiers ( visibilityModifier ) ,
127
+ createName ( declarationName ) ,
129
128
emptyArray ,
130
- typeNode ,
131
- ambient ? undefined : body || createStubbedMethodBody ( quotePreference ) ) ) ;
129
+ createTypeNode ( typeNode ) ,
130
+ createBody ( body , quotePreference , ambient ) ) ) ;
132
131
}
133
132
else {
134
133
Debug . assertNode ( accessor , isSetAccessorDeclaration , "The counterpart to a getter should be a setter" ) ;
135
134
const parameter = getSetAccessorValueParameter ( accessor ) ;
136
135
const parameterName = parameter && isIdentifier ( parameter . name ) ? idText ( parameter . name ) : undefined ;
137
136
addClassElement ( factory . createSetAccessorDeclaration (
138
- modifiers ,
139
- name ,
140
- createDummyParameters ( 1 , [ parameterName ] , [ typeNode ] , 1 , /*inJs*/ false ) ,
141
- ambient ? undefined : body || createStubbedMethodBody ( quotePreference ) ) ) ;
137
+ createModifiers ( visibilityModifier ) ,
138
+ createName ( declarationName ) ,
139
+ createDummyParameters ( 1 , [ parameterName ] , [ createTypeNode ( typeNode ) ] , 1 , /*inJs*/ false ) ,
140
+ createBody ( body , quotePreference , ambient ) ) ) ;
142
141
}
143
142
}
144
143
break ;
@@ -161,23 +160,23 @@ namespace ts.codefix {
161
160
if ( declarations . length === 1 ) {
162
161
Debug . assert ( signatures . length === 1 , "One declaration implies one signature" ) ;
163
162
const signature = signatures [ 0 ] ;
164
- outputMethod ( quotePreference , signature , modifiers , name , ambient ? undefined : body || createStubbedMethodBody ( quotePreference ) ) ;
163
+ outputMethod ( quotePreference , signature , createModifiers ( visibilityModifier ) , createName ( declarationName ) , createBody ( body , quotePreference , ambient ) ) ;
165
164
break ;
166
165
}
167
166
168
167
for ( const signature of signatures ) {
169
168
// Ensure nodes are fresh so they can have different positions when going through formatting.
170
- outputMethod ( quotePreference , signature , getSynthesizedDeepClones ( modifiers , /*includeTrivia*/ false ) , getSynthesizedDeepClone ( name , /*includeTrivia*/ false ) ) ;
169
+ outputMethod ( quotePreference , signature , createModifiers ( visibilityModifier ) , createName ( declarationName ) ) ;
171
170
}
172
171
173
172
if ( ! ambient ) {
174
173
if ( declarations . length > signatures . length ) {
175
174
const signature = checker . getSignatureFromDeclaration ( declarations [ declarations . length - 1 ] as SignatureDeclaration ) ! ;
176
- outputMethod ( quotePreference , signature , modifiers , name , body || createStubbedMethodBody ( quotePreference ) ) ;
175
+ outputMethod ( quotePreference , signature , createModifiers ( visibilityModifier ) , createName ( declarationName ) , createBody ( body , quotePreference ) ) ;
177
176
}
178
177
else {
179
178
Debug . assert ( declarations . length === signatures . length , "Declarations and signatures should match count" ) ;
180
- addClassElement ( createMethodImplementingSignatures ( checker , context , enclosingDeclaration , signatures , name , optional && ! ! ( preserveOptional & PreserveOptionalFlags . Method ) , modifiers , quotePreference , body ) ) ;
179
+ addClassElement ( createMethodImplementingSignatures ( checker , context , enclosingDeclaration , signatures , createName ( declarationName ) , optional && ! ! ( preserveOptional & PreserveOptionalFlags . Method ) , createModifiers ( visibilityModifier ) , quotePreference , body ) ) ;
181
180
}
182
181
}
183
182
break ;
@@ -187,6 +186,23 @@ namespace ts.codefix {
187
186
const method = createSignatureDeclarationFromSignature ( SyntaxKind . MethodDeclaration , context , quotePreference , signature , body , name , modifiers , optional && ! ! ( preserveOptional & PreserveOptionalFlags . Method ) , enclosingDeclaration , importAdder ) as MethodDeclaration ;
188
187
if ( method ) addClassElement ( method ) ;
189
188
}
189
+
190
+ function createName ( node : PropertyName ) {
191
+ return getSynthesizedDeepClone ( node , /*includeTrivia*/ false ) ;
192
+ }
193
+
194
+ function createModifiers ( modifier : Modifier | undefined ) {
195
+ return modifier ? factory . createNodeArray ( [ modifier ] ) : undefined ;
196
+ }
197
+
198
+ function createBody ( block : Block | undefined , quotePreference : QuotePreference , ambient ?: boolean ) {
199
+ return ambient ? undefined :
200
+ getSynthesizedDeepClone ( block , /*includeTrivia*/ false ) || createStubbedMethodBody ( quotePreference ) ;
201
+ }
202
+
203
+ function createTypeNode ( typeNode : TypeNode | undefined ) {
204
+ return getSynthesizedDeepClone ( typeNode , /*includeTrivia*/ false ) ;
205
+ }
190
206
}
191
207
192
208
export function createSignatureDeclarationFromSignature (
0 commit comments