@@ -14,48 +14,31 @@ namespace ts.codefix {
14
14
15
15
function doChange ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , position : number , checker : TypeChecker ) : void {
16
16
const ctorSymbol = checker . getSymbolAtLocation ( getTokenAtPosition ( sourceFile , position ) ) ! ;
17
-
18
17
if ( ! ctorSymbol || ! ( ctorSymbol . flags & ( SymbolFlags . Function | SymbolFlags . Variable ) ) ) {
19
18
// Bad input
20
19
return undefined ;
21
20
}
22
21
23
22
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 ) ) ;
49
25
}
26
+ else if ( isVariableDeclaration ( ctorDeclaration ) ) {
27
+ const classDeclaration = createClassFromVariableDeclaration ( ctorDeclaration ) ;
28
+ if ( ! classDeclaration ) {
29
+ return undefined ;
30
+ }
50
31
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
+ }
54
40
}
55
41
56
- // Because the preceding node could be touched, we need to insert nodes before delete nodes.
57
- changes . insertNodeAfter ( sourceFile , precedingNode ! , newClassDeclaration ) ;
58
-
59
42
function createClassElementsFromSymbol ( symbol : Symbol ) {
60
43
const memberElements : ClassElement [ ] = [ ] ;
61
44
// all instance members are stored in the "member" array of symbol
@@ -220,12 +203,8 @@ namespace ts.codefix {
220
203
}
221
204
222
205
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 ) ) {
229
208
return undefined ;
230
209
}
231
210
@@ -234,7 +213,7 @@ namespace ts.codefix {
234
213
memberElements . unshift ( factory . createConstructorDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , initializer . parameters , initializer . body ) ) ;
235
214
}
236
215
237
- const modifiers = getModifierKindFromSource ( precedingNode ! , SyntaxKind . ExportKeyword ) ;
216
+ const modifiers = getModifierKindFromSource ( node . parent . parent , SyntaxKind . ExportKeyword ) ;
238
217
const cls = factory . createClassDeclaration ( /*decorators*/ undefined , modifiers , node . name ,
239
218
/*typeParameters*/ undefined , /*heritageClauses*/ undefined , memberElements ) ;
240
219
// Don't call copyComments here because we'll already leave them in place
0 commit comments