@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.
- package/dist/cjs/package.json +5 -5
- package/dist/cjs/src/declaration.js +10 -8
- package/dist/cjs/src/javascript.js +4 -7
- package/package.json +5 -5
- package/src/declaration.ts +0 -248
- package/src/editions.ts +0 -38
- package/src/javascript.ts +0 -652
- package/src/protoc-gen-es-plugin.ts +0 -27
- package/src/typescript.ts +0 -673
- package/src/util.ts +0 -315
- package/tsconfig.json +0 -7
package/src/util.ts
DELETED
|
@@ -1,315 +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 {
|
|
16
|
-
codegenInfo,
|
|
17
|
-
DescEnumValue,
|
|
18
|
-
DescExtension,
|
|
19
|
-
DescField,
|
|
20
|
-
FieldDescriptorProto_Label,
|
|
21
|
-
LongType,
|
|
22
|
-
ScalarType,
|
|
23
|
-
ScalarValue,
|
|
24
|
-
} from "@bufbuild/protobuf";
|
|
25
|
-
import type { Printable } from "@bufbuild/protoplugin/ecmascript";
|
|
26
|
-
import { localName } from "@bufbuild/protoplugin/ecmascript";
|
|
27
|
-
|
|
28
|
-
export function getFieldTypeInfo(field: DescField | DescExtension): {
|
|
29
|
-
typing: Printable;
|
|
30
|
-
optional: boolean;
|
|
31
|
-
typingInferrableFromZeroValue: boolean;
|
|
32
|
-
} {
|
|
33
|
-
const typing: Printable = [];
|
|
34
|
-
let typingInferrableFromZeroValue: boolean;
|
|
35
|
-
let optional = false;
|
|
36
|
-
switch (field.fieldKind) {
|
|
37
|
-
case "scalar":
|
|
38
|
-
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
39
|
-
optional =
|
|
40
|
-
field.optional ||
|
|
41
|
-
field.proto.label === FieldDescriptorProto_Label.REQUIRED;
|
|
42
|
-
typingInferrableFromZeroValue = true;
|
|
43
|
-
break;
|
|
44
|
-
case "message": {
|
|
45
|
-
const baseType = codegenInfo.getUnwrappedFieldType(field);
|
|
46
|
-
if (baseType !== undefined) {
|
|
47
|
-
typing.push(scalarTypeScriptType(baseType, LongType.BIGINT));
|
|
48
|
-
} else {
|
|
49
|
-
typing.push({
|
|
50
|
-
kind: "es_ref_message",
|
|
51
|
-
type: field.message,
|
|
52
|
-
typeOnly: true,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
optional = true;
|
|
56
|
-
typingInferrableFromZeroValue = true;
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
case "enum":
|
|
60
|
-
typing.push({
|
|
61
|
-
kind: "es_ref_enum",
|
|
62
|
-
type: field.enum,
|
|
63
|
-
typeOnly: true,
|
|
64
|
-
});
|
|
65
|
-
optional =
|
|
66
|
-
field.optional ||
|
|
67
|
-
field.proto.label === FieldDescriptorProto_Label.REQUIRED;
|
|
68
|
-
typingInferrableFromZeroValue = true;
|
|
69
|
-
break;
|
|
70
|
-
case "map": {
|
|
71
|
-
let keyType: string;
|
|
72
|
-
switch (field.mapKey) {
|
|
73
|
-
case ScalarType.INT32:
|
|
74
|
-
case ScalarType.FIXED32:
|
|
75
|
-
case ScalarType.UINT32:
|
|
76
|
-
case ScalarType.SFIXED32:
|
|
77
|
-
case ScalarType.SINT32:
|
|
78
|
-
keyType = "number";
|
|
79
|
-
break;
|
|
80
|
-
default:
|
|
81
|
-
keyType = "string";
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
let valueType: Printable;
|
|
85
|
-
switch (field.mapValue.kind) {
|
|
86
|
-
case "scalar":
|
|
87
|
-
valueType = scalarTypeScriptType(
|
|
88
|
-
field.mapValue.scalar,
|
|
89
|
-
LongType.BIGINT,
|
|
90
|
-
);
|
|
91
|
-
break;
|
|
92
|
-
case "message":
|
|
93
|
-
valueType = {
|
|
94
|
-
kind: "es_ref_message",
|
|
95
|
-
type: field.mapValue.message,
|
|
96
|
-
typeOnly: true,
|
|
97
|
-
};
|
|
98
|
-
break;
|
|
99
|
-
case "enum":
|
|
100
|
-
valueType = {
|
|
101
|
-
kind: "es_ref_enum",
|
|
102
|
-
type: field.mapValue.enum,
|
|
103
|
-
typeOnly: true,
|
|
104
|
-
};
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
108
|
-
typingInferrableFromZeroValue = false;
|
|
109
|
-
optional = false;
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (field.repeated) {
|
|
114
|
-
typing.push("[]");
|
|
115
|
-
optional = false;
|
|
116
|
-
typingInferrableFromZeroValue = false;
|
|
117
|
-
}
|
|
118
|
-
return { typing, optional, typingInferrableFromZeroValue };
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Return a printable expression for the default value of a field.
|
|
123
|
-
* Only applicable for singular scalar and enum fields.
|
|
124
|
-
*/
|
|
125
|
-
export function getFieldDefaultValueExpression(
|
|
126
|
-
field: DescField | DescExtension,
|
|
127
|
-
enumAs:
|
|
128
|
-
| "enum_value_as_is"
|
|
129
|
-
| "enum_value_as_integer"
|
|
130
|
-
| "enum_value_as_cast_integer" = "enum_value_as_is",
|
|
131
|
-
): Printable | undefined {
|
|
132
|
-
if (field.repeated) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
if (field.fieldKind !== "enum" && field.fieldKind !== "scalar") {
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
const defaultValue = field.getDefaultValue();
|
|
139
|
-
if (defaultValue === undefined) {
|
|
140
|
-
return undefined;
|
|
141
|
-
}
|
|
142
|
-
switch (field.fieldKind) {
|
|
143
|
-
case "enum": {
|
|
144
|
-
const enumValue = field.enum.values.find(
|
|
145
|
-
(value) => value.number === defaultValue,
|
|
146
|
-
);
|
|
147
|
-
if (enumValue === undefined) {
|
|
148
|
-
throw new Error(
|
|
149
|
-
`invalid enum default value: ${String(defaultValue)} for ${enumValue}`,
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
return literalEnumValue(enumValue, enumAs);
|
|
153
|
-
}
|
|
154
|
-
case "scalar":
|
|
155
|
-
return literalScalarValue(defaultValue, field);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Return a printable expression for the zero value of a field.
|
|
161
|
-
*
|
|
162
|
-
* Returns either:
|
|
163
|
-
* - empty array literal for repeated fields
|
|
164
|
-
* - empty object literal for maps
|
|
165
|
-
* - undefined for message fields
|
|
166
|
-
* - an enums first value
|
|
167
|
-
* - scalar zero value
|
|
168
|
-
*/
|
|
169
|
-
export function getFieldZeroValueExpression(
|
|
170
|
-
field: DescField | DescExtension,
|
|
171
|
-
enumAs:
|
|
172
|
-
| "enum_value_as_is"
|
|
173
|
-
| "enum_value_as_integer"
|
|
174
|
-
| "enum_value_as_cast_integer" = "enum_value_as_is",
|
|
175
|
-
): Printable | undefined {
|
|
176
|
-
if (field.repeated) {
|
|
177
|
-
return "[]";
|
|
178
|
-
}
|
|
179
|
-
switch (field.fieldKind) {
|
|
180
|
-
case "message":
|
|
181
|
-
return undefined;
|
|
182
|
-
case "map":
|
|
183
|
-
return "{}";
|
|
184
|
-
case "enum": {
|
|
185
|
-
// In proto3, the first enum value must be zero.
|
|
186
|
-
// In proto2, protobuf-go returns the first value as the default.
|
|
187
|
-
if (field.enum.values.length < 1) {
|
|
188
|
-
throw new Error("invalid enum: missing at least one value");
|
|
189
|
-
}
|
|
190
|
-
const zeroValue = field.enum.values[0];
|
|
191
|
-
return literalEnumValue(zeroValue, enumAs);
|
|
192
|
-
}
|
|
193
|
-
case "scalar": {
|
|
194
|
-
const defaultValue = codegenInfo.scalarZeroValue(
|
|
195
|
-
field.scalar,
|
|
196
|
-
field.longType,
|
|
197
|
-
);
|
|
198
|
-
return literalScalarValue(defaultValue, field);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function literalScalarValue(
|
|
204
|
-
value: ScalarValue,
|
|
205
|
-
field: (DescField | DescExtension) & { fieldKind: "scalar" },
|
|
206
|
-
): Printable {
|
|
207
|
-
switch (field.scalar) {
|
|
208
|
-
case ScalarType.DOUBLE:
|
|
209
|
-
case ScalarType.FLOAT:
|
|
210
|
-
case ScalarType.INT32:
|
|
211
|
-
case ScalarType.FIXED32:
|
|
212
|
-
case ScalarType.UINT32:
|
|
213
|
-
case ScalarType.SFIXED32:
|
|
214
|
-
case ScalarType.SINT32:
|
|
215
|
-
if (typeof value != "number") {
|
|
216
|
-
throw new Error(
|
|
217
|
-
`Unexpected value for ${ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`,
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
return value;
|
|
221
|
-
case ScalarType.BOOL:
|
|
222
|
-
if (typeof value != "boolean") {
|
|
223
|
-
throw new Error(
|
|
224
|
-
`Unexpected value for ${ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`,
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
return value;
|
|
228
|
-
case ScalarType.STRING:
|
|
229
|
-
if (typeof value != "string") {
|
|
230
|
-
throw new Error(
|
|
231
|
-
`Unexpected value for ${ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`,
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
return { kind: "es_string", value };
|
|
235
|
-
case ScalarType.BYTES:
|
|
236
|
-
if (!(value instanceof Uint8Array)) {
|
|
237
|
-
throw new Error(
|
|
238
|
-
`Unexpected value for ${ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`,
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
return value;
|
|
242
|
-
case ScalarType.INT64:
|
|
243
|
-
case ScalarType.SINT64:
|
|
244
|
-
case ScalarType.SFIXED64:
|
|
245
|
-
case ScalarType.UINT64:
|
|
246
|
-
case ScalarType.FIXED64:
|
|
247
|
-
if (typeof value != "bigint" && typeof value != "string") {
|
|
248
|
-
throw new Error(
|
|
249
|
-
`Unexpected value for ${ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`,
|
|
250
|
-
);
|
|
251
|
-
}
|
|
252
|
-
return {
|
|
253
|
-
kind: "es_proto_int64",
|
|
254
|
-
type: field.scalar,
|
|
255
|
-
longType: field.longType,
|
|
256
|
-
value,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function literalEnumValue(
|
|
262
|
-
value: DescEnumValue,
|
|
263
|
-
enumAs:
|
|
264
|
-
| "enum_value_as_is"
|
|
265
|
-
| "enum_value_as_integer"
|
|
266
|
-
| "enum_value_as_cast_integer",
|
|
267
|
-
): Printable {
|
|
268
|
-
switch (enumAs) {
|
|
269
|
-
case "enum_value_as_is":
|
|
270
|
-
return [
|
|
271
|
-
{ kind: "es_ref_enum", type: value.parent, typeOnly: false },
|
|
272
|
-
".",
|
|
273
|
-
localName(value),
|
|
274
|
-
];
|
|
275
|
-
case "enum_value_as_integer":
|
|
276
|
-
return [
|
|
277
|
-
value.number,
|
|
278
|
-
" /* ",
|
|
279
|
-
value.parent.typeName,
|
|
280
|
-
".",
|
|
281
|
-
value.name,
|
|
282
|
-
" */",
|
|
283
|
-
];
|
|
284
|
-
case "enum_value_as_cast_integer":
|
|
285
|
-
return [
|
|
286
|
-
value.number,
|
|
287
|
-
" as ",
|
|
288
|
-
{ kind: "es_ref_enum", type: value.parent, typeOnly: true },
|
|
289
|
-
".",
|
|
290
|
-
localName(value),
|
|
291
|
-
];
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function scalarTypeScriptType(type: ScalarType, longType: LongType): Printable {
|
|
296
|
-
switch (type) {
|
|
297
|
-
case ScalarType.STRING:
|
|
298
|
-
return "string";
|
|
299
|
-
case ScalarType.BOOL:
|
|
300
|
-
return "boolean";
|
|
301
|
-
case ScalarType.UINT64:
|
|
302
|
-
case ScalarType.SFIXED64:
|
|
303
|
-
case ScalarType.FIXED64:
|
|
304
|
-
case ScalarType.SINT64:
|
|
305
|
-
case ScalarType.INT64:
|
|
306
|
-
if (longType === LongType.STRING) {
|
|
307
|
-
return "string";
|
|
308
|
-
}
|
|
309
|
-
return "bigint";
|
|
310
|
-
case ScalarType.BYTES:
|
|
311
|
-
return "Uint8Array";
|
|
312
|
-
default:
|
|
313
|
-
return "number";
|
|
314
|
-
}
|
|
315
|
-
}
|