@bufbuild/protoc-gen-es 1.8.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -16,15 +16,15 @@
16
16
  },
17
17
  "scripts": {
18
18
  "clean": "rm -rf ./dist/cjs/*",
19
- "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs"
19
+ "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/cjs"
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.8.0",
24
- "@bufbuild/protoplugin": "1.8.0"
23
+ "@bufbuild/protobuf": "^1.10.0",
24
+ "@bufbuild/protoplugin": "1.10.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.8.0"
27
+ "@bufbuild/protobuf": "1.10.0"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -37,7 +37,7 @@ exports.generateDts = generateDts;
37
37
  // prettier-ignore
38
38
  function generateEnum(schema, f, enumeration) {
39
39
  f.print(f.jsDoc(enumeration));
40
- f.print("export declare enum ", enumeration, " {");
40
+ f.print(f.exportDecl("declare enum", (0, ecmascript_1.localName)(enumeration)), " {");
41
41
  for (const value of enumeration.values) {
42
42
  if (enumeration.values.indexOf(value) > 0) {
43
43
  f.print();
@@ -52,8 +52,9 @@ function generateEnum(schema, f, enumeration) {
52
52
  function generateMessage(schema, f, message) {
53
53
  const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
54
54
  const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
55
+ const m = (0, ecmascript_1.localName)(message);
55
56
  f.print(f.jsDoc(message));
56
- f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
57
+ f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
57
58
  for (const member of message.members) {
58
59
  switch (member.kind) {
59
60
  case "oneof":
@@ -65,7 +66,7 @@ function generateMessage(schema, f, message) {
65
66
  }
66
67
  f.print();
67
68
  }
68
- f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
69
+ f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
69
70
  f.print();
70
71
  generateWktMethods(schema, f, message);
71
72
  f.print(" static readonly runtime: typeof ", protoN, ";");
@@ -75,13 +76,13 @@ function generateMessage(schema, f, message) {
75
76
  //f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
76
77
  f.print();
77
78
  generateWktStaticMethods(schema, f, message);
78
- f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";");
79
+ f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";");
79
80
  f.print();
80
- f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";");
81
+ f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";");
81
82
  f.print();
82
- f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";");
83
+ f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";");
83
84
  f.print();
84
- f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;");
85
+ f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;");
85
86
  f.print("}");
86
87
  f.print();
87
88
  for (const nestedEnum of message.nestedEnums) {
@@ -125,8 +126,9 @@ function generateField(schema, f, field) {
125
126
  // prettier-ignore
126
127
  function generateExtension(schema, f, ext) {
127
128
  const { typing } = (0, util_js_1.getFieldTypeInfo)(ext);
129
+ const e = f.import(ext.extendee).toTypeOnly();
128
130
  f.print(f.jsDoc(ext));
129
- f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
131
+ f.print(f.exportDecl("declare const", (0, ecmascript_1.localName)(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
130
132
  f.print();
131
133
  }
132
134
  // prettier-ignore
@@ -43,15 +43,12 @@ function generateEnum(schema, f, enumeration) {
43
43
  f.print(f.exportDecl("const", enumeration), " = /*@__PURE__*/ ", protoN, ".makeEnum(");
44
44
  f.print(` "`, enumeration.typeName, `",`);
45
45
  f.print(` [`);
46
- if (enumeration.sharedPrefix === undefined) {
47
- for (const value of enumeration.values) {
46
+ for (const value of enumeration.values) {
47
+ if ((0, ecmascript_1.localName)(value) === value.name) {
48
48
  f.print(" {no: ", value.number, ", name: ", f.string(value.name), "},");
49
49
  }
50
- }
51
- else {
52
- for (const value of enumeration.values) {
53
- const localName = value.name.substring(enumeration.sharedPrefix.length);
54
- f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string(localName), "},");
50
+ else {
51
+ f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string((0, ecmascript_1.localName)(value)), "},");
55
52
  }
56
53
  }
57
54
  f.print(` ],`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -16,15 +16,15 @@
16
16
  },
17
17
  "scripts": {
18
18
  "clean": "rm -rf ./dist/cjs/*",
19
- "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs"
19
+ "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/cjs"
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.8.0",
24
- "@bufbuild/protoplugin": "1.8.0"
23
+ "@bufbuild/protobuf": "^1.10.0",
24
+ "@bufbuild/protoplugin": "1.10.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.8.0"
27
+ "@bufbuild/protobuf": "1.10.0"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -1,248 +0,0 @@
1
- // Copyright 2021-2024 Buf Technologies, Inc.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type {
16
- DescEnum,
17
- DescField,
18
- DescMessage,
19
- DescOneof,
20
- } from "@bufbuild/protobuf";
21
- import { DescExtension } from "@bufbuild/protobuf";
22
- import type {
23
- GeneratedFile,
24
- Printable,
25
- Schema,
26
- } from "@bufbuild/protoplugin/ecmascript";
27
- import { localName, reifyWkt } from "@bufbuild/protoplugin/ecmascript";
28
- import { getNonEditionRuntime } from "./editions.js";
29
- import { getFieldTypeInfo } from "./util.js";
30
-
31
- export function generateDts(schema: Schema) {
32
- for (const file of schema.files) {
33
- const f = schema.generateFile(file.name + "_pb.d.ts");
34
- f.preamble(file);
35
- for (const enumeration of file.enums) {
36
- generateEnum(schema, f, enumeration);
37
- }
38
- for (const message of file.messages) {
39
- generateMessage(schema, f, message);
40
- }
41
- for (const extension of file.extensions) {
42
- generateExtension(schema, f, extension);
43
- }
44
- // We do not generate anything for services
45
- }
46
- }
47
-
48
- // prettier-ignore
49
- function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
50
- f.print(f.jsDoc(enumeration));
51
- f.print("export declare enum ", enumeration, " {");
52
- for (const value of enumeration.values) {
53
- if (enumeration.values.indexOf(value) > 0) {
54
- f.print();
55
- }
56
- f.print(f.jsDoc(value, " "));
57
- f.print(" ", localName(value), " = ", value.number, ",");
58
- }
59
- f.print("}");
60
- f.print();
61
- }
62
-
63
- // prettier-ignore
64
- function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
65
- const protoN = getNonEditionRuntime(schema, message.file);
66
- const {
67
- PartialMessage,
68
- FieldList,
69
- Message,
70
- PlainMessage,
71
- BinaryReadOptions,
72
- JsonReadOptions,
73
- JsonValue
74
- } = schema.runtime;
75
- f.print(f.jsDoc(message));
76
- f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
77
- for (const member of message.members) {
78
- switch (member.kind) {
79
- case "oneof":
80
- generateOneof(schema, f, member);
81
- break;
82
- default:
83
- generateField(schema, f, member);
84
- break;
85
- }
86
- f.print();
87
- }
88
- f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
89
- f.print();
90
- generateWktMethods(schema, f, message);
91
- f.print(" static readonly runtime: typeof ", protoN, ";");
92
- f.print(' static readonly typeName = ', f.string(message.typeName), ';');
93
- f.print(" static readonly fields: ", FieldList, ";");
94
- // In case we start supporting options, we have to surface them here
95
- //f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
96
- f.print();
97
- generateWktStaticMethods(schema, f, message);
98
- f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";")
99
- f.print()
100
- f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";")
101
- f.print()
102
- f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";")
103
- f.print()
104
- f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;")
105
- f.print("}")
106
- f.print()
107
- for (const nestedEnum of message.nestedEnums) {
108
- generateEnum(schema, f, nestedEnum);
109
- }
110
- for (const nestedMessage of message.nestedMessages) {
111
- generateMessage(schema, f, nestedMessage);
112
- }
113
- for (const nestedExtension of message.nestedExtensions) {
114
- generateExtension(schema, f, nestedExtension);
115
- }
116
- }
117
-
118
- // prettier-ignore
119
- function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
120
- f.print(f.jsDoc(oneof, " "));
121
- f.print(" ", localName(oneof), ": {");
122
- for (const field of oneof.fields) {
123
- if (oneof.fields.indexOf(field) > 0) {
124
- f.print(` } | {`);
125
- }
126
- f.print(f.jsDoc(field, " "));
127
- const { typing } = getFieldTypeInfo(field);
128
- f.print(` value: `, typing, `;`);
129
- f.print(` case: "`, localName(field), `";`);
130
- }
131
- f.print(` } | { case: undefined; value?: undefined };`);
132
- }
133
-
134
- // prettier-ignore
135
- function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
136
- f.print(f.jsDoc(field, " "));
137
- const e: Printable = [];
138
- const { typing, optional } = getFieldTypeInfo(field);
139
- if (!optional) {
140
- e.push(" ", localName(field), ": ", typing, ";");
141
- } else {
142
- e.push(" ", localName(field), "?: ", typing, ";");
143
- }
144
- f.print(e);
145
- }
146
-
147
- // prettier-ignore
148
- function generateExtension(
149
- schema: Schema,
150
- f: GeneratedFile,
151
- ext: DescExtension,
152
- ) {
153
- const { typing } = getFieldTypeInfo(ext);
154
- f.print(f.jsDoc(ext));
155
- f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
156
- f.print();
157
- }
158
-
159
- // prettier-ignore
160
- function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
161
- const ref = reifyWkt(message);
162
- if (ref === undefined) {
163
- return;
164
- }
165
- const {
166
- Message,
167
- MessageType,
168
- IMessageTypeRegistry
169
- } = schema.runtime;
170
- switch (ref.typeName) {
171
- case "google.protobuf.Any":
172
- f.print(" packFrom(message: ", Message, "): void;");
173
- f.print();
174
- f.print(" unpackTo(target: ", Message, "): boolean;");
175
- f.print();
176
- f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
177
- f.print();
178
- f.print(" is(type: ", MessageType, " | string): boolean;");
179
- f.print();
180
- f.print(" private typeNameToUrl(name: string): string;");
181
- f.print();
182
- f.print(" private typeUrlToName(url: string): string;");
183
- f.print();
184
- break;
185
- case "google.protobuf.Timestamp":
186
- f.print(" toDate(): Date;");
187
- f.print();
188
- break;
189
- case "google.protobuf.Duration":
190
- case "google.protobuf.Struct":
191
- case "google.protobuf.Value":
192
- case "google.protobuf.ListValue":
193
- case "google.protobuf.FieldMask":
194
- case "google.protobuf.DoubleValue":
195
- case "google.protobuf.FloatValue":
196
- case "google.protobuf.Int64Value":
197
- case "google.protobuf.UInt64Value":
198
- case "google.protobuf.Int32Value":
199
- case "google.protobuf.UInt32Value":
200
- case "google.protobuf.BoolValue":
201
- case "google.protobuf.StringValue":
202
- case "google.protobuf.BytesValue":
203
- break;
204
- }
205
- }
206
-
207
- // prettier-ignore
208
- function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
209
- const ref = reifyWkt(message);
210
- if (ref === undefined) {
211
- return;
212
- }
213
- switch (ref.typeName) {
214
- case "google.protobuf.Any":
215
- f.print(" static pack(message: Message): ", message, ";")
216
- f.print()
217
- break;
218
- case "google.protobuf.Timestamp":
219
- f.print(" static now(): ", message, ";")
220
- f.print()
221
- f.print(" static fromDate(date: Date): ", message, ";")
222
- f.print()
223
- break;
224
- case "google.protobuf.DoubleValue":
225
- case "google.protobuf.FloatValue":
226
- case "google.protobuf.Int64Value":
227
- case "google.protobuf.UInt64Value":
228
- case "google.protobuf.Int32Value":
229
- case "google.protobuf.UInt32Value":
230
- case "google.protobuf.BoolValue":
231
- case "google.protobuf.StringValue":
232
- case "google.protobuf.BytesValue": {
233
- const {typing} = getFieldTypeInfo(ref.value);
234
- f.print(" static readonly fieldWrapper: {")
235
- f.print(" wrapField(value: ", typing, "): ", message, ",")
236
- f.print(" unwrapField(value: ", message, "): ", typing, ",")
237
- f.print(" };")
238
- f.print()
239
- break;
240
- }
241
- case "google.protobuf.Duration":
242
- case "google.protobuf.Struct":
243
- case "google.protobuf.Value":
244
- case "google.protobuf.ListValue":
245
- case "google.protobuf.FieldMask":
246
- break;
247
- }
248
- }
package/src/editions.ts DELETED
@@ -1,38 +0,0 @@
1
- // Copyright 2021-2024 Buf Technologies, Inc.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type { ImportSymbol, Schema } from "@bufbuild/protoplugin/ecmascript";
16
- import { DescFile } from "@bufbuild/protobuf";
17
-
18
- /**
19
- * Temporary function to retrieve the import symbol for the proto2 or proto3
20
- * runtime.
21
- *
22
- * For syntax "editions", this function raises and error.
23
- *
24
- * Once support for "editions" is implemented in the runtime, this function can
25
- * be removed.
26
- */
27
- export function getNonEditionRuntime(
28
- schema: Schema,
29
- file: DescFile,
30
- ): ImportSymbol {
31
- if (file.syntax === "editions") {
32
- // TODO support editions
33
- throw new Error(
34
- `${file.proto.name ?? ""}: syntax "editions" is not supported yet`,
35
- );
36
- }
37
- return schema.runtime[file.syntax];
38
- }