@bufbuild/protoc-gen-es 1.9.0 → 2.0.0-alpha.2
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/README.md +22 -24
- package/dist/cjs/package.json +9 -5
- package/dist/cjs/src/protoc-gen-es-plugin.js +320 -6
- package/dist/cjs/src/util.js +65 -195
- package/package.json +9 -5
- package/dist/cjs/src/declaration.js +0 -218
- package/dist/cjs/src/editions.js +0 -34
- package/dist/cjs/src/javascript.js +0 -625
- package/dist/cjs/src/typescript.js +0 -639
- package/src/declaration.ts +0 -250
- package/src/editions.ts +0 -38
- package/src/javascript.ts +0 -649
- 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 -12
package/src/declaration.ts
DELETED
|
@@ -1,250 +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(f.exportDecl("declare enum", localName(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
|
-
const m = localName(message);
|
|
76
|
-
f.print(f.jsDoc(message));
|
|
77
|
-
f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
|
|
78
|
-
for (const member of message.members) {
|
|
79
|
-
switch (member.kind) {
|
|
80
|
-
case "oneof":
|
|
81
|
-
generateOneof(schema, f, member);
|
|
82
|
-
break;
|
|
83
|
-
default:
|
|
84
|
-
generateField(schema, f, member);
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
f.print();
|
|
88
|
-
}
|
|
89
|
-
f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
|
|
90
|
-
f.print();
|
|
91
|
-
generateWktMethods(schema, f, message);
|
|
92
|
-
f.print(" static readonly runtime: typeof ", protoN, ";");
|
|
93
|
-
f.print(' static readonly typeName = ', f.string(message.typeName), ';');
|
|
94
|
-
f.print(" static readonly fields: ", FieldList, ";");
|
|
95
|
-
// In case we start supporting options, we have to surface them here
|
|
96
|
-
//f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
|
|
97
|
-
f.print();
|
|
98
|
-
generateWktStaticMethods(schema, f, message);
|
|
99
|
-
f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";")
|
|
100
|
-
f.print()
|
|
101
|
-
f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";")
|
|
102
|
-
f.print()
|
|
103
|
-
f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";")
|
|
104
|
-
f.print()
|
|
105
|
-
f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;")
|
|
106
|
-
f.print("}")
|
|
107
|
-
f.print()
|
|
108
|
-
for (const nestedEnum of message.nestedEnums) {
|
|
109
|
-
generateEnum(schema, f, nestedEnum);
|
|
110
|
-
}
|
|
111
|
-
for (const nestedMessage of message.nestedMessages) {
|
|
112
|
-
generateMessage(schema, f, nestedMessage);
|
|
113
|
-
}
|
|
114
|
-
for (const nestedExtension of message.nestedExtensions) {
|
|
115
|
-
generateExtension(schema, f, nestedExtension);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// prettier-ignore
|
|
120
|
-
function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
|
|
121
|
-
f.print(f.jsDoc(oneof, " "));
|
|
122
|
-
f.print(" ", localName(oneof), ": {");
|
|
123
|
-
for (const field of oneof.fields) {
|
|
124
|
-
if (oneof.fields.indexOf(field) > 0) {
|
|
125
|
-
f.print(` } | {`);
|
|
126
|
-
}
|
|
127
|
-
f.print(f.jsDoc(field, " "));
|
|
128
|
-
const { typing } = getFieldTypeInfo(field);
|
|
129
|
-
f.print(` value: `, typing, `;`);
|
|
130
|
-
f.print(` case: "`, localName(field), `";`);
|
|
131
|
-
}
|
|
132
|
-
f.print(` } | { case: undefined; value?: undefined };`);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// prettier-ignore
|
|
136
|
-
function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
|
|
137
|
-
f.print(f.jsDoc(field, " "));
|
|
138
|
-
const e: Printable = [];
|
|
139
|
-
const { typing, optional } = getFieldTypeInfo(field);
|
|
140
|
-
if (!optional) {
|
|
141
|
-
e.push(" ", localName(field), ": ", typing, ";");
|
|
142
|
-
} else {
|
|
143
|
-
e.push(" ", localName(field), "?: ", typing, ";");
|
|
144
|
-
}
|
|
145
|
-
f.print(e);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// prettier-ignore
|
|
149
|
-
function generateExtension(
|
|
150
|
-
schema: Schema,
|
|
151
|
-
f: GeneratedFile,
|
|
152
|
-
ext: DescExtension,
|
|
153
|
-
) {
|
|
154
|
-
const { typing } = getFieldTypeInfo(ext);
|
|
155
|
-
const e = f.import(ext.extendee).toTypeOnly();
|
|
156
|
-
f.print(f.jsDoc(ext));
|
|
157
|
-
f.print(f.exportDecl("declare const", localName(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
|
|
158
|
-
f.print();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// prettier-ignore
|
|
162
|
-
function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
|
|
163
|
-
const ref = reifyWkt(message);
|
|
164
|
-
if (ref === undefined) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const {
|
|
168
|
-
Message,
|
|
169
|
-
MessageType,
|
|
170
|
-
IMessageTypeRegistry
|
|
171
|
-
} = schema.runtime;
|
|
172
|
-
switch (ref.typeName) {
|
|
173
|
-
case "google.protobuf.Any":
|
|
174
|
-
f.print(" packFrom(message: ", Message, "): void;");
|
|
175
|
-
f.print();
|
|
176
|
-
f.print(" unpackTo(target: ", Message, "): boolean;");
|
|
177
|
-
f.print();
|
|
178
|
-
f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
|
|
179
|
-
f.print();
|
|
180
|
-
f.print(" is(type: ", MessageType, " | string): boolean;");
|
|
181
|
-
f.print();
|
|
182
|
-
f.print(" private typeNameToUrl(name: string): string;");
|
|
183
|
-
f.print();
|
|
184
|
-
f.print(" private typeUrlToName(url: string): string;");
|
|
185
|
-
f.print();
|
|
186
|
-
break;
|
|
187
|
-
case "google.protobuf.Timestamp":
|
|
188
|
-
f.print(" toDate(): Date;");
|
|
189
|
-
f.print();
|
|
190
|
-
break;
|
|
191
|
-
case "google.protobuf.Duration":
|
|
192
|
-
case "google.protobuf.Struct":
|
|
193
|
-
case "google.protobuf.Value":
|
|
194
|
-
case "google.protobuf.ListValue":
|
|
195
|
-
case "google.protobuf.FieldMask":
|
|
196
|
-
case "google.protobuf.DoubleValue":
|
|
197
|
-
case "google.protobuf.FloatValue":
|
|
198
|
-
case "google.protobuf.Int64Value":
|
|
199
|
-
case "google.protobuf.UInt64Value":
|
|
200
|
-
case "google.protobuf.Int32Value":
|
|
201
|
-
case "google.protobuf.UInt32Value":
|
|
202
|
-
case "google.protobuf.BoolValue":
|
|
203
|
-
case "google.protobuf.StringValue":
|
|
204
|
-
case "google.protobuf.BytesValue":
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// prettier-ignore
|
|
210
|
-
function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
|
|
211
|
-
const ref = reifyWkt(message);
|
|
212
|
-
if (ref === undefined) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
switch (ref.typeName) {
|
|
216
|
-
case "google.protobuf.Any":
|
|
217
|
-
f.print(" static pack(message: Message): ", message, ";")
|
|
218
|
-
f.print()
|
|
219
|
-
break;
|
|
220
|
-
case "google.protobuf.Timestamp":
|
|
221
|
-
f.print(" static now(): ", message, ";")
|
|
222
|
-
f.print()
|
|
223
|
-
f.print(" static fromDate(date: Date): ", message, ";")
|
|
224
|
-
f.print()
|
|
225
|
-
break;
|
|
226
|
-
case "google.protobuf.DoubleValue":
|
|
227
|
-
case "google.protobuf.FloatValue":
|
|
228
|
-
case "google.protobuf.Int64Value":
|
|
229
|
-
case "google.protobuf.UInt64Value":
|
|
230
|
-
case "google.protobuf.Int32Value":
|
|
231
|
-
case "google.protobuf.UInt32Value":
|
|
232
|
-
case "google.protobuf.BoolValue":
|
|
233
|
-
case "google.protobuf.StringValue":
|
|
234
|
-
case "google.protobuf.BytesValue": {
|
|
235
|
-
const {typing} = getFieldTypeInfo(ref.value);
|
|
236
|
-
f.print(" static readonly fieldWrapper: {")
|
|
237
|
-
f.print(" wrapField(value: ", typing, "): ", message, ",")
|
|
238
|
-
f.print(" unwrapField(value: ", message, "): ", typing, ",")
|
|
239
|
-
f.print(" };")
|
|
240
|
-
f.print()
|
|
241
|
-
break;
|
|
242
|
-
}
|
|
243
|
-
case "google.protobuf.Duration":
|
|
244
|
-
case "google.protobuf.Struct":
|
|
245
|
-
case "google.protobuf.Value":
|
|
246
|
-
case "google.protobuf.ListValue":
|
|
247
|
-
case "google.protobuf.FieldMask":
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
}
|
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
|
-
}
|