Skip to content

Commit a1f9a4f

Browse files
author
Andy
authored
fixCannotFindModule: Special handling for node core modules like "fs" (microsoft#23807)
* fixCannotFindModule: Special handling for node core modules like "fs" * Hardcode @types/node
1 parent 3bfbe68 commit a1f9a4f

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed
+11-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
/* @internal */
22
namespace 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
}

src/services/jsTyping.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ namespace ts.JsTyping {
3939
"crypto", "stream", "util", "assert", "tty", "domain",
4040
"constants", "process", "v8", "timers", "console"];
4141

42-
const nodeCoreModules = arrayToSet(nodeCoreModuleList);
42+
/* @internal */
43+
export const nodeCoreModules = arrayToSet(nodeCoreModuleList);
4344

4445
/**
4546
* A map of loose file names to library names that we are confident require typings
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
5+
// @Filename: /a.ts
6+
////import fs = require("fs");
7+
////fs;
8+
9+
verify.codeFixAvailable([{
10+
description: "Install '@types/node'",
11+
commands: [{
12+
type: "install package",
13+
file: "/a.ts",
14+
packageName: "@types/node",
15+
}],
16+
}]);

0 commit comments

Comments
 (0)