mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
installTypesForPackage refactor: Trigger even if resolved to a ".js" file (#20353)
* installTypesForPackage refactor: Trigger even if resolved to a ".js" file * Use `extensionIsTypeScript`
This commit is contained in:
@@ -46,9 +46,19 @@ namespace ts.refactor.installTypesForPackage {
|
||||
function getAction(context: RefactorContext): CodeAction | undefined {
|
||||
const { file, startPosition } = context;
|
||||
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
|
||||
if (isStringLiteral(node) && isModuleIdentifier(node) && getResolvedModule(file, node.text) === undefined) {
|
||||
return codefix.tryGetCodeActionForInstallPackageTypes(context.host, file.fileName, node.text);
|
||||
if (!isStringLiteral(node) || !isModuleIdentifier(node)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const resolvedTo = getResolvedModule(file, node.text);
|
||||
// Still offer to install types if it resolved to e.g. a ".js" file.
|
||||
// `tryGetCodeActionForInstallPackageTypes` will verify that we're looking for a valid package name,
|
||||
// so the fix won't trigger for imports of ".js" files that couldn't be better replaced by typings.
|
||||
if (resolvedTo && extensionIsTypeScript(resolvedTo.extension)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return codefix.tryGetCodeActionForInstallPackageTypes(context.host, file.fileName, node.text);
|
||||
}
|
||||
|
||||
function isModuleIdentifier(node: StringLiteral): boolean {
|
||||
|
||||
29
tests/cases/fourslash/refactorInstallTypesForPackage_js.ts
Normal file
29
tests/cases/fourslash/refactorInstallTypesForPackage_js.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
// @allowJs: true
|
||||
|
||||
// @Filename: /node_modules/abs/index.js
|
||||
////not read
|
||||
|
||||
// @Filename: /a.js
|
||||
////import abs = require("/*a*/abs/*b*/");
|
||||
|
||||
test.setTypesRegistry({ "abs": undefined });
|
||||
|
||||
goTo.select("a", "b");
|
||||
verify.refactor({
|
||||
name: "Install missing types package",
|
||||
actionName: "install",
|
||||
refactors: [
|
||||
{
|
||||
name: "Install missing types package",
|
||||
description: "Install missing types package",
|
||||
actions: [
|
||||
{
|
||||
description: "Install '@types/abs'",
|
||||
name: "install",
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
});
|
||||
Reference in New Issue
Block a user