1
1
/* @internal */
2
2
namespace ts . codefix {
3
3
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
+ ] ;
5
9
registerCodeFix ( {
6
10
errorCodes,
7
11
getCodeActions : context => {
8
12
const { host, sourceFile, span : { start } } = context ;
9
- const packageName = getTypesPackageNameToInstall ( host , sourceFile , start ) ;
13
+ const packageName = getTypesPackageNameToInstall ( host , sourceFile , start , context . errorCode ) ;
10
14
return packageName === undefined ? [ ]
11
15
: [ createCodeFixAction ( fixId , /*changes*/ [ ] , [ Diagnostics . Install_0 , packageName ] , fixId , Diagnostics . Install_all_missing_types_packages , getCommand ( sourceFile . fileName , packageName ) ) ] ;
12
16
} ,
13
17
fixIds : [ fixId ] ,
14
18
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 ) ;
16
20
if ( pkg ) {
17
21
commands . push ( getCommand ( diag . file . fileName , pkg ) ) ;
18
22
}
@@ -23,9 +27,11 @@ namespace ts.codefix {
23
27
return { type : "install package" , file : fileName , packageName } ;
24
28
}
25
29
26
- function getTypesPackageNameToInstall ( host : LanguageServiceHost , sourceFile : SourceFile , pos : number ) : string | undefined {
30
+ function getTypesPackageNameToInstall ( host : LanguageServiceHost , sourceFile : SourceFile , pos : number , diagCode : number ) : string | undefined {
27
31
const moduleName = cast ( getTokenAtPosition ( sourceFile , pos , /*includeJsDocComment*/ false ) , isStringLiteral ) . text ;
28
32
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 ) ;
30
36
}
31
37
}
0 commit comments