From c043944b1434daa67b66573c66850d51c0a16944 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Wed, 13 Nov 2024 09:04:51 -0800 Subject: [PATCH] Let isolated declarations fixer add toplevel imports more (#60267) --- src/services/codefixes/helpers.ts | 10 +++---- ...peAnnotationOnExports56-toplevel-import.ts | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports56-toplevel-import.ts diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 7957b28b110..107f954def4 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -606,12 +606,10 @@ export function typeToAutoImportableTypeNode(checker: TypeChecker, importAdder: /** @internal */ export function typeNodeToAutoImportableTypeNode(typeNode: TypeNode, importAdder: ImportAdder, scriptTarget: ScriptTarget): TypeNode | undefined { - if (typeNode && isImportTypeNode(typeNode)) { - const importableReference = tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget); - if (importableReference) { - importSymbols(importAdder, importableReference.symbols); - typeNode = importableReference.typeNode; - } + const importableReference = tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget); + if (importableReference) { + importSymbols(importAdder, importableReference.symbols); + typeNode = importableReference.typeNode; } // Ensure nodes are fresh so they can have different positions when going through formatting. diff --git a/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports56-toplevel-import.ts b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports56-toplevel-import.ts new file mode 100644 index 00000000000..436d32e4185 --- /dev/null +++ b/tests/cases/fourslash/codeFixMissingTypeAnnotationOnExports56-toplevel-import.ts @@ -0,0 +1,30 @@ +/// + +// @isolatedDeclarations: true +// @declaration: true + +// @Filename: /person-code.ts +////export interface Person { x: string; } +////export function getPerson() : Person { +//// return null! +////} + +// @Filename: /code.ts +////import { getPerson } from "./person-code"; +////export function wrapPerson() { +//// return { person: getPerson() } +////}; + +goTo.file("/code.ts"); + +verify.codeFix({ + description: "Add return type '{ person: Person; }'", + index: 0, + newFileContent: +`import { getPerson, Person } from "./person-code"; +export function wrapPerson(): { + person: Person; +} { + return { person: getPerson() } +};` +});