From 4ac70b9bcec5441a66aa19221ca6e4840795d345 Mon Sep 17 00:00:00 2001 From: Sri Krishna Paritala Date: Wed, 15 May 2024 10:26:53 +0530 Subject: [PATCH] Rename `exportDecl` to `export` --- docs/writing_plugins.md | 6 ++-- .../protoc-gen-es/src/protoc-gen-es-plugin.ts | 36 +++++++++---------- .../src/protoc-gen-twirp-es.ts | 2 +- .../src/file-export-decl.test.ts | 8 ++--- .../src/js_import_style.test.ts | 2 +- .../src/keep_empty_files.test.ts | 2 +- .../src/ecmascript/generated-file.ts | 7 ++-- 7 files changed, 31 insertions(+), 32 deletions(-) diff --git a/docs/writing_plugins.md b/docs/writing_plugins.md index a29b0b235..61ad07ae8 100644 --- a/docs/writing_plugins.md +++ b/docs/writing_plugins.md @@ -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: @@ -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: diff --git a/packages/protoc-gen-es/src/protoc-gen-es-plugin.ts b/packages/protoc-gen-es/src/protoc-gen-es-plugin.ts index 204c8b29e..344baf40b 100644 --- a/packages/protoc-gen-es/src/protoc-gen-es-plugin.ts +++ b/packages/protoc-gen-es/src/protoc-gen-es-plugin.ts @@ -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)) { @@ -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; @@ -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; @@ -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; @@ -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; @@ -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)) { @@ -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; @@ -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(); } @@ -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(); } @@ -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; @@ -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; @@ -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) { @@ -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; } @@ -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; } @@ -215,7 +215,7 @@ 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; } @@ -223,7 +223,7 @@ function generateDts(schema: Schema) { 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; } @@ -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(); @@ -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": diff --git a/packages/protoplugin-example/src/protoc-gen-twirp-es.ts b/packages/protoplugin-example/src/protoc-gen-twirp-es.ts index 2064ebe44..b89c8f4af 100755 --- a/packages/protoplugin-example/src/protoc-gen-twirp-es.ts +++ b/packages/protoplugin-example/src/protoc-gen-twirp-es.ts @@ -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, diff --git a/packages/protoplugin-test/src/file-export-decl.test.ts b/packages/protoplugin-test/src/file-export-decl.test.ts index 612f73e23..0e7386b99 100644 --- a/packages/protoplugin-test/src/file-export-decl.test.ts +++ b/packages/protoplugin-test/src/file-export-decl.test.ts @@ -17,11 +17,11 @@ 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;"]); }); @@ -29,7 +29,7 @@ describe("GeneratedFile.exportDecl", () => { 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;"]); }); @@ -37,7 +37,7 @@ describe("GeneratedFile.exportDecl", () => { 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";`, diff --git a/packages/protoplugin-test/src/js_import_style.test.ts b/packages/protoplugin-test/src/js_import_style.test.ts index 72bee906f..e7422493d 100644 --- a/packages/protoplugin-test/src/js_import_style.test.ts +++ b/packages/protoplugin-test/src/js_import_style.test.ts @@ -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";`); diff --git a/packages/protoplugin-test/src/keep_empty_files.test.ts b/packages/protoplugin-test/src/keep_empty_files.test.ts index b654e03cf..be41aaa48 100644 --- a/packages/protoplugin-test/src/keep_empty_files.test.ts +++ b/packages/protoplugin-test/src/keep_empty_files.test.ts @@ -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); diff --git a/packages/protoplugin/src/ecmascript/generated-file.ts b/packages/protoplugin/src/ecmascript/generated-file.ts index 8705ab886..2d4aa4ca6 100644 --- a/packages/protoplugin/src/ecmascript/generated-file.ts +++ b/packages/protoplugin/src/ecmascript/generated-file.ts @@ -94,12 +94,11 @@ export interface GeneratedFile { */ jsDoc(desc: Exclude, 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: @@ -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. @@ -212,7 +211,7 @@ export function createGeneratedFile( ); el.push("\n"); }, - exportDecl(declaration, name) { + export(declaration, name) { return { kind: "es_export_stmt", name,