Don’t create invalid type-only imports during add missing import (#43828)

This commit is contained in:
Andrew Branch
2021-04-26 11:52:34 -07:00
committed by GitHub
parent d5af89c552
commit 3de706a852
2 changed files with 37 additions and 0 deletions

View File

@@ -420,6 +420,9 @@ namespace ts.codefix {
const { importClause } = declaration;
if (!importClause || !isStringLiteralLike(declaration.moduleSpecifier)) return undefined;
const { name, namedBindings } = importClause;
// A type-only import may not have both a default and named imports, so the only way a name can
// be added to an existing type-only import is adding a named import to existing named bindings.
if (importClause.isTypeOnly && !(importKind === ImportKind.Named && namedBindings)) return undefined;
return importKind === ImportKind.Default && !name || importKind === ImportKind.Named && (!namedBindings || namedBindings.kind === SyntaxKind.NamedImports)
? { kind: ImportFixKind.AddToExisting, importClauseOrBindingPattern: importClause, importKind, moduleSpecifier: declaration.moduleSpecifier.text, canUseTypeOnlyImport }
: undefined;

View File

@@ -0,0 +1,34 @@
/// <reference path="fourslash.ts" />
// @importsNotUsedAsValues: error
// @Filename: Presenter.ts
//// export type DisplayStyle = "normal" | "compact";
////
//// export default class Presenter {
//// present(displayStyle: DisplayStyle): Element {
//// return document.createElement("placeholder");
//// }
//// }
// @Filename: index.ts
//// import type Presenter from "./Presenter";
////
//// function present(
//// presenter: Presenter,
//// displayStyle: DisplayStyle,
//// ) {}
goTo.file("index.ts");
verify.codeFix({
errorCode: ts.Diagnostics.Cannot_find_name_0.code,
description: ignoreInterpolations(ts.Diagnostics.Import_0_from_module_1),
newFileContent:
`import type { DisplayStyle } from "./Presenter";
import type Presenter from "./Presenter";
function present(
presenter: Presenter,
displayStyle: DisplayStyle,
) {}`
});