@bufbuild/protoc-gen-es 1.8.0 → 2.0.0-alpha.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.
@@ -13,31 +13,46 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.getFieldZeroValueExpression = exports.getFieldDefaultValueExpression = exports.getFieldTypeInfo = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
- const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
16
+ exports.arrayLiteral = exports.functionCall = exports.getFieldTypeInfo = exports.fieldUsesPrototype = void 0;
17
+ const reflect_1 = require("@bufbuild/protobuf/reflect");
18
+ const wkt_1 = require("@bufbuild/protobuf/wkt");
19
+ /**
20
+ * Tells whether a field uses the prototype chain for field presence.
21
+ * Behavior must match with the counterpart in @bufbuild/protobuf.
22
+ */
23
+ function fieldUsesPrototype(field) {
24
+ if (field.parent.file.edition != wkt_1.Edition.EDITION_PROTO2) {
25
+ return false;
26
+ }
27
+ if (field.fieldKind != "scalar" && field.fieldKind != "enum") {
28
+ return false;
29
+ }
30
+ if (field.oneof) {
31
+ return false;
32
+ }
33
+ // proto2 singular scalar and enum fields use an initial value on the prototype chain
34
+ return true;
35
+ }
36
+ exports.fieldUsesPrototype = fieldUsesPrototype;
19
37
  function getFieldTypeInfo(field) {
20
38
  const typing = [];
21
39
  let typingInferrableFromZeroValue;
22
40
  let optional = false;
23
41
  switch (field.fieldKind) {
24
42
  case "scalar":
25
- typing.push(scalarTypeScriptType(field.scalar, field.longType));
26
- optional =
27
- field.optional ||
28
- field.proto.label === protobuf_1.FieldDescriptorProto_Label.REQUIRED;
43
+ typing.push((0, reflect_1.scalarTypeScriptType)(field.scalar, field.longType));
44
+ optional = field.optional;
29
45
  typingInferrableFromZeroValue = true;
30
46
  break;
31
47
  case "message": {
32
- const baseType = protobuf_1.codegenInfo.getUnwrappedFieldType(field);
33
- if (baseType !== undefined) {
34
- typing.push(scalarTypeScriptType(baseType, protobuf_1.LongType.BIGINT));
48
+ if (!field.oneof && (0, wkt_1.isWrapperDesc)(field.message)) {
49
+ const baseType = field.message.fields[0].scalar;
50
+ typing.push((0, reflect_1.scalarTypeScriptType)(baseType, reflect_1.LongType.BIGINT));
35
51
  }
36
52
  else {
37
53
  typing.push({
38
- kind: "es_ref_message",
39
- type: field.message,
40
- typeOnly: true,
54
+ kind: "es_shape_ref",
55
+ desc: field.message,
41
56
  });
42
57
  }
43
58
  optional = true;
@@ -46,23 +61,42 @@ function getFieldTypeInfo(field) {
46
61
  }
47
62
  case "enum":
48
63
  typing.push({
49
- kind: "es_ref_enum",
50
- type: field.enum,
51
- typeOnly: true,
64
+ kind: "es_shape_ref",
65
+ desc: field.enum,
52
66
  });
53
- optional =
54
- field.optional ||
55
- field.proto.label === protobuf_1.FieldDescriptorProto_Label.REQUIRED;
67
+ optional = field.optional;
56
68
  typingInferrableFromZeroValue = true;
57
69
  break;
70
+ case "list":
71
+ optional = false;
72
+ typingInferrableFromZeroValue = false;
73
+ switch (field.listKind) {
74
+ case "enum":
75
+ typing.push({
76
+ kind: "es_shape_ref",
77
+ desc: field.enum,
78
+ }, "[]");
79
+ break;
80
+ case "scalar":
81
+ typing.push((0, reflect_1.scalarTypeScriptType)(field.scalar, field.longType), "[]");
82
+ break;
83
+ case "message": {
84
+ typing.push({
85
+ kind: "es_shape_ref",
86
+ desc: field.message,
87
+ }, "[]");
88
+ break;
89
+ }
90
+ }
91
+ break;
58
92
  case "map": {
59
93
  let keyType;
60
94
  switch (field.mapKey) {
61
- case protobuf_1.ScalarType.INT32:
62
- case protobuf_1.ScalarType.FIXED32:
63
- case protobuf_1.ScalarType.UINT32:
64
- case protobuf_1.ScalarType.SFIXED32:
65
- case protobuf_1.ScalarType.SINT32:
95
+ case reflect_1.ScalarType.INT32:
96
+ case reflect_1.ScalarType.FIXED32:
97
+ case reflect_1.ScalarType.UINT32:
98
+ case reflect_1.ScalarType.SFIXED32:
99
+ case reflect_1.ScalarType.SINT32:
66
100
  keyType = "number";
67
101
  break;
68
102
  default:
@@ -70,22 +104,20 @@ function getFieldTypeInfo(field) {
70
104
  break;
71
105
  }
72
106
  let valueType;
73
- switch (field.mapValue.kind) {
107
+ switch (field.mapKind) {
74
108
  case "scalar":
75
- valueType = scalarTypeScriptType(field.mapValue.scalar, protobuf_1.LongType.BIGINT);
109
+ valueType = (0, reflect_1.scalarTypeScriptType)(field.scalar, reflect_1.LongType.BIGINT);
76
110
  break;
77
111
  case "message":
78
112
  valueType = {
79
- kind: "es_ref_message",
80
- type: field.mapValue.message,
81
- typeOnly: true,
113
+ kind: "es_shape_ref",
114
+ desc: field.message,
82
115
  };
83
116
  break;
84
117
  case "enum":
85
118
  valueType = {
86
- kind: "es_ref_enum",
87
- type: field.mapValue.enum,
88
- typeOnly: true,
119
+ kind: "es_shape_ref",
120
+ desc: field.enum,
89
121
  };
90
122
  break;
91
123
  }
@@ -95,166 +127,28 @@ function getFieldTypeInfo(field) {
95
127
  break;
96
128
  }
97
129
  }
98
- if (field.repeated) {
99
- typing.push("[]");
100
- optional = false;
101
- typingInferrableFromZeroValue = false;
102
- }
103
130
  return { typing, optional, typingInferrableFromZeroValue };
104
131
  }
105
132
  exports.getFieldTypeInfo = getFieldTypeInfo;
106
- /**
107
- * Return a printable expression for the default value of a field.
108
- * Only applicable for singular scalar and enum fields.
109
- */
110
- function getFieldDefaultValueExpression(field, enumAs = "enum_value_as_is") {
111
- if (field.repeated) {
112
- return undefined;
113
- }
114
- if (field.fieldKind !== "enum" && field.fieldKind !== "scalar") {
115
- return undefined;
116
- }
117
- const defaultValue = field.getDefaultValue();
118
- if (defaultValue === undefined) {
119
- return undefined;
120
- }
121
- switch (field.fieldKind) {
122
- case "enum": {
123
- const enumValue = field.enum.values.find((value) => value.number === defaultValue);
124
- if (enumValue === undefined) {
125
- throw new Error(`invalid enum default value: ${String(defaultValue)} for ${enumValue}`);
126
- }
127
- return literalEnumValue(enumValue, enumAs);
128
- }
129
- case "scalar":
130
- return literalScalarValue(defaultValue, field);
131
- }
132
- }
133
- exports.getFieldDefaultValueExpression = getFieldDefaultValueExpression;
134
- /**
135
- * Return a printable expression for the zero value of a field.
136
- *
137
- * Returns either:
138
- * - empty array literal for repeated fields
139
- * - empty object literal for maps
140
- * - undefined for message fields
141
- * - an enums first value
142
- * - scalar zero value
143
- */
144
- function getFieldZeroValueExpression(field, enumAs = "enum_value_as_is") {
145
- if (field.repeated) {
146
- return "[]";
147
- }
148
- switch (field.fieldKind) {
149
- case "message":
150
- return undefined;
151
- case "map":
152
- return "{}";
153
- case "enum": {
154
- // In proto3, the first enum value must be zero.
155
- // In proto2, protobuf-go returns the first value as the default.
156
- if (field.enum.values.length < 1) {
157
- throw new Error("invalid enum: missing at least one value");
158
- }
159
- const zeroValue = field.enum.values[0];
160
- return literalEnumValue(zeroValue, enumAs);
161
- }
162
- case "scalar": {
163
- const defaultValue = protobuf_1.codegenInfo.scalarZeroValue(field.scalar, field.longType);
164
- return literalScalarValue(defaultValue, field);
165
- }
133
+ function functionCall(fn, args, typeParams) {
134
+ let tp = [];
135
+ if (typeParams !== undefined && typeParams.length > 0) {
136
+ tp = ["<", commaSeparate(typeParams), ">"];
166
137
  }
138
+ return [fn, ...tp, "(", commaSeparate(args), ")"];
167
139
  }
168
- exports.getFieldZeroValueExpression = getFieldZeroValueExpression;
169
- function literalScalarValue(value, field) {
170
- switch (field.scalar) {
171
- case protobuf_1.ScalarType.DOUBLE:
172
- case protobuf_1.ScalarType.FLOAT:
173
- case protobuf_1.ScalarType.INT32:
174
- case protobuf_1.ScalarType.FIXED32:
175
- case protobuf_1.ScalarType.UINT32:
176
- case protobuf_1.ScalarType.SFIXED32:
177
- case protobuf_1.ScalarType.SINT32:
178
- if (typeof value != "number") {
179
- throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
180
- }
181
- return value;
182
- case protobuf_1.ScalarType.BOOL:
183
- if (typeof value != "boolean") {
184
- throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
185
- }
186
- return value;
187
- case protobuf_1.ScalarType.STRING:
188
- if (typeof value != "string") {
189
- throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
190
- }
191
- return { kind: "es_string", value };
192
- case protobuf_1.ScalarType.BYTES:
193
- if (!(value instanceof Uint8Array)) {
194
- throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
195
- }
196
- return value;
197
- case protobuf_1.ScalarType.INT64:
198
- case protobuf_1.ScalarType.SINT64:
199
- case protobuf_1.ScalarType.SFIXED64:
200
- case protobuf_1.ScalarType.UINT64:
201
- case protobuf_1.ScalarType.FIXED64:
202
- if (typeof value != "bigint" && typeof value != "string") {
203
- throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
204
- }
205
- return {
206
- kind: "es_proto_int64",
207
- type: field.scalar,
208
- longType: field.longType,
209
- value,
210
- };
211
- }
212
- }
213
- function literalEnumValue(value, enumAs) {
214
- switch (enumAs) {
215
- case "enum_value_as_is":
216
- return [
217
- { kind: "es_ref_enum", type: value.parent, typeOnly: false },
218
- ".",
219
- (0, ecmascript_1.localName)(value),
220
- ];
221
- case "enum_value_as_integer":
222
- return [
223
- value.number,
224
- " /* ",
225
- value.parent.typeName,
226
- ".",
227
- value.name,
228
- " */",
229
- ];
230
- case "enum_value_as_cast_integer":
231
- return [
232
- value.number,
233
- " as ",
234
- { kind: "es_ref_enum", type: value.parent, typeOnly: true },
235
- ".",
236
- (0, ecmascript_1.localName)(value),
237
- ];
238
- }
140
+ exports.functionCall = functionCall;
141
+ function arrayLiteral(elements) {
142
+ return ["[", commaSeparate(elements), "]"];
239
143
  }
240
- function scalarTypeScriptType(type, longType) {
241
- switch (type) {
242
- case protobuf_1.ScalarType.STRING:
243
- return "string";
244
- case protobuf_1.ScalarType.BOOL:
245
- return "boolean";
246
- case protobuf_1.ScalarType.UINT64:
247
- case protobuf_1.ScalarType.SFIXED64:
248
- case protobuf_1.ScalarType.FIXED64:
249
- case protobuf_1.ScalarType.SINT64:
250
- case protobuf_1.ScalarType.INT64:
251
- if (longType === protobuf_1.LongType.STRING) {
252
- return "string";
253
- }
254
- return "bigint";
255
- case protobuf_1.ScalarType.BYTES:
256
- return "Uint8Array";
257
- default:
258
- return "number";
144
+ exports.arrayLiteral = arrayLiteral;
145
+ function commaSeparate(elements) {
146
+ const r = [];
147
+ for (let i = 0; i < elements.length; i++) {
148
+ r.push(elements[i]);
149
+ if (i < elements.length - 1) {
150
+ r.push(", ");
151
+ }
259
152
  }
153
+ return r;
260
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.8.0",
3
+ "version": "2.0.0-alpha.1",
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": "^2.0.0-alpha.1",
24
+ "@bufbuild/protoplugin": "2.0.0-alpha.1"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.8.0"
27
+ "@bufbuild/protobuf": "2.0.0-alpha.1"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -12,10 +12,27 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
 
15
+ import type {
16
+ DescEnum,
17
+ DescFile,
18
+ DescMessage,
19
+ DescService,
20
+ } from "@bufbuild/protobuf";
21
+ import { localName } from "@bufbuild/protobuf/reflect";
22
+ import { embedFileDesc, pathInFileDesc } from "@bufbuild/protobuf/codegenv1";
15
23
  import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
16
- import { generateTs } from "./typescript.js";
17
- import { generateJs } from "./javascript.js";
18
- import { generateDts } from "./declaration.js";
24
+ import type {
25
+ Schema,
26
+ GeneratedFile,
27
+ Printable,
28
+ Target,
29
+ } from "@bufbuild/protoplugin/ecmascript";
30
+ import {
31
+ arrayLiteral,
32
+ fieldUsesPrototype,
33
+ functionCall,
34
+ getFieldTypeInfo,
35
+ } from "./util";
19
36
  import { version } from "../package.json";
20
37
 
21
38
  export const protocGenEs = createEcmaScriptPlugin({
@@ -25,3 +42,295 @@ export const protocGenEs = createEcmaScriptPlugin({
25
42
  generateJs,
26
43
  generateDts,
27
44
  });
45
+
46
+ // This annotation informs bundlers that the succeeding function call is free of
47
+ // side effects. This means the symbol can be removed from the module during
48
+ // tree-shaking if it is unused.
49
+ // See https://github.com/bufbuild/protobuf-es/pull/470
50
+ const pure = "/*@__PURE__*/";
51
+
52
+ // prettier-ignore
53
+ function generateTs(schema: Schema) {
54
+ for (const file of schema.files) {
55
+ const f = schema.generateFile(file.name + "_pb.ts");
56
+ f.preamble(file);
57
+ const { GenDescFile } = f.runtime.codegen;
58
+ const fileDesc = f.importDesc(file);
59
+ f.print(f.exportDecl("const", fileDesc.name), ": ", GenDescFile, " = ", pure);
60
+ f.print(" ", getFileDescCall(f, file), ";");
61
+ f.print();
62
+ for (const desc of schema.typesInFile(file)) {
63
+ switch (desc.kind) {
64
+ case "message": {
65
+ generateMessageShape(f, desc, "ts");
66
+ const {GenDescMessage, messageDesc} = f.runtime.codegen;
67
+ const MessageShape = f.importShape(desc);
68
+ const fileDesc = f.importDesc(file);
69
+ const name = f.importDesc(desc).name;
70
+ f.print("// Describes the ", desc.toString(), ".");
71
+ f.print("// Use `create(", name, ")` to create a new ", MessageShape.name, ".");
72
+ const call = functionCall(messageDesc, [fileDesc, ...pathInFileDesc(desc)]);
73
+ f.print(f.exportDecl("const", name), ": ", GenDescMessage, "<", MessageShape, ">", " = ", pure);
74
+ f.print(" ", call, ";");
75
+ f.print();
76
+ break;
77
+ }
78
+ case "enum": {
79
+ generateEnumShape(f, desc);
80
+ const {GenDescEnum, enumDesc} = f.runtime.codegen;
81
+ const EnumShape = f.importShape(desc);
82
+ const fileDesc = f.importDesc(file);
83
+ f.print("// Describes the ", desc.toString(), ".");
84
+ const name = f.importDesc(desc).name;
85
+ const call = functionCall(enumDesc, [fileDesc, ...pathInFileDesc(desc)]);
86
+ f.print(f.exportDecl("const", name), ": ", GenDescEnum, "<", EnumShape, ">", " = ", pure);
87
+ f.print(" ", call, ";");
88
+ f.print();
89
+ break;
90
+ }
91
+ case "extension": {
92
+ const { GenDescExtension, extDesc } = f.runtime.codegen;
93
+ const name = f.importDesc(desc).name;
94
+ const E = f.importShape(desc.extendee);
95
+ const V = getFieldTypeInfo(desc).typing;
96
+ const call = functionCall(extDesc, [f.importDesc(file), ...pathInFileDesc(desc)]);
97
+ f.print(f.jsDoc(desc));
98
+ f.print(f.exportDecl("const", name), ": ", GenDescExtension, "<", E, ", ", V, ">", " = ", pure);
99
+ f.print(" ", call, ";");
100
+ f.print();
101
+ break;
102
+ }
103
+ case "service": {
104
+ const { GenDescService, serviceDesc} = f.runtime.codegen;
105
+ const name = f.importDesc(desc).name;
106
+ const call = functionCall(serviceDesc, [f.importDesc(file), ...pathInFileDesc(desc)]);
107
+ f.print(f.jsDoc(desc));
108
+ f.print(f.exportDecl("const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), "> = ", pure);
109
+ f.print(" ", call, ";");
110
+ f.print();
111
+ break;
112
+ }
113
+ }
114
+ }
115
+ }
116
+ }
117
+
118
+ // prettier-ignore
119
+ function generateJs(schema: Schema) {
120
+ for (const file of schema.files) {
121
+ const f = schema.generateFile(file.name + "_pb.js");
122
+ f.preamble(file);
123
+ const fileDesc = f.importDesc(file);
124
+ f.print(f.exportDecl("const", fileDesc.name), " = ", pure);
125
+ f.print(" ", getFileDescCall(f, file), ";");
126
+ f.print();
127
+ for (const desc of schema.typesInFile(file)) {
128
+ switch (desc.kind) {
129
+ case "message": {
130
+ const { messageDesc} = f.runtime.codegen;
131
+ const MessageShape = f.importShape(desc);
132
+ const name = f.importDesc(desc).name;
133
+ f.print("// Describes the ", desc.toString(), ". Use `create(", name, ")` to create a new ", MessageShape.name, ".");
134
+ const call = functionCall(messageDesc, [fileDesc, ...pathInFileDesc(desc)]);
135
+ f.print(f.exportDecl("const", name), " = ", pure);
136
+ f.print(" ", call, ";");
137
+ f.print();
138
+ break;
139
+ }
140
+ case "enum": {
141
+ // generate descriptor
142
+ {
143
+ const { enumDesc } = f.runtime.codegen;
144
+ f.print("// Describes the ", desc.toString(), ".");
145
+ const name = f.importDesc(desc).name;
146
+ const call = functionCall(enumDesc, [fileDesc, ...pathInFileDesc(desc)]);
147
+ f.print(f.exportDecl("const", name), " = ", pure);
148
+ f.print(" ", call, ";");
149
+ f.print();
150
+ }
151
+ // declare TypeScript enum
152
+ {
153
+ f.print(f.jsDoc(desc));
154
+ const { tsEnum } = f.runtime.codegen;
155
+ const call = functionCall(tsEnum, [f.importDesc(desc)]);
156
+ f.print(f.exportDecl("const", f.importShape(desc).name), " = ", pure);
157
+ f.print(" ", call, ";");
158
+ f.print();
159
+ }
160
+ break;
161
+ }
162
+ case "extension": {
163
+ const { extDesc } = f.runtime.codegen;
164
+ const name = f.importDesc(desc).name;
165
+ const call = functionCall(extDesc, [f.importDesc(desc.file), ...pathInFileDesc(desc)]);
166
+ f.print(f.jsDoc(desc));
167
+ f.print(f.exportDecl("const", name), " = ", pure);
168
+ f.print(" ", call, ";");
169
+ f.print();
170
+ break;
171
+ }
172
+ case "service": {
173
+ const { serviceDesc} = f.runtime.codegen;
174
+ const name = f.importDesc(desc).name;
175
+ f.print(f.jsDoc(desc));
176
+ const call = functionCall(serviceDesc, [f.importDesc(desc.file), ...pathInFileDesc(desc)]);
177
+ f.print(f.exportDecl("const", name), " = ", pure);
178
+ f.print(" ", call, ";");
179
+ f.print();
180
+ break;
181
+ }
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ // prettier-ignore
188
+ function generateDts(schema: Schema) {
189
+ for (const file of schema.files) {
190
+ const f = schema.generateFile(file.name + "_pb.d.ts");
191
+ f.preamble(file);
192
+ const { GenDescFile } = f.runtime.codegen;
193
+ const fileDesc = f.importDesc(file);
194
+ f.print(f.exportDecl("declare const", fileDesc.name), ": ", GenDescFile, ";");
195
+ f.print();
196
+ for (const desc of schema.typesInFile(file)) {
197
+ switch (desc.kind) {
198
+ case "message": {
199
+ generateMessageShape(f, desc, "dts");
200
+ const { GenDescMessage } = f.runtime.codegen;
201
+ const MessageShape = f.importShape(desc);
202
+ const name = f.importDesc(desc).name;
203
+ f.print("// Describes the ", desc.toString(), ". Use `create(", name, ")` to create a new ", MessageShape.name, ".");
204
+ f.print(f.exportDecl("declare const", name), ": ", GenDescMessage, "<", MessageShape, ">", ";");
205
+ f.print();
206
+ break;
207
+ }
208
+ case "enum": {
209
+ generateEnumShape(f, desc);
210
+ const { GenDescEnum } = f.runtime.codegen;
211
+ const EnumShape = f.importShape(desc);
212
+ f.print("// Describes the ", desc.toString(), ".");
213
+ const name = f.importDesc(desc).name;
214
+ f.print(f.exportDecl("declare const", name), ": ", GenDescEnum, "<", EnumShape, ">;");
215
+ f.print();
216
+ break;
217
+ }
218
+ case "extension": {
219
+ const { GenDescExtension } = f.runtime.codegen;
220
+ const name = f.importDesc(desc).name;
221
+ const E = f.importShape(desc.extendee);
222
+ const V = getFieldTypeInfo(desc).typing;
223
+ f.print(f.jsDoc(desc));
224
+ f.print(f.exportDecl("declare const", name), ": ", GenDescExtension, "<", E, ", ", V, ">;");
225
+ f.print();
226
+ break;
227
+ }
228
+ case "service": {
229
+ const { GenDescService } = f.runtime.codegen;
230
+ const name = f.importDesc(desc).name;
231
+ f.print(f.jsDoc(desc));
232
+ f.print(f.exportDecl("declare const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), ">;");
233
+ f.print();
234
+ break;
235
+ }
236
+ }
237
+ }
238
+ }
239
+ }
240
+
241
+ // prettier-ignore
242
+ function getFileDescCall(f: GeneratedFile, file: DescFile) {
243
+ const info = embedFileDesc(file);
244
+ if (info.bootable && !f.runtime.create.from.startsWith("@bufbuild/protobuf")) {
245
+ // google/protobuf/descriptor.proto is embedded as a plain object when
246
+ // bootstrapping to avoid recursion
247
+ return functionCall(f.runtime.codegen.boot, [JSON.stringify(info.boot())]);
248
+ }
249
+ const { fileDesc } = f.runtime.codegen;
250
+ if (file.dependencies.length > 0) {
251
+ const deps: Printable = file.dependencies.map((f) => ({
252
+ kind: "es_desc_ref",
253
+ desc: f,
254
+ }));
255
+ return functionCall(fileDesc, [
256
+ f.string(info.base64()),
257
+ arrayLiteral(deps),
258
+ ]);
259
+ }
260
+ return functionCall(fileDesc, [f.string(info.base64())]);
261
+ }
262
+
263
+ // prettier-ignore
264
+ function getServiceShapeExpr(f: GeneratedFile, service: DescService): Printable {
265
+ const p: Printable[] = [];
266
+ function print(...printables: Printable[]) {
267
+ p.push(...printables, "\n");
268
+ }
269
+ print("{");
270
+ for (const method of service.methods) {
271
+ print(f.jsDoc(method, " "));
272
+ print(" ", localName(method), ": {");
273
+ print(" kind: ", f.string(method.methodKind), ";");
274
+ print(" I: ", f.importShape(method.input), ";");
275
+ print(" O: ", f.importShape(method.output), ";");
276
+ print(" },");
277
+ }
278
+ print("}");
279
+ return p;
280
+ }
281
+
282
+ // prettier-ignore
283
+ function generateEnumShape(f: GeneratedFile, enumeration: DescEnum) {
284
+ f.print(f.jsDoc(enumeration));
285
+ f.print(f.exportDecl("enum", f.importShape(enumeration).name), " {");
286
+ for (const value of enumeration.values) {
287
+ if (enumeration.values.indexOf(value) > 0) {
288
+ f.print();
289
+ }
290
+ f.print(f.jsDoc(value, " "));
291
+ f.print(" ", localName(value), " = ", value.number, ",");
292
+ }
293
+ f.print("}");
294
+ f.print();
295
+ }
296
+
297
+ // prettier-ignore
298
+ function generateMessageShape(f: GeneratedFile, message: DescMessage, target: Extract<Target, "ts" | "dts">) {
299
+ const { Message } = f.runtime;
300
+ const declaration = target == "ts" ? "type" : "declare type";
301
+ f.print(f.jsDoc(message));
302
+ f.print(f.exportDecl(declaration, f.importShape(message).name), " = ", Message, "<", f.string(message.typeName), "> & {");
303
+ for (const member of message.members) {
304
+ switch (member.kind) {
305
+ case "oneof":
306
+ f.print(f.jsDoc(member, " "));
307
+ f.print(" ", localName(member), ": {");
308
+ for (const field of member.fields) {
309
+ if (member.fields.indexOf(field) > 0) {
310
+ f.print(` } | {`);
311
+ }
312
+ f.print(f.jsDoc(field, " "));
313
+ const { typing } = getFieldTypeInfo(field);
314
+ f.print(` value: `, typing, `;`);
315
+ f.print(` case: "`, localName(field), `";`);
316
+ }
317
+ f.print(` } | { case: undefined; value?: undefined };`);
318
+ break;
319
+ default: {
320
+ f.print(f.jsDoc(member, " "));
321
+ const {typing, optional} = getFieldTypeInfo(member);
322
+ if (fieldUsesPrototype(member) || !optional) {
323
+ f.print(" ", localName(member), ": ", typing, ";");
324
+ } else {
325
+ f.print(" ", localName(member), "?: ", typing, ";");
326
+ }
327
+ break;
328
+ }
329
+ }
330
+ if (message.members.indexOf(member) < message.members.length - 1) {
331
+ f.print();
332
+ }
333
+ }
334
+ f.print("};");
335
+ f.print();
336
+ }