2
2
namespace ts . codefix {
3
3
registerCodeFix ( {
4
4
errorCodes : [ Diagnostics . Property_0_does_not_exist_on_type_1 . code ,
5
- Diagnostics . Property_0_does_not_exist_on_type_1_Did_you_mean_2 . code ] ,
5
+ Diagnostics . Property_0_does_not_exist_on_type_1_Did_you_mean_2 . code ] ,
6
6
getCodeActions : getActionsForAddMissingMember
7
7
} ) ;
8
8
@@ -33,13 +33,12 @@ namespace ts.codefix {
33
33
return undefined ;
34
34
}
35
35
36
+ const tokenName = token . getText ( sourceFile ) ;
36
37
const isStatic = hasModifier ( classMemberDeclaration , ModifierFlags . Static ) ;
37
38
38
39
return isInJavaScriptFile ( sourceFile ) ? getActionsForAddMissingMemberInJavaScriptFile ( ) : getActionsForAddMissingMemberInTypeScriptFile ( ) ;
39
40
40
41
function getActionsForAddMissingMemberInJavaScriptFile ( ) : CodeAction [ ] | undefined {
41
- const memberName = token . getText ( ) ;
42
-
43
42
if ( isStatic ) {
44
43
if ( classDeclaration . kind === SyntaxKind . ClassExpression ) {
45
44
return undefined ;
@@ -48,12 +47,12 @@ namespace ts.codefix {
48
47
const className = classDeclaration . name . getText ( ) ;
49
48
50
49
return [ {
51
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Initialize_static_property_0 ) , [ memberName ] ) ,
50
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Initialize_static_property_0 ) , [ tokenName ] ) ,
52
51
changes : [ {
53
52
fileName : sourceFile . fileName ,
54
53
textChanges : [ {
55
54
span : { start : classDeclaration . getEnd ( ) , length : 0 } ,
56
- newText : `${ context . newLineCharacter } ${ className } .${ memberName } = undefined;${ context . newLineCharacter } `
55
+ newText : `${ context . newLineCharacter } ${ className } .${ tokenName } = undefined;${ context . newLineCharacter } `
57
56
} ]
58
57
} ]
59
58
} ] ;
@@ -66,12 +65,12 @@ namespace ts.codefix {
66
65
}
67
66
68
67
return [ {
69
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Initialize_property_0_in_the_constructor ) , [ memberName ] ) ,
68
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Initialize_property_0_in_the_constructor ) , [ tokenName ] ) ,
70
69
changes : [ {
71
70
fileName : sourceFile . fileName ,
72
71
textChanges : [ {
73
72
span : { start : classConstructor . body . getEnd ( ) - 1 , length : 0 } ,
74
- newText : `this.${ memberName } = undefined;${ context . newLineCharacter } `
73
+ newText : `this.${ tokenName } = undefined;${ context . newLineCharacter } `
75
74
} ]
76
75
} ]
77
76
} ] ;
@@ -80,7 +79,6 @@ namespace ts.codefix {
80
79
81
80
function getActionsForAddMissingMemberInTypeScriptFile ( ) : CodeAction [ ] | undefined {
82
81
const openBrace = getOpenBraceOfClassLike ( classDeclaration , sourceFile ) ;
83
- const tokenName = token . getText ( sourceFile ) ;
84
82
let actions : CodeAction [ ] ;
85
83
86
84
if ( token . parent . parent . kind === SyntaxKind . CallExpression ) {
@@ -107,15 +105,15 @@ namespace ts.codefix {
107
105
const property = createProperty (
108
106
/*decorators*/ undefined ,
109
107
/*modifiers*/ isStatic ? [ createToken ( SyntaxKind . StaticKeyword ) ] : undefined ,
110
- token . getText ( sourceFile ) ,
108
+ tokenName ,
111
109
/*questionToken*/ undefined ,
112
110
typeNode ,
113
111
/*initializer*/ undefined ) ;
114
112
const propertyChangeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context ) ;
115
113
propertyChangeTracker . insertNodeAfter ( sourceFile , openBrace , property , { suffix : context . newLineCharacter } ) ;
116
114
117
115
( actions || ( actions = [ ] ) ) . push ( {
118
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
116
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ tokenName ] ) ,
119
117
changes : propertyChangeTracker . getChanges ( )
120
118
} ) ;
121
119
@@ -139,7 +137,7 @@ namespace ts.codefix {
139
137
indexSignatureChangeTracker . insertNodeAfter ( sourceFile , openBrace , indexSignature , { suffix : context . newLineCharacter } ) ;
140
138
141
139
actions . push ( {
142
- description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_index_signature_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
140
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_index_signature_for_missing_property_0 ) , [ tokenName ] ) ,
143
141
changes : indexSignatureChangeTracker . getChanges ( )
144
142
} ) ;
145
143
}
0 commit comments