namespace ts . codefix { const fixId = "fixImplicitThis" ; const errorCodes = [ Diagnostics . this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation . code ] ; registerCodeFix ( { errorCodes , getCodeActions ( context ) { const { sourceFile , program , span } = context ; let diagnostic ; const changes = textChanges . ChangeTracker . with ( context , t => { diagnostic = doChange ( t , sourceFile , span . start , program . getTypeChecker ( ) ) ; } ) ; return diagnostic ? [ createCodeFixAction ( fixId , changes , diagnostic , fixId , Diagnostics . Fix_all_implicit_this_errors ) ] : emptyArray ; } , fixIds : [ fixId ] , getAllCodeActions : context => codeFixAll ( context , errorCodes , ( changes , diag ) => { doChange ( changes , diag . file , diag . start , context . program . getTypeChecker ( ) ) ; } ) , } ) ; function doChange ( changes , sourceFile , pos , checker ) { const token = getTokenAtPosition ( sourceFile , pos ) ; Debug . assert ( token . kind === SyntaxKind . ThisKeyword ) ; const fn = getThisContainer ( token , false ) ; if ( ! isFunctionDeclaration ( fn ) && ! isFunctionExpression ( fn ) ) return undefined ; if ( ! isSourceFile ( getThisContainer ( fn , false ) ) ) { const fnKeyword = Debug . checkDefined ( findChildOfKind ( fn , SyntaxKind . FunctionKeyword , sourceFile ) ) ; const { name } = fn ; const body = Debug . checkDefined ( fn . body ) ; if ( isFunctionExpression ( fn ) ) { if ( name && FindAllReferences . Core . isSymbolReferencedInFile ( name , checker , sourceFile , body ) ) { return undefined ; } changes . delete ( sourceFile , fnKeyword ) ; if ( name ) { changes . delete ( sourceFile , name ) ; } changes . insertText ( sourceFile , body . pos , " =>" ) ; return [ Diagnostics . Convert_function_expression_0_to_arrow_function , name ? name . text : ANONYMOUS ] ; } else { changes . replaceNode ( sourceFile , fnKeyword , factory . createToken ( SyntaxKind . ConstKeyword ) ) ; changes . insertText ( sourceFile , name ! . end , " = " ) ; changes . insertText ( sourceFile , body . pos , " =>" ) ; return [ Diagnostics . Convert_function_declaration_0_to_arrow_function , name ! . text ] ; } } } }