From 55953f2aaee6145dadceed7e518f9e26299ddaaa Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Thu, 13 Jun 2024 13:48:26 +0200 Subject: [PATCH] V2: Add typeOnly argument to GeneratedFile.import (#887) --- packages/protoplugin-test/src/file-import.test.ts | 10 ++++++++++ packages/protoplugin/src/generated-file.ts | 11 ++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/protoplugin-test/src/file-import.test.ts b/packages/protoplugin-test/src/file-import.test.ts index ade95516d..56d95a5dd 100644 --- a/packages/protoplugin-test/src/file-import.test.ts +++ b/packages/protoplugin-test/src/file-import.test.ts @@ -52,4 +52,14 @@ describe("GeneratedFile.import", () => { }, }); }); + test("should honor typeOnly argument", async function () { + await createTestPluginAndRun({ + proto: `syntax="proto3";`, + parameter: "target=ts", + generateAny(f) { + const imp = f.import("Foo", "@scope/pkg", true); + expect(imp.typeOnly).toBe(true); + }, + }); + }); }); diff --git a/packages/protoplugin/src/generated-file.ts b/packages/protoplugin/src/generated-file.ts index 7b8bd5df5..f5fe0f8bb 100644 --- a/packages/protoplugin/src/generated-file.ts +++ b/packages/protoplugin/src/generated-file.ts @@ -134,11 +134,16 @@ export interface GeneratedFile { * relative to the project root. The import path is automatically made * relative to the current file. */ - import(name: string, from: string): ImportSymbol; + import(name: string, from: string, typeOnly?: boolean): ImportSymbol; /** * In case you need full control over exports and imports, use print() and * formulate your own imports and exports based on this property. + * + * With the plugin option `js_import_style=legacy_commonjs`, this property + * reports "legacy_commonjs", but only if the current target is "js". + * This matches the behavior of import(), which also only generates CommonJS + * under this condition. */ readonly jsImportStyle: "module" | "legacy_commonjs"; @@ -239,8 +244,8 @@ export function createGeneratedFile( importShape(desc) { return resolveShapeImport(desc); }, - import(name, from) { - return createImportSymbol(name, from); + import(name, from, typeOnly = false) { + return createImportSymbol(name, from, typeOnly); }, jsImportStyle, runtime,