11/* @internal */
22namespace ts . codefix {
33 const fixId = "fixCannotFindModule" ;
4- const errorCodes = [ Diagnostics . Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type . code ] ;
4+ const errorCodeCannotFindModule = Diagnostics . Cannot_find_module_0 . code ;
5+ const errorCodes = [
6+ errorCodeCannotFindModule ,
7+ Diagnostics . Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type . code ,
8+ ] ;
59 registerCodeFix ( {
610 errorCodes,
711 getCodeActions : context => {
812 const { host, sourceFile, span : { start } } = context ;
9- const packageName = getTypesPackageNameToInstall ( host , sourceFile , start ) ;
13+ const packageName = getTypesPackageNameToInstall ( host , sourceFile , start , context . errorCode ) ;
1014 return packageName === undefined ? [ ]
1115 : [ createCodeFixAction ( fixId , /*changes*/ [ ] , [ Diagnostics . Install_0 , packageName ] , fixId , Diagnostics . Install_all_missing_types_packages , getCommand ( sourceFile . fileName , packageName ) ) ] ;
1216 } ,
1317 fixIds : [ fixId ] ,
1418 getAllCodeActions : context => codeFixAll ( context , errorCodes , ( _ , diag , commands ) => {
15- const pkg = getTypesPackageNameToInstall ( context . host , diag . file , diag . start ) ;
19+ const pkg = getTypesPackageNameToInstall ( context . host , diag . file , diag . start , diag . code ) ;
1620 if ( pkg ) {
1721 commands . push ( getCommand ( diag . file . fileName , pkg ) ) ;
1822 }
@@ -23,9 +27,11 @@ namespace ts.codefix {
2327 return { type : "install package" , file : fileName , packageName } ;
2428 }
2529
26- function getTypesPackageNameToInstall ( host : LanguageServiceHost , sourceFile : SourceFile , pos : number ) : string | undefined {
30+ function getTypesPackageNameToInstall ( host : LanguageServiceHost , sourceFile : SourceFile , pos : number , diagCode : number ) : string | undefined {
2731 const moduleName = cast ( getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) , isStringLiteral ) . text ;
2832 const { packageName } = getPackageName ( moduleName ) ;
29- return host . isKnownTypesPackageName ( packageName ) ? getTypesPackageName ( packageName ) : undefined ;
33+ return diagCode === errorCodeCannotFindModule
34+ ? ( JsTyping . nodeCoreModules . has ( packageName ) ? "@types/node" : undefined )
35+ : ( host . isKnownTypesPackageName ( packageName ) ? getTypesPackageName ( packageName ) : undefined ) ;
3036 }
3137}
0 commit comments