@@ -14,48 +14,31 @@ namespace ts.codefix {
1414
1515 function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , position : number , checker : TypeChecker ) : void {
1616 const ctorSymbol = checker . getSymbolAtLocation ( getTokenAtPosition ( sourceFile , position ) ) ! ;
17-
1817 if ( ! ctorSymbol || ! ( ctorSymbol . flags & ( SymbolFlags . Function | SymbolFlags . Variable ) ) ) {
1918 // Bad input
2019 return undefined ;
2120 }
2221
2322 const ctorDeclaration = ctorSymbol . valueDeclaration ;
24-
25- let precedingNode : Node | undefined ;
26- let newClassDeclaration : ClassDeclaration | undefined ;
27- switch ( ctorDeclaration . kind ) {
28- case SyntaxKind . FunctionDeclaration :
29- precedingNode = ctorDeclaration ;
30- changes . delete ( sourceFile , ctorDeclaration ) ;
31- newClassDeclaration = createClassFromFunctionDeclaration ( ctorDeclaration as FunctionDeclaration ) ;
32- break ;
33-
34- case SyntaxKind . VariableDeclaration :
35- precedingNode = ctorDeclaration . parent . parent ;
36- newClassDeclaration = createClassFromVariableDeclaration ( ctorDeclaration as VariableDeclaration ) ;
37- if ( ( < VariableDeclarationList > ctorDeclaration . parent ) . declarations . length === 1 ) {
38- copyLeadingComments ( precedingNode , newClassDeclaration ! , sourceFile ) ; // TODO: GH#18217
39- changes . delete ( sourceFile , precedingNode ) ;
40- }
41- else {
42- changes . delete ( sourceFile , ctorDeclaration ) ;
43- }
44- break ;
45- }
46-
47- if ( ! newClassDeclaration ) {
48- return undefined ;
23+ if ( isFunctionDeclaration ( ctorDeclaration ) ) {
24+ changes . replaceNode ( sourceFile , ctorDeclaration , createClassFromFunctionDeclaration ( ctorDeclaration ) ) ;
4925 }
26+ else if ( isVariableDeclaration ( ctorDeclaration ) ) {
27+ const classDeclaration = createClassFromVariableDeclaration ( ctorDeclaration ) ;
28+ if ( ! classDeclaration ) {
29+ return undefined ;
30+ }
5031
51- // Deleting a declaration only deletes JSDoc style comments, so only copy those to the new node.
52- if ( hasJSDocNodes ( ctorDeclaration ) ) {
53- copyLeadingComments ( ctorDeclaration , newClassDeclaration , sourceFile ) ;
32+ const ancestor = ctorDeclaration . parent . parent ;
33+ if ( isVariableDeclarationList ( ctorDeclaration . parent ) && ctorDeclaration . parent . declarations . length > 1 ) {
34+ changes . delete ( sourceFile , ctorDeclaration ) ;
35+ changes . insertNodeAfter ( sourceFile , ancestor , classDeclaration ) ;
36+ }
37+ else {
38+ changes . replaceNode ( sourceFile , ancestor , classDeclaration ) ;
39+ }
5440 }
5541
56- // Because the preceding node could be touched, we need to insert nodes before delete nodes.
57- changes . insertNodeAfter ( sourceFile , precedingNode ! , newClassDeclaration ) ;
58-
5942 function createClassElementsFromSymbol ( symbol : Symbol ) {
6043 const memberElements : ClassElement [ ] = [ ] ;
6144 // all instance members are stored in the "member" array of symbol
@@ -220,12 +203,8 @@ namespace ts.codefix {
220203 }
221204
222205 function createClassFromVariableDeclaration ( node : VariableDeclaration ) : ClassDeclaration | undefined {
223- const initializer = node . initializer as FunctionExpression ;
224- if ( ! initializer || initializer . kind !== SyntaxKind . FunctionExpression ) {
225- return undefined ;
226- }
227-
228- if ( node . name . kind !== SyntaxKind . Identifier ) {
206+ const initializer = node . initializer ;
207+ if ( ! initializer || ! isFunctionExpression ( initializer ) || ! isIdentifier ( node . name ) ) {
229208 return undefined ;
230209 }
231210
@@ -234,7 +213,7 @@ namespace ts.codefix {
234213 memberElements . unshift ( factory . createConstructorDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , initializer . parameters , initializer . body ) ) ;
235214 }
236215
237- const modifiers = getModifierKindFromSource ( precedingNode ! , SyntaxKind . ExportKeyword ) ;
216+ const modifiers = getModifierKindFromSource ( node . parent . parent , SyntaxKind . ExportKeyword ) ;
238217 const cls = factory . createClassDeclaration ( /*decorators*/ undefined , modifiers , node . name ,
239218 /*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
240219 // Don't call copyComments here because we'll already leave them in place
0 commit comments