22namespace ts . codefix {
33 registerCodeFix ( {
44 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 ] ,
66 getCodeActions : getActionsForAddMissingMember
77 } ) ;
88
@@ -79,20 +79,31 @@ namespace ts.codefix {
7979 }
8080
8181 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+ }
8397
98+ let typeNode : TypeNode ;
8499 if ( token . parent . parent . kind === SyntaxKind . BinaryExpression ) {
85100 const binaryExpression = token . parent . parent as BinaryExpression ;
86-
87101 const checker = context . program . getTypeChecker ( ) ;
88102 const widenedType = checker . getWidenedType ( checker . getBaseTypeOfLiteralType ( checker . getTypeAtLocation ( binaryExpression . right ) ) ) ;
89103 typeNode = checker . typeToTypeNode ( widenedType , classDeclaration ) ;
90104 }
91-
92105 typeNode = typeNode || createKeywordTypeNode ( SyntaxKind . AnyKeyword ) ;
93106
94- const openBrace = getOpenBraceOfClassLike ( classDeclaration , sourceFile ) ;
95-
96107 const property = createProperty (
97108 /*decorators*/ undefined ,
98109 /*modifiers*/ isStatic ? [ createToken ( SyntaxKind . StaticKeyword ) ] : undefined ,
@@ -103,10 +114,10 @@ namespace ts.codefix {
103114 const propertyChangeTracker = textChanges . ChangeTracker . fromCodeFixContext ( context ) ;
104115 propertyChangeTracker . insertNodeAfter ( sourceFile , openBrace , property , { suffix : context . newLineCharacter } ) ;
105116
106- const actions = [ {
117+ ( actions || ( actions = [ ] ) ) . push ( {
107118 description : formatStringFromArgs ( getLocaleSpecificMessage ( Diagnostics . Add_declaration_for_missing_property_0 ) , [ token . getText ( ) ] ) ,
108119 changes : propertyChangeTracker . getChanges ( )
109- } ] ;
120+ } ) ;
110121
111122 if ( ! isStatic ) {
112123 const stringTypeNode = createKeywordTypeNode ( SyntaxKind . StringKeyword ) ;
0 commit comments