@bufbuild/protoc-gen-es 0.0.8 → 0.1.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.
@@ -0,0 +1,221 @@
1
+ // Copyright 2021-2022 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 type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
22
+ import {
23
+ getFieldTyping,
24
+ literalString,
25
+ localName,
26
+ makeJsDoc,
27
+ } from "@bufbuild/protoplugin/ecmascript";
28
+ import { matchWkt } from "./match-wkt.js";
29
+
30
+ export const declaration = {
31
+ target: "dts",
32
+ extension: "_pb.d.ts",
33
+ generateEnum,
34
+ generateMessage,
35
+ } as const;
36
+
37
+ // prettier-ignore
38
+ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
39
+ f.print(makeJsDoc(enumeration));
40
+ f.print("export declare enum ", enumeration, " {");
41
+ for (const value of enumeration.values) {
42
+ if (enumeration.values.indexOf(value) > 0) {
43
+ f.print();
44
+ }
45
+ f.print(makeJsDoc(value, " "));
46
+ f.print(" ", localName(value), " = ", value.number, ",");
47
+ }
48
+ f.print("}");
49
+ f.print();
50
+ }
51
+
52
+ // prettier-ignore
53
+ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
54
+ const protoN = schema.runtime[message.file.syntax];
55
+ const {
56
+ PartialMessage,
57
+ FieldList,
58
+ Message,
59
+ PlainMessage,
60
+ BinaryReadOptions,
61
+ JsonReadOptions,
62
+ JsonValue
63
+ } = schema.runtime;
64
+ f.print(makeJsDoc(message));
65
+ f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
66
+ for (const member of message.members) {
67
+ switch (member.kind) {
68
+ case "oneof":
69
+ generateOneof(schema, f, member);
70
+ break;
71
+ default:
72
+ generateField(schema, f, member);
73
+ break;
74
+ }
75
+ f.print();
76
+ }
77
+ f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
78
+ f.print();
79
+ generateWktMethods(schema, f, message);
80
+ f.print(" static readonly runtime: typeof ", protoN, ";");
81
+ f.print(' static readonly typeName = ', literalString(message.typeName), ';');
82
+ f.print(" static readonly fields: ", FieldList, ";");
83
+ // In case we start supporting options, we have to surface them here
84
+ //f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
85
+ f.print();
86
+ generateWktStaticMethods(schema, f, message);
87
+ f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";")
88
+ f.print()
89
+ f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";")
90
+ f.print()
91
+ f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";")
92
+ f.print()
93
+ f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;")
94
+ f.print("}")
95
+ f.print()
96
+ for (const nestedEnum of message.nestedEnums) {
97
+ generateEnum(schema, f, nestedEnum);
98
+ }
99
+ for (const nestedMessage of message.nestedMessages) {
100
+ generateMessage(schema, f, nestedMessage);
101
+ }
102
+ // We do not support extensions at this time
103
+ }
104
+
105
+ // prettier-ignore
106
+ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
107
+ f.print(makeJsDoc(oneof, " "));
108
+ f.print(" ", localName(oneof), ": {");
109
+ for (const field of oneof.fields) {
110
+ if (oneof.fields.indexOf(field) > 0) {
111
+ f.print(` } | {`);
112
+ }
113
+ f.print(makeJsDoc(field, " "));
114
+ const { typing } = getFieldTyping(field, f);
115
+ f.print(` value: `, typing, `;`);
116
+ f.print(` case: "`, localName(field), `";`);
117
+ }
118
+ f.print(` } | { case: undefined; value?: undefined };`);
119
+ }
120
+
121
+ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
122
+ f.print(makeJsDoc(field, " "));
123
+ const e: Parameters<typeof f.print> = [];
124
+ e.push(" ", localName(field));
125
+ const { typing, optional } = getFieldTyping(field, f);
126
+ if (optional) {
127
+ e.push("?: ", typing);
128
+ } else {
129
+ e.push(": ", typing);
130
+ }
131
+ e.push(";");
132
+ f.print(e);
133
+ }
134
+
135
+ // prettier-ignore
136
+ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
137
+ const ref = matchWkt(message);
138
+ if (ref === undefined) {
139
+ return;
140
+ }
141
+ const {
142
+ Message,
143
+ MessageType,
144
+ } = schema.runtime;
145
+ switch (ref.typeName) {
146
+ case "google.protobuf.Any":
147
+ f.print(" packFrom(message: ", Message, "): void;");
148
+ f.print();
149
+ f.print(" unpackTo(target: ", Message, "): boolean;");
150
+ f.print();
151
+ f.print(" is(type: ", MessageType, "): boolean;");
152
+ f.print();
153
+ f.print(" private typeNameToUrl(name: string): string;");
154
+ f.print();
155
+ f.print(" private typeUrlToName(url: string): string;");
156
+ f.print();
157
+ break;
158
+ case "google.protobuf.Timestamp":
159
+ f.print(" toDate(): Date;");
160
+ f.print();
161
+ break;
162
+ case "google.protobuf.Duration":
163
+ case "google.protobuf.Struct":
164
+ case "google.protobuf.Value":
165
+ case "google.protobuf.ListValue":
166
+ case "google.protobuf.FieldMask":
167
+ case "google.protobuf.DoubleValue":
168
+ case "google.protobuf.FloatValue":
169
+ case "google.protobuf.Int64Value":
170
+ case "google.protobuf.UInt64Value":
171
+ case "google.protobuf.Int32Value":
172
+ case "google.protobuf.UInt32Value":
173
+ case "google.protobuf.BoolValue":
174
+ case "google.protobuf.StringValue":
175
+ case "google.protobuf.BytesValue":
176
+ break;
177
+ }
178
+ }
179
+
180
+ // prettier-ignore
181
+ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
182
+ const ref = matchWkt(message);
183
+ if (ref === undefined) {
184
+ return;
185
+ }
186
+ switch (ref.typeName) {
187
+ case "google.protobuf.Any":
188
+ f.print(" static pack(message: Message): ", message, ";")
189
+ f.print()
190
+ break;
191
+ case "google.protobuf.Timestamp":
192
+ f.print(" static now(): ", message, ";")
193
+ f.print()
194
+ f.print(" static fromDate(date: Date): ", message, ";")
195
+ f.print()
196
+ break;
197
+ case "google.protobuf.DoubleValue":
198
+ case "google.protobuf.FloatValue":
199
+ case "google.protobuf.Int64Value":
200
+ case "google.protobuf.UInt64Value":
201
+ case "google.protobuf.Int32Value":
202
+ case "google.protobuf.UInt32Value":
203
+ case "google.protobuf.BoolValue":
204
+ case "google.protobuf.StringValue":
205
+ case "google.protobuf.BytesValue": {
206
+ const {typing} = getFieldTyping(ref.value, f);
207
+ f.print(" static readonly fieldWrapper: {")
208
+ f.print(" wrapField(value: ", typing, " | ", message, "): ", message, ",")
209
+ f.print(" unwrapField(value: ", message, "): ", typing, ",")
210
+ f.print(" };")
211
+ f.print()
212
+ break;
213
+ }
214
+ case "google.protobuf.Duration":
215
+ case "google.protobuf.Struct":
216
+ case "google.protobuf.Value":
217
+ case "google.protobuf.ListValue":
218
+ case "google.protobuf.FieldMask":
219
+ break;
220
+ }
221
+ }