Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Rename exportDecl to export #843

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/writing_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ The natural instinct would be to simply print your own import statements as `f.p

### Exporting

To export a declaration from your code, use `exportDecl`:
To export a declaration from your code, use `export`:

```typescript
const name = "foo";
f.exportDecl("const", name);
f.export("const", name);
```

This method takes two arguments:
Expand All @@ -354,7 +354,7 @@ The return value of the method can be passed to `print`:

```typescript
const name = "foo";
f.print(f.exportDecl("const", name), " = 123;");
f.print(f.export("const", name), " = 123;");
```

The example above will generate the following code:
Expand Down
36 changes: 18 additions & 18 deletions packages/protoc-gen-es/src/protoc-gen-es-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function generateTs(schema: Schema) {
const { GenDescFile } = f.runtime.codegen;
const fileDesc = f.importDesc(file);
generateDescDoc(f, file);
f.print(f.exportDecl("const", fileDesc.name), ": ", GenDescFile, " = ", pure);
f.print(f.export("const", fileDesc.name), ": ", GenDescFile, " = ", pure);
f.print(" ", getFileDescCall(f, file, schema), ";");
f.print();
for (const desc of schema.typesInFile(file)) {
Expand All @@ -64,7 +64,7 @@ function generateTs(schema: Schema) {
const name = f.importDesc(desc).name;
generateDescDoc(f, desc);
const call = functionCall(messageDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.exportDecl("const", name), ": ", GenDescMessage, "<", MessageShape, ">", " = ", pure);
f.print(f.export("const", name), ": ", GenDescMessage, "<", MessageShape, ">", " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -76,7 +76,7 @@ function generateTs(schema: Schema) {
generateDescDoc(f, desc);
const name = f.importDesc(desc).name;
const call = functionCall(enumDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.exportDecl("const", name), ": ", GenDescEnum, "<", EnumShape, ">", " = ", pure);
f.print(f.export("const", name), ": ", GenDescEnum, "<", EnumShape, ">", " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -88,7 +88,7 @@ function generateTs(schema: Schema) {
const V = fieldTypeScriptType(desc).typing;
const call = functionCall(extDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.jsDoc(desc));
f.print(f.exportDecl("const", name), ": ", GenDescExtension, "<", E, ", ", V, ">", " = ", pure);
f.print(f.export("const", name), ": ", GenDescExtension, "<", E, ", ", V, ">", " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -98,7 +98,7 @@ function generateTs(schema: Schema) {
const name = f.importDesc(desc).name;
const call = functionCall(serviceDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.jsDoc(desc));
f.print(f.exportDecl("const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), "> = ", pure);
f.print(f.export("const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), "> = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -115,7 +115,7 @@ function generateJs(schema: Schema) {
f.preamble(file);
const fileDesc = f.importDesc(file);
generateDescDoc(f, file);
f.print(f.exportDecl("const", fileDesc.name), " = ", pure);
f.print(f.export("const", fileDesc.name), " = ", pure);
f.print(" ", getFileDescCall(f, file, schema), ";");
f.print();
for (const desc of schema.typesInFile(file)) {
Expand All @@ -125,7 +125,7 @@ function generateJs(schema: Schema) {
const name = f.importDesc(desc).name;
generateDescDoc(f, desc);
const call = functionCall(messageDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.exportDecl("const", name), " = ", pure);
f.print(f.export("const", name), " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -137,7 +137,7 @@ function generateJs(schema: Schema) {
generateDescDoc(f, desc);
const name = f.importDesc(desc).name;
const call = functionCall(enumDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.exportDecl("const", name), " = ", pure);
f.print(f.export("const", name), " = ", pure);
f.print(" ", call, ";");
f.print();
}
Expand All @@ -146,7 +146,7 @@ function generateJs(schema: Schema) {
f.print(f.jsDoc(desc));
const { tsEnum } = f.runtime.codegen;
const call = functionCall(tsEnum, [f.importDesc(desc)]);
f.print(f.exportDecl("const", f.importShape(desc).name), " = ", pure);
f.print(f.export("const", f.importShape(desc).name), " = ", pure);
f.print(" ", call, ";");
f.print();
}
Expand All @@ -157,7 +157,7 @@ function generateJs(schema: Schema) {
const name = f.importDesc(desc).name;
const call = functionCall(extDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.jsDoc(desc));
f.print(f.exportDecl("const", name), " = ", pure);
f.print(f.export("const", name), " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -167,7 +167,7 @@ function generateJs(schema: Schema) {
const name = f.importDesc(desc).name;
f.print(f.jsDoc(desc));
const call = functionCall(serviceDesc, [fileDesc, ...pathInFileDesc(desc)]);
f.print(f.exportDecl("const", name), " = ", pure);
f.print(f.export("const", name), " = ", pure);
f.print(" ", call, ";");
f.print();
break;
Expand All @@ -185,7 +185,7 @@ function generateDts(schema: Schema) {
const { GenDescFile } = f.runtime.codegen;
const fileDesc = f.importDesc(file);
generateDescDoc(f, file);
f.print(f.exportDecl("declare const", fileDesc.name), ": ", GenDescFile, ";");
f.print(f.export("declare const", fileDesc.name), ": ", GenDescFile, ";");
f.print();
for (const desc of schema.typesInFile(file)) {
switch (desc.kind) {
Expand All @@ -195,7 +195,7 @@ function generateDts(schema: Schema) {
const MessageShape = f.importShape(desc);
const name = f.importDesc(desc).name;
generateDescDoc(f, desc);
f.print(f.exportDecl("declare const", name), ": ", GenDescMessage, "<", MessageShape, ">", ";");
f.print(f.export("declare const", name), ": ", GenDescMessage, "<", MessageShape, ">", ";");
f.print();
break;
}
Expand All @@ -205,7 +205,7 @@ function generateDts(schema: Schema) {
const EnumShape = f.importShape(desc);
generateDescDoc(f, desc);
const name = f.importDesc(desc).name;
f.print(f.exportDecl("declare const", name), ": ", GenDescEnum, "<", EnumShape, ">;");
f.print(f.export("declare const", name), ": ", GenDescEnum, "<", EnumShape, ">;");
f.print();
break;
}
Expand All @@ -215,15 +215,15 @@ function generateDts(schema: Schema) {
const E = f.importShape(desc.extendee);
const V = fieldTypeScriptType(desc).typing;
f.print(f.jsDoc(desc));
f.print(f.exportDecl("declare const", name), ": ", GenDescExtension, "<", E, ", ", V, ">;");
f.print(f.export("declare const", name), ": ", GenDescExtension, "<", E, ", ", V, ">;");
f.print();
break;
}
case "service": {
const { GenDescService } = f.runtime.codegen;
const name = f.importDesc(desc).name;
f.print(f.jsDoc(desc));
f.print(f.exportDecl("declare const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), ">;");
f.print(f.export("declare const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), ">;");
f.print();
break;
}
Expand Down Expand Up @@ -311,7 +311,7 @@ function getServiceShapeExpr(f: GeneratedFile, service: DescService): Printable
// prettier-ignore
function generateEnumShape(f: GeneratedFile, enumeration: DescEnum) {
f.print(f.jsDoc(enumeration));
f.print(f.exportDecl("enum", f.importShape(enumeration).name), " {");
f.print(f.export("enum", f.importShape(enumeration).name), " {");
for (const value of enumeration.values) {
if (enumeration.values.indexOf(value) > 0) {
f.print();
Expand All @@ -328,7 +328,7 @@ function generateMessageShape(f: GeneratedFile, message: DescMessage, target: Ex
const { Message } = f.runtime;
const declaration = target == "ts" ? "type" : "declare type";
f.print(f.jsDoc(message));
f.print(f.exportDecl(declaration, f.importShape(message).name), " = ", Message, "<", f.string(message.typeName), "> & {");
f.print(f.export(declaration, f.importShape(message).name), " = ", Message, "<", f.string(message.typeName), "> & {");
for (const member of message.members) {
switch (member.kind) {
case "oneof":
Expand Down
2 changes: 1 addition & 1 deletion packages/protoplugin-example/src/protoc-gen-twirp-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function generateTs(schema: Schema) {
f.preamble(file);
for (const service of file.services) {
f.print(f.jsDoc(service));
f.print(f.exportDecl("class", safeIdentifier(service.name) + "Client"), " {");
f.print(f.export("class", safeIdentifier(service.name) + "Client"), " {");
f.print();

// To support the custom option we defined in customoptions/default_host.proto,
Expand Down
8 changes: 4 additions & 4 deletions packages/protoplugin-test/src/file-export-decl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ import type { DescEnum, DescMessage } from "@bufbuild/protobuf";
import type { GeneratedFile } from "@bufbuild/protoplugin/ecmascript";
import { createTestPluginAndRun } from "./helpers.js";

describe("GeneratedFile.exportDecl", () => {
describe("GeneratedFile.export", () => {
test("works as documented", async () => {
const lines = await testGenerate((f) => {
const name = "foo";
f.print(f.exportDecl("const", name), " = 123;");
f.print(f.export("const", name), " = 123;");
});
expect(lines).toStrictEqual(["export const foo = 123;"]);
});

test("declaration can be empty string", async () => {
const lines = await testGenerate((f) => {
f.print("const foo = 123;");
f.print(f.exportDecl("", "foo"), ";");
f.print(f.export("", "foo"), ";");
});
expect(lines).toStrictEqual(["const foo = 123;", "export foo;"]);
});

test("forces import with same name to be aliased", async () => {
const lines = await testGenerate((f) => {
f.print(f.import("Foo", "pkg"));
f.print(f.exportDecl("const", "Foo"), " = 123;");
f.print(f.export("const", "Foo"), " = 123;");
});
expect(lines).toStrictEqual([
`import { Foo as Foo$1 } from "pkg";`,
Expand Down
2 changes: 1 addition & 1 deletion packages/protoplugin-test/src/js_import_style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("js_import_style", () => {
) {
const f = schema.generateFile(`test.${target}`);
f.print("const thirdParty = ", f.import("third", "party"), ";");
f.print(f.exportDecl("class", "MyClass"), " {}");
f.print(f.export("class", "MyClass"), " {}");
switch (f.jsImportStyle) {
case "module":
f.print(`import { hand } from "written";`);
Expand Down
2 changes: 1 addition & 1 deletion packages/protoplugin-test/src/keep_empty_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("keep_empty_files", () => {
// An unused import does not count as non-empty
f.import("foo", "bar");
// An unused export declaration does not count as non-empty
f.exportDecl("foo", "bar");
f.export("foo", "bar");
},
});
expect(res.file.length).toBe(0);
Expand Down
7 changes: 3 additions & 4 deletions packages/protoplugin/src/ecmascript/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ export interface GeneratedFile {
*/
jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): Printable;

// TODO rename to "export"?
/**
* Create a printable export statement. For example:
*
* ```ts
* f.print(f.exportDecl("abstract class", "MyClass"), " {}")
* f.print(f.export("abstract class", "MyClass"), " {}")
* ```
*
* Will generate as:
Expand All @@ -111,7 +110,7 @@ export interface GeneratedFile {
* statement. If the plugin option `js_import_style=legacy_commonjs` is set,
* exports will automatically be generated for CommonJS.
*/
exportDecl(declaration: string, name: string): Printable;
export(declaration: string, name: string): Printable;

/**
* Import a message or enumeration generated by protoc-gen-es.
Expand Down Expand Up @@ -212,7 +211,7 @@ export function createGeneratedFile(
);
el.push("\n");
},
exportDecl(declaration, name) {
export(declaration, name) {
return {
kind: "es_export_stmt",
name,
Expand Down