Skip to content

Commit

Permalink
V2: Add typeOnly argument to GeneratedFile.import (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Jun 13, 2024
1 parent fe29cca commit 55953f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/protoplugin-test/src/file-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
});
});
});
11 changes: 8 additions & 3 deletions packages/protoplugin/src/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 55953f2

Please sign in to comment.