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: Add GeneratedFile.array #935

Merged
merged 2 commits into from
Jul 19, 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
9 changes: 2 additions & 7 deletions packages/protoc-gen-es/src/protoc-gen-es-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ import {
type Schema,
type Target,
} from "@bufbuild/protoplugin";
import {
arrayLiteral,
fieldJsonType,
fieldTypeScriptType,
functionCall,
} from "./util";
import { fieldJsonType, fieldTypeScriptType, functionCall } from "./util";
import { version } from "../package.json";
import { RawPluginOptions } from "@bufbuild/protoplugin/dist/cjs/parameter";

Expand Down Expand Up @@ -345,7 +340,7 @@ function getFileDescCall(f: GeneratedFile, file: DescFile, schema: Schema) {
}));
return functionCall(fileDesc, [
f.string(info.base64()),
arrayLiteral(deps),
f.array(deps),
]);
}
return functionCall(fileDesc, [f.string(info.base64())]);
Expand Down
4 changes: 0 additions & 4 deletions packages/protoc-gen-es/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,6 @@ export function functionCall(
return [fn, ...tp, "(", commaSeparate(args), ")"];
}

export function arrayLiteral(elements: Printable[]): Printable {
return ["[", commaSeparate(elements), "]"];
}

function commaSeparate(elements: Printable[]): Printable {
const r: Printable[] = [];
for (let i = 0; i < elements.length; i++) {
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 @@ -38,7 +38,7 @@ function generateTs(schema: Schema<PluginOptions>) {
f.preamble(file);
for (const service of file.services) {
f.print(f.jsDoc(service));
f.print(f.export("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
32 changes: 32 additions & 0 deletions packages/protoplugin-test/src/file-array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { describe, expect, test } from "@jest/globals";
import type { Printable } from "@bufbuild/protoplugin";
import { createTestPluginAndRun } from "./helpers.js";

describe("GeneratedFile.array", () => {
test("creates an array literal", async () => {
const lines = await createTestPluginAndRun({
proto: `syntax="proto3";`,
parameter: "target=ts",
returnLinesOfFirstFile: true,
generateAny: (f) => {
const arr: Printable = f.array(["foo", 1, true]);
f.print(arr);
},
});
expect(lines).toStrictEqual(["[foo, 1, true]"]);
});
});
17 changes: 17 additions & 0 deletions packages/protoplugin/src/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export interface GeneratedFile {
*/
string(string: string): Printable;

/**
* Create an array literal.
*/
array(elements: Printable[]): Printable;

/**
* Create a JSDoc comment block with the given text. Line breaks and white-space
* stay intact.
Expand Down Expand Up @@ -238,6 +243,18 @@ export function createGeneratedFile(
value: string,
};
},
array(elements) {
const p: Printable = [];
p.push("[");
for (const [index, element] of elements.entries()) {
p.push(element);
if (index < elements.length - 1) {
p.push(", ");
}
}
p.push("]");
return p;
},
jsDoc(textOrSchema, indentation) {
return {
kind: "es_jsdoc",
Expand Down
6 changes: 5 additions & 1 deletion packages/protoplugin/src/safe-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ const reservedIdentifiers = new Set([
]);

/**
* Escapes names that are reserved identifiers in ECMAScript, TypeScript.
* Escapes reserved words in ECMAScript and TypeScript identifiers, by appending
* a dollar sign.
*
* This function is intended for use with identifiers from Protobuf. The passed
* string must be a valid identifier (e.g. not start with a digit).
Comment on lines +86 to +90
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not directly related, but I noticed the docs for the function safeIdentifier need clarification.

*
* Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
*/
Expand Down