@@ -23,45 +23,67 @@ namespace ts.codefix {
23
23
return undefined ;
24
24
}
25
25
26
- if ( ! ( token . parent && token . parent . kind === SyntaxKind . PropertyAccessExpression ) ) {
26
+ if ( ! isPropertyAccessExpression ( token . parent ) || token . parent . expression . kind !== SyntaxKind . ThisKeyword ) {
27
27
return undefined ;
28
28
}
29
29
30
- if ( ( token . parent as PropertyAccessExpression ) . expression . kind !== SyntaxKind . ThisKeyword ) {
31
- return undefined ;
32
- }
30
+ return isInJavaScriptFile ( sourceFile ) ? getActionsForAddMissingMemberInJavaScriptFile ( ) : getActionsForAddMissingMemberInTypeScriptFile ( ) ;
33
31
34
- let typeString = "any" ;
35
32
36
- if ( token . parent . parent . kind === SyntaxKind . BinaryExpression ) {
37
- const binaryExpression = token . parent . parent as BinaryExpression ;
33
+ function getActionsForAddMissingMemberInTypeScriptFile ( ) : CodeAction [ ] | undefined {
34
+ let typeString = "any" ;
38
35
39
- const checker = context . program . getTypeChecker ( ) ;
40
- const widenedType = checker . getWidenedType ( checker . getBaseTypeOfLiteralType ( checker . getTypeAtLocation ( binaryExpression . right ) ) ) ;
41
- typeString = checker . typeToString ( widenedType ) ;
42
- }
36
+ if ( token . parent . parent . kind === SyntaxKind . BinaryExpression ) {
37
+ const binaryExpression = token . parent . parent as BinaryExpression ;
43
38
44
- const startPos = classDeclaration . members . pos ;
39
+ const checker = context . program . getTypeChecker ( ) ;
40
+ const widenedType = checker . getWidenedType ( checker . getBaseTypeOfLiteralType ( checker . getTypeAtLocation ( binaryExpression . right ) ) ) ;
41
+ typeString = checker . typeToString ( widenedType ) ;
42
+ }
45
43
46
- return [ {
47
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
48
- changes : [ {
49
- fileName : sourceFile . fileName ,
50
- textChanges : [ {
51
- span : { start : startPos , length : 0 } ,
52
- newText : `${ token . getFullText ( sourceFile ) } : ${ typeString } ;`
44
+ const startPos = classDeclaration . members . pos ;
45
+
46
+ return [ {
47
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
48
+ changes : [ {
49
+ fileName : sourceFile . fileName ,
50
+ textChanges : [ {
51
+ span : { start : startPos , length : 0 } ,
52
+ newText : `${ token . getFullText ( sourceFile ) } : ${ typeString } ;`
53
+ } ]
54
+ } ]
55
+ } ,
56
+ {
57
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_index_signature_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
58
+ changes : [ {
59
+ fileName : sourceFile . fileName ,
60
+ textChanges : [ {
61
+ span : { start : startPos , length : 0 } ,
62
+ newText : `[name: string]: ${ typeString } ;`
63
+ } ]
53
64
} ]
54
- } ]
55
- } ,
56
- {
57
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_index_signature_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
58
- changes : [ {
59
- fileName : sourceFile . fileName ,
60
- textChanges : [ {
61
- span : { start : startPos , length : 0 } ,
62
- newText : `[name: string]: ${ typeString } ;`
65
+ } ] ;
66
+ }
67
+
68
+ function getActionsForAddMissingMemberInJavaScriptFile ( ) : CodeAction [ ] | undefined {
69
+ const classConstructor = getFirstConstructorWithBody ( classDeclaration ) ;
70
+ if ( ! classConstructor ) {
71
+ return undefined ;
72
+ }
73
+
74
+ const memberName = token . getText ( ) ;
75
+ const startPos = classConstructor . body . getEnd ( ) - 1 ;
76
+
77
+ return [ {
78
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Initialize_property_0_in_the_constructor ) , [ memberName ] ) ,
79
+ changes : [ {
80
+ fileName : sourceFile . fileName ,
81
+ textChanges : [ {
82
+ span : { start : startPos , length : 0 } ,
83
+ newText : `this.${ memberName } = undefined;`
84
+ } ]
63
85
} ]
64
- } ]
65
- } ] ;
86
+ } ] ;
87
+ }
66
88
}
67
89
}
0 commit comments