@@ -164,12 +164,15 @@ namespace ts.refactor {
164164 }
165165
166166 switch ( assignmentBinaryExpression . right . kind ) {
167- case SyntaxKind . FunctionExpression :
167+ case SyntaxKind . FunctionExpression : {
168168 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 ,
170170 /*typeParameters*/ undefined , functionExpression . parameters , /*type*/ undefined , functionExpression . body ) ;
171+ copyComments ( assignmentBinaryExpression , method ) ;
172+ return method ;
173+ }
171174
172- case SyntaxKind . ArrowFunction :
175+ case SyntaxKind . ArrowFunction : {
173176 const arrowFunction = assignmentBinaryExpression . right as ArrowFunction ;
174177 const arrowFunctionBody = arrowFunction . body ;
175178 let bodyBlock : Block ;
@@ -183,20 +186,42 @@ namespace ts.refactor {
183186 const expression = arrowFunctionBody as Expression ;
184187 bodyBlock = createBlock ( [ createReturn ( expression ) ] ) ;
185188 }
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 ,
187190 /*typeParameters*/ undefined , arrowFunction . parameters , /*type*/ undefined , bodyBlock ) ;
191+ copyComments ( assignmentBinaryExpression , method ) ;
192+ return method ;
193+ }
188194
189- default :
195+ default : {
190196 // Don't try to declare members in JavaScript files
191197 if ( isSourceFileJavaScript ( sourceFile ) ) {
192198 return ;
193199 }
194- return createProperty ( /*decorators*/ undefined , modifiers , memberDeclaration . name , /*questionToken*/ undefined ,
200+ const prop = createProperty ( /*decorators*/ undefined , modifiers , memberDeclaration . name , /*questionToken*/ undefined ,
195201 /*type*/ undefined , assignmentBinaryExpression . right ) ;
202+ copyComments ( assignmentBinaryExpression . parent , prop ) ;
203+ return prop ;
204+ }
196205 }
197206 }
198207 }
199208
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+
200225 function createClassFromVariableDeclaration ( node : VariableDeclaration ) : ClassDeclaration {
201226 const initializer = node . initializer as FunctionExpression ;
202227 if ( ! initializer || initializer . kind !== SyntaxKind . FunctionExpression ) {
@@ -212,17 +237,22 @@ namespace ts.refactor {
212237 memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , initializer . parameters , initializer . body ) ) ;
213238 }
214239
215- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
240+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
216241 /*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
242+ // Don't call copyComments here because we'll already leave them in place
243+ return cls ;
217244 }
218245
219246 function createClassFromFunctionDeclaration ( node : FunctionDeclaration ) : ClassDeclaration {
220247 const memberElements = createClassElementsFromSymbol ( ctorSymbol ) ;
221248 if ( node . body ) {
222249 memberElements . unshift ( createConstructor ( /*decorators*/ undefined , /*modifiers*/ undefined , node . parameters , node . body ) ) ;
223250 }
224- return createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
251+
252+ const cls = createClassDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , node . name ,
225253 /*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
254+ // Don't call copyComments here because we'll already leave them in place
255+ return cls ;
226256 }
227257 }
228258}
0 commit comments