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
@@ -79,20 +79,31 @@ namespace ts.codefix {
79
79
}
80
80
81
81
function getActionsForAddMissingMemberInTypeScriptFile ( ) : CodeAction [ ] | undefined {
82
- let typeNode : TypeNode ;
82
+ const openBrace = getOpenBraceOfClassLike ( classDeclaration , sourceFile ) ;
83
+ const tokenName = token . getText ( sourceFile ) ;
84
+ let actions : CodeAction [ ] ;
85
+
86
+ if ( token . parent . parent . kind === SyntaxKind . CallExpression ) {
87
+ const callExpression = < CallExpression > token . parent . parent ;
88
+ const methodDeclaration = createMethodFromCallExpression ( callExpression , tokenName ) ;
89
+
90
+ const methodDeclarationChangeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context ) ;
91
+ methodDeclarationChangeTracker . insertNodeAfter ( sourceFile , openBrace , methodDeclaration , { suffix : context . newLineCharacter } ) ;
92
+ actions = [ {
93
+ description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_method_0 ) , [ tokenName ] ) ,
94
+ changes : methodDeclarationChangeTracker . getChanges ( )
95
+ } ] ;
96
+ }
83
97
98
+ let typeNode : TypeNode ;
84
99
if ( token . parent . parent . kind === SyntaxKind . BinaryExpression ) {
85
100
const binaryExpression = token . parent . parent as BinaryExpression ;
86
-
87
101
const checker = context . program . getTypeChecker ( ) ;
88
102
const widenedType = checker . getWidenedType ( checker . getBaseTypeOfLiteralType ( checker . getTypeAtLocation ( binaryExpression . right ) ) ) ;
89
103
typeNode = checker . typeToTypeNode ( widenedType , classDeclaration ) ;
90
104
}
91
-
92
105
typeNode = typeNode || createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ;
93
106
94
- const openBrace = getOpenBraceOfClassLike ( classDeclaration , sourceFile ) ;
95
-
96
107
const property = createProperty (
97
108
/*decorators*/ undefined ,
98
109
/*modifiers*/ isStatic ? [ createToken ( SyntaxKind . StaticKeyword ) ] : undefined ,
@@ -103,10 +114,10 @@ namespace ts.codefix {
103
114
const propertyChangeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context ) ;
104
115
propertyChangeTracker . insertNodeAfter ( sourceFile , openBrace , property , { suffix : context . newLineCharacter } ) ;
105
116
106
- const actions = [ {
117
+ ( actions || ( actions = [ ] ) ) . push ( {
107
118
description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
108
119
changes : propertyChangeTracker . getChanges ( )
109
- } ] ;
120
+ } ) ;
110
121
111
122
if ( ! isStatic ) {
112
123
const stringTypeNode = createKeywordTypeNode ( SyntaxKind . StringKeyword ) ;
0 commit comments