@@ -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,40 @@ 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
200
return createProperty ( /*decorators*/ undefined , modifiers , memberDeclaration . name , /*questionToken*/ undefined ,
195
201
/*type*/ undefined , assignmentBinaryExpression . right ) ;
202
+ }
196
203
}
197
204
}
198
205
}
199
206
207
+ function copyComments ( sourceNode : Node , targetNode : Node ) {
208
+ forEachLeadingCommentRange ( sourceFile . text , sourceNode . pos , ( pos , end , kind , htnl ) => {
209
+ if ( kind === SyntaxKind . MultiLineCommentTrivia ) {
210
+ // Remove leading /*
211
+ pos += 2 ;
212
+ // Remove trailing */
213
+ end -= 2 ;
214
+ }
215
+ else {
216
+ // Remove leading //
217
+ pos += 2 ;
218
+ }
219
+ addSyntheticLeadingComment ( targetNode , kind , sourceFile . text . slice ( pos , end ) , htnl ) ;
220
+ } ) ;
221
+ }
222
+
200
223
function createClassFromVariableDeclaration ( node : VariableDeclaration ) : ClassDeclaration {
201
224
const initializer = node . initializer as FunctionExpression ;
202
225
if ( ! initializer || initializer . kind !== SyntaxKind . FunctionExpression ) {
@@ -212,17 +235,22 @@ namespace ts.refactor {
212
235
memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , initializer . parameters , initializer . body ) ) ;
213
236
}
214
237
215
- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
238
+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
216
239
/*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
240
+ // Don't call copyComments here because we'll already leave them in place
241
+ return cls ;
217
242
}
218
243
219
244
function createClassFromFunctionDeclaration ( node : FunctionDeclaration ) : ClassDeclaration {
220
245
const memberElements = createClassElementsFromSymbol ( ctorSymbol ) ;
221
246
if ( node . body ) {
222
247
memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , node . parameters , node . body ) ) ;
223
248
}
224
- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
249
+
250
+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
225
251
/*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
252
+ // Don't call copyComments here because we'll already leave them in place
253
+ return cls ;
226
254
}
227
255
}
228
256
}
0 commit comments