Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixCannotFindModule: Special handling for node core modules like "fs" #23807

Merged
2 commits merged into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/services/codefixes/fixCannotFindModule.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixCannotFindModule";
const errorCodes = [Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type.code];
const errorCodeCannotFindModule = Diagnostics.Cannot_find_module_0.code;
const errorCodes = [
errorCodeCannotFindModule,
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type.code,
];
registerCodeFix({
errorCodes,
getCodeActions: context => {
const { host, sourceFile, span: { start } } = context;
const packageName = getTypesPackageNameToInstall(host, sourceFile, start);
const packageName = getTypesPackageNameToInstall(host, sourceFile, start, context.errorCode);
return packageName === undefined ? []
: [createCodeFixAction(fixId, /*changes*/ [], [Diagnostics.Install_0, packageName], fixId, Diagnostics.Install_all_missing_types_packages, getCommand(sourceFile.fileName, packageName))];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (_, diag, commands) => {
const pkg = getTypesPackageNameToInstall(context.host, diag.file, diag.start);
const pkg = getTypesPackageNameToInstall(context.host, diag.file, diag.start, diag.code);
if (pkg) {
commands.push(getCommand(diag.file.fileName, pkg));
}
Expand All @@ -23,9 +27,11 @@ namespace ts.codefix {
return { type: "install package", file: fileName, packageName };
}

function getTypesPackageNameToInstall(host: LanguageServiceHost, sourceFile: SourceFile, pos: number): string | undefined {
function getTypesPackageNameToInstall(host: LanguageServiceHost, sourceFile: SourceFile, pos: number, diagCode: number): string | undefined {
const moduleName = cast(getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false), isStringLiteral).text;
const { packageName } = getPackageName(moduleName);
return host.isKnownTypesPackageName(packageName) ? getTypesPackageName(packageName) : undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we arent using getTypesPackageName remove it ?

Copy link
Member

@sheetalkamat sheetalkamat May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i see you moved that later, but dont cant you just make it @types/node here and getTypesPackageName(packageName) in else branch instead ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return diagCode === errorCodeCannotFindModule
? (JsTyping.nodeCoreModules.has(packageName) ? "@types/node" : undefined)
: (host.isKnownTypesPackageName(packageName) ? getTypesPackageName(packageName) : undefined);
}
}
3 changes: 2 additions & 1 deletion src/services/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace ts.JsTyping {
"crypto", "stream", "util", "assert", "tty", "domain",
"constants", "process", "v8", "timers", "console"];

const nodeCoreModules = arrayToSet(nodeCoreModuleList);
/* @internal */
export const nodeCoreModules = arrayToSet(nodeCoreModuleList);

/**
* A map of loose file names to library names that we are confident require typings
Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/codeFixCannotFindModule_nodeCoreModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

// @noImplicitAny: true

// @Filename: /a.ts
////import fs = require("fs");
////fs;

verify.codeFixAvailable([{
description: "Install '@types/node'",
commands: [{
type: "install package",
file: "/a.ts",
packageName: "@types/node",
}],
}]);