@bufbuild/protoc-gen-es 1.4.2 → 1.5.1

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.4.2",
3
+ "version": "1.5.1",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,11 +20,11 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.4.2",
24
- "@bufbuild/protoplugin": "1.4.2"
23
+ "@bufbuild/protobuf": "^1.5.1",
24
+ "@bufbuild/protoplugin": "1.5.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.4.2"
27
+ "@bufbuild/protobuf": "1.5.1"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -15,6 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateDts = void 0;
17
17
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
18
+ const editions_js_1 = require("./editions.js");
18
19
  function generateDts(schema) {
19
20
  for (const file of schema.files) {
20
21
  const f = schema.generateFile(file.name + "_pb.d.ts");
@@ -45,7 +46,7 @@ function generateEnum(schema, f, enumeration) {
45
46
  }
46
47
  // prettier-ignore
47
48
  function generateMessage(schema, f, message) {
48
- const protoN = schema.runtime[message.file.syntax];
49
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
49
50
  const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
50
51
  f.print((0, ecmascript_1.makeJsDoc)(message));
51
52
  f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ // Copyright 2021-2023 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.getNonEditionRuntime = void 0;
17
+ /**
18
+ * Temporary function to retrieve the import symbol for the proto2 or proto3
19
+ * runtime.
20
+ *
21
+ * For syntax "editions", this function raises and error.
22
+ *
23
+ * Once support for "editions" is implemented in the runtime, this function can
24
+ * be removed.
25
+ */
26
+ function getNonEditionRuntime(schema, file) {
27
+ var _a;
28
+ if (file.syntax === "editions") {
29
+ // TODO support editions
30
+ throw new Error(`${(_a = file.proto.name) !== null && _a !== void 0 ? _a : ""}: syntax "editions" is not supported yet`);
31
+ }
32
+ return schema.runtime[file.syntax];
33
+ }
34
+ exports.getNonEditionRuntime = getNonEditionRuntime;
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateFieldInfo = exports.generateJs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
+ const editions_js_1 = require("./editions.js");
19
20
  function generateJs(schema) {
20
21
  for (const file of schema.files) {
21
22
  const f = schema.generateFile(file.name + "_pb.js");
@@ -32,7 +33,7 @@ function generateJs(schema) {
32
33
  exports.generateJs = generateJs;
33
34
  // prettier-ignore
34
35
  function generateEnum(schema, f, enumeration) {
35
- const protoN = schema.runtime[enumeration.file.syntax];
36
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, enumeration.file);
36
37
  f.print((0, ecmascript_1.makeJsDoc)(enumeration));
37
38
  f.print("export const ", enumeration, " = ", protoN, ".makeEnum(");
38
39
  f.print(` "`, enumeration.typeName, `",`);
@@ -54,7 +55,7 @@ function generateEnum(schema, f, enumeration) {
54
55
  }
55
56
  // prettier-ignore
56
57
  function generateMessage(schema, f, message) {
57
- const protoN = schema.runtime[message.file.syntax];
58
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
58
59
  f.print((0, ecmascript_1.makeJsDoc)(message));
59
60
  f.print("export const ", message, " = ", protoN, ".makeMessageType(");
60
61
  f.print(` `, (0, ecmascript_1.literalString)(message.typeName), `,`);
@@ -89,7 +90,7 @@ function generateMessage(schema, f, message) {
89
90
  }
90
91
  // prettier-ignore
91
92
  function generateFieldInfo(schema, f, field) {
92
- const protoN = schema.runtime[field.parent.file.syntax];
93
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, field.parent.file);
93
94
  const e = [];
94
95
  e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
95
96
  if (field.jsonName !== undefined) {
@@ -118,6 +119,9 @@ function generateFieldInfo(schema, f, field) {
118
119
  break;
119
120
  case "message":
120
121
  e.push(`kind: "message", T: `, field.message, `, `);
122
+ if (field.proto.type === protobuf_1.FieldDescriptorProto_Type.GROUP) {
123
+ e.push(`delimited: true, `);
124
+ }
121
125
  break;
122
126
  case "enum":
123
127
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
@@ -155,7 +159,7 @@ function generateWktMethods(schema, f, message) {
155
159
  return;
156
160
  }
157
161
  const { ScalarType: rtScalarType, protoInt64, } = schema.runtime;
158
- const protoN = schema.runtime[message.file.syntax];
162
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
159
163
  switch (ref.typeName) {
160
164
  case "google.protobuf.Any":
161
165
  f.print(message, ".prototype.toJson = function toJson(options) {");
@@ -250,7 +254,7 @@ function generateWktMethods(schema, f, message) {
250
254
  f.print(" throw new Error(`invalid type url: ${url}`);");
251
255
  f.print(" }");
252
256
  f.print(` const slash = url.lastIndexOf("/");`);
253
- f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
257
+ f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
254
258
  f.print(" if (!name.length) {");
255
259
  f.print(" throw new Error(`invalid type url: ${url}`);");
256
260
  f.print(" }");
@@ -17,6 +17,7 @@ exports.generateTs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
19
  const javascript_js_1 = require("./javascript.js");
20
+ const editions_js_1 = require("./editions.js");
20
21
  function generateTs(schema) {
21
22
  for (const file of schema.files) {
22
23
  const f = schema.generateFile(file.name + "_pb.ts");
@@ -33,7 +34,7 @@ function generateTs(schema) {
33
34
  exports.generateTs = generateTs;
34
35
  // prettier-ignore
35
36
  function generateEnum(schema, f, enumeration) {
36
- const protoN = schema.runtime[enumeration.file.syntax];
37
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, enumeration.file);
37
38
  f.print((0, ecmascript_1.makeJsDoc)(enumeration));
38
39
  f.print("export enum ", enumeration, " {");
39
40
  for (const value of enumeration.values) {
@@ -54,7 +55,7 @@ function generateEnum(schema, f, enumeration) {
54
55
  }
55
56
  // prettier-ignore
56
57
  function generateMessage(schema, f, message) {
57
- const protoN = schema.runtime[message.file.syntax];
58
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
58
59
  const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
59
60
  f.print((0, ecmascript_1.makeJsDoc)(message));
60
61
  f.print("export class ", message, " extends ", Message, "<", message, "> {");
@@ -152,7 +153,7 @@ function generateWktMethods(schema, f, message) {
152
153
  return;
153
154
  }
154
155
  const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, LongType: rtLongType, protoInt64, } = schema.runtime;
155
- const protoN = schema.runtime[message.file.syntax];
156
+ const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
156
157
  switch (ref.typeName) {
157
158
  case "google.protobuf.Any":
158
159
  f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
@@ -247,7 +248,7 @@ function generateWktMethods(schema, f, message) {
247
248
  f.print(" throw new Error(`invalid type url: ${url}`);");
248
249
  f.print(" }");
249
250
  f.print(` const slash = url.lastIndexOf("/");`);
250
- f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
251
+ f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
251
252
  f.print(" if (!name.length) {");
252
253
  f.print(" throw new Error(`invalid type url: ${url}`);");
253
254
  f.print(" }");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.4.2",
3
+ "version": "1.5.1",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,11 +20,11 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.4.2",
24
- "@bufbuild/protoplugin": "1.4.2"
23
+ "@bufbuild/protobuf": "^1.5.1",
24
+ "@bufbuild/protoplugin": "1.5.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.4.2"
27
+ "@bufbuild/protobuf": "1.5.1"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -30,6 +30,7 @@ import {
30
30
  makeJsDoc,
31
31
  reifyWkt,
32
32
  } from "@bufbuild/protoplugin/ecmascript";
33
+ import { getNonEditionRuntime } from "./editions.js";
33
34
 
34
35
  export function generateDts(schema: Schema) {
35
36
  for (const file of schema.files) {
@@ -62,7 +63,7 @@ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
62
63
 
63
64
  // prettier-ignore
64
65
  function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
65
- const protoN = schema.runtime[message.file.syntax];
66
+ const protoN = getNonEditionRuntime(schema, message.file);
66
67
  const {
67
68
  PartialMessage,
68
69
  FieldList,
@@ -0,0 +1,38 @@
1
+ // Copyright 2021-2023 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
+ }
package/src/javascript.ts CHANGED
@@ -13,7 +13,13 @@
13
13
  // limitations under the License.
14
14
 
15
15
  import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
16
- import { LongType, proto2, proto3, ScalarType } from "@bufbuild/protobuf";
16
+ import {
17
+ FieldDescriptorProto_Type,
18
+ LongType,
19
+ proto2,
20
+ proto3,
21
+ ScalarType,
22
+ } from "@bufbuild/protobuf";
17
23
  import type {
18
24
  GeneratedFile,
19
25
  Printable,
@@ -26,6 +32,7 @@ import {
26
32
  makeJsDoc,
27
33
  reifyWkt,
28
34
  } from "@bufbuild/protoplugin/ecmascript";
35
+ import { getNonEditionRuntime } from "./editions.js";
29
36
 
30
37
  export function generateJs(schema: Schema) {
31
38
  for (const file of schema.files) {
@@ -43,7 +50,7 @@ export function generateJs(schema: Schema) {
43
50
 
44
51
  // prettier-ignore
45
52
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
46
- const protoN = schema.runtime[enumeration.file.syntax];
53
+ const protoN = getNonEditionRuntime(schema, enumeration.file);
47
54
  f.print(makeJsDoc(enumeration));
48
55
  f.print("export const ", enumeration, " = ", protoN, ".makeEnum(")
49
56
  f.print(` "`, enumeration.typeName, `",`)
@@ -65,7 +72,7 @@ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
65
72
 
66
73
  // prettier-ignore
67
74
  function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
68
- const protoN = schema.runtime[message.file.syntax];
75
+ const protoN = getNonEditionRuntime(schema, message.file);
69
76
  f.print(makeJsDoc(message));
70
77
  f.print("export const ", message, " = ", protoN, ".makeMessageType(")
71
78
  f.print(` `, literalString(message.typeName), `,`)
@@ -100,7 +107,7 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
100
107
 
101
108
  // prettier-ignore
102
109
  export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
103
- const protoN = schema.runtime[field.parent.file.syntax];
110
+ const protoN = getNonEditionRuntime(schema, field.parent.file);
104
111
  const e: Printable = [];
105
112
  e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
106
113
  if (field.jsonName !== undefined) {
@@ -129,6 +136,9 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
129
136
  break;
130
137
  case "message":
131
138
  e.push(`kind: "message", T: `, field.message, `, `);
139
+ if (field.proto.type === FieldDescriptorProto_Type.GROUP) {
140
+ e.push(`delimited: true, `);
141
+ }
132
142
  break;
133
143
  case "enum":
134
144
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
@@ -168,7 +178,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
168
178
  ScalarType: rtScalarType,
169
179
  protoInt64,
170
180
  } = schema.runtime;
171
- const protoN = schema.runtime[message.file.syntax];
181
+ const protoN = getNonEditionRuntime(schema, message.file);
172
182
  switch (ref.typeName) {
173
183
  case "google.protobuf.Any":
174
184
  f.print(message, ".prototype.toJson = function toJson(options) {")
@@ -263,7 +273,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
263
273
  f.print(" throw new Error(`invalid type url: ${url}`);");
264
274
  f.print(" }");
265
275
  f.print(` const slash = url.lastIndexOf("/");`);
266
- f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
276
+ f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
267
277
  f.print(" if (!name.length) {");
268
278
  f.print(" throw new Error(`invalid type url: ${url}`);");
269
279
  f.print(" }");
package/src/typescript.ts CHANGED
@@ -33,6 +33,7 @@ import {
33
33
  reifyWkt,
34
34
  } from "@bufbuild/protoplugin/ecmascript";
35
35
  import { generateFieldInfo } from "./javascript.js";
36
+ import { getNonEditionRuntime } from "./editions.js";
36
37
 
37
38
  export function generateTs(schema: Schema) {
38
39
  for (const file of schema.files) {
@@ -50,7 +51,7 @@ export function generateTs(schema: Schema) {
50
51
 
51
52
  // prettier-ignore
52
53
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
53
- const protoN = schema.runtime[enumeration.file.syntax];
54
+ const protoN = getNonEditionRuntime(schema, enumeration.file);
54
55
  f.print(makeJsDoc(enumeration));
55
56
  f.print("export enum ", enumeration, " {");
56
57
  for (const value of enumeration.values) {
@@ -72,7 +73,7 @@ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
72
73
 
73
74
  // prettier-ignore
74
75
  function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
75
- const protoN = schema.runtime[message.file.syntax];
76
+ const protoN = getNonEditionRuntime(schema, message.file);
76
77
  const {
77
78
  PartialMessage,
78
79
  FieldList,
@@ -191,7 +192,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
191
192
  LongType: rtLongType,
192
193
  protoInt64,
193
194
  } = schema.runtime;
194
- const protoN = schema.runtime[message.file.syntax];
195
+ const protoN = getNonEditionRuntime(schema, message.file);
195
196
  switch (ref.typeName) {
196
197
  case "google.protobuf.Any":
197
198
  f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
@@ -286,7 +287,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
286
287
  f.print(" throw new Error(`invalid type url: ${url}`);");
287
288
  f.print(" }");
288
289
  f.print(` const slash = url.lastIndexOf("/");`);
289
- f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
290
+ f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
290
291
  f.print(" if (!name.length) {");
291
292
  f.print(" throw new Error(`invalid type url: ${url}`);");
292
293
  f.print(" }");