@@ -164,12 +164,15 @@ namespace ts.refactor {
164
164
}
165
165
166
166
switch ( assignmentBinaryExpression . right . kind ) {
167
- case SyntaxKind . FunctionExpression :
167
+ case SyntaxKind . FunctionExpression : {
168
168
const functionExpression = assignmentBinaryExpression . right as FunctionExpression ;
169
- return createMethod ( /*decorators*/ undefined , modifiers , /*asteriskToken*/ undefined , memberDeclaration . name , /*questionToken*/ undefined ,
169
+ const method = createMethod ( /*decorators*/ undefined , modifiers , /*asteriskToken*/ undefined , memberDeclaration . name , /*questionToken*/ undefined ,
170
170
/*typeParameters*/ undefined , functionExpression . parameters , /*type*/ undefined , functionExpression . body ) ;
171
+ copyComments ( assignmentBinaryExpression , method ) ;
172
+ return method ;
173
+ }
171
174
172
- case SyntaxKind . ArrowFunction :
175
+ case SyntaxKind . ArrowFunction : {
173
176
const arrowFunction = assignmentBinaryExpression . right as ArrowFunction ;
174
177
const arrowFunctionBody = arrowFunction . body ;
175
178
let bodyBlock : Block ;
@@ -183,20 +186,42 @@ namespace ts.refactor {
183
186
const expression = arrowFunctionBody as Expression ;
184
187
bodyBlock = createBlock ( [ createReturn ( expression ) ] ) ;
185
188
}
186
- return createMethod ( /*decorators*/ undefined , modifiers , /*asteriskToken*/ undefined , memberDeclaration . name , /*questionToken*/ undefined ,
189
+ const method = createMethod ( /*decorators*/ undefined , modifiers , /*asteriskToken*/ undefined , memberDeclaration . name , /*questionToken*/ undefined ,
187
190
/*typeParameters*/ undefined , arrowFunction . parameters , /*type*/ undefined , bodyBlock ) ;
191
+ copyComments ( assignmentBinaryExpression , method ) ;
192
+ return method ;
193
+ }
188
194
189
- default :
195
+ default : {
190
196
// Don't try to declare members in JavaScript files
191
197
if ( isSourceFileJavaScript ( sourceFile ) ) {
192
198
return ;
193
199
}
194
- return createProperty ( /*decorators*/ undefined , modifiers , memberDeclaration . name , /*questionToken*/ undefined ,
200
+ const prop = createProperty ( /*decorators*/ undefined , modifiers , memberDeclaration . name , /*questionToken*/ undefined ,
195
201
/*type*/ undefined , assignmentBinaryExpression . right ) ;
202
+ copyComments ( assignmentBinaryExpression . parent , prop ) ;
203
+ return prop ;
204
+ }
196
205
}
197
206
}
198
207
}
199
208
209
+ function copyComments ( sourceNode : Node , targetNode : Node ) {
210
+ forEachLeadingCommentRange ( sourceFile . text , sourceNode . pos , ( pos , end , kind , htnl ) => {
211
+ if ( kind === SyntaxKind . MultiLineCommentTrivia ) {
212
+ // Remove leading /*
213
+ pos += 2 ;
214
+ // Remove trailing */
215
+ end -= 2 ;
216
+ }
217
+ else {
218
+ // Remove leading //
219
+ pos += 2 ;
220
+ }
221
+ addSyntheticLeadingComment ( targetNode , kind , sourceFile . text . slice ( pos , end ) , htnl ) ;
222
+ } ) ;
223
+ }
224
+
200
225
function createClassFromVariableDeclaration ( node : VariableDeclaration ) : ClassDeclaration {
201
226
const initializer = node . initializer as FunctionExpression ;
202
227
if ( ! initializer || initializer . kind !== SyntaxKind . FunctionExpression ) {
@@ -212,17 +237,22 @@ namespace ts.refactor {
212
237
memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , initializer . parameters , initializer . body ) ) ;
213
238
}
214
239
215
- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
240
+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
216
241
/*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
242
+ // Don't call copyComments here because we'll already leave them in place
243
+ return cls ;
217
244
}
218
245
219
246
function createClassFromFunctionDeclaration ( node : FunctionDeclaration ) : ClassDeclaration {
220
247
const memberElements = createClassElementsFromSymbol ( ctorSymbol ) ;
221
248
if ( node . body ) {
222
249
memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , node . parameters , node . body ) ) ;
223
250
}
224
- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
251
+
252
+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
225
253
/*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
254
+ // Don't call copyComments here because we'll already leave them in place
255
+ return cls ;
226
256
}
227
257
}
228
258
}
0 commit comments