@bufbuild/protoc-gen-es 1.4.1 → 1.5.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 +4 -3
- package/dist/cjs/src/declaration.js +2 -1
- package/dist/cjs/src/editions.js +34 -0
- package/dist/cjs/src/javascript.js +6 -5
- package/dist/cjs/src/typescript.js +5 -4
- package/package.json +4 -3
- package/src/declaration.ts +2 -1
- package/src/editions.ts +38 -0
- package/src/javascript.ts +6 -5
- package/src/typescript.ts +5 -4
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"preferUnplugged": true,
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@bufbuild/
|
|
23
|
+
"@bufbuild/protobuf": "^1.5.0",
|
|
24
|
+
"@bufbuild/protoplugin": "1.5.0"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "1.
|
|
27
|
+
"@bufbuild/protobuf": "1.5.0"
|
|
27
28
|
},
|
|
28
29
|
"peerDependenciesMeta": {
|
|
29
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
|
|
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
|
|
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
|
|
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
|
|
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) {
|
|
@@ -155,7 +156,7 @@ function generateWktMethods(schema, f, message) {
|
|
|
155
156
|
return;
|
|
156
157
|
}
|
|
157
158
|
const { ScalarType: rtScalarType, protoInt64, } = schema.runtime;
|
|
158
|
-
const protoN = schema
|
|
159
|
+
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
159
160
|
switch (ref.typeName) {
|
|
160
161
|
case "google.protobuf.Any":
|
|
161
162
|
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
@@ -250,7 +251,7 @@ function generateWktMethods(schema, f, message) {
|
|
|
250
251
|
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
251
252
|
f.print(" }");
|
|
252
253
|
f.print(` const slash = url.lastIndexOf("/");`);
|
|
253
|
-
f.print(" const name = slash
|
|
254
|
+
f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
|
|
254
255
|
f.print(" if (!name.length) {");
|
|
255
256
|
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
256
257
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -20,10 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"preferUnplugged": true,
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@bufbuild/
|
|
23
|
+
"@bufbuild/protobuf": "^1.5.0",
|
|
24
|
+
"@bufbuild/protoplugin": "1.5.0"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "1.
|
|
27
|
+
"@bufbuild/protobuf": "1.5.0"
|
|
27
28
|
},
|
|
28
29
|
"peerDependenciesMeta": {
|
|
29
30
|
"@bufbuild/protobuf": {
|
package/src/declaration.ts
CHANGED
|
@@ -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
|
|
66
|
+
const protoN = getNonEditionRuntime(schema, message.file);
|
|
66
67
|
const {
|
|
67
68
|
PartialMessage,
|
|
68
69
|
FieldList,
|
package/src/editions.ts
ADDED
|
@@ -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
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
makeJsDoc,
|
|
27
27
|
reifyWkt,
|
|
28
28
|
} from "@bufbuild/protoplugin/ecmascript";
|
|
29
|
+
import { getNonEditionRuntime } from "./editions.js";
|
|
29
30
|
|
|
30
31
|
export function generateJs(schema: Schema) {
|
|
31
32
|
for (const file of schema.files) {
|
|
@@ -43,7 +44,7 @@ export function generateJs(schema: Schema) {
|
|
|
43
44
|
|
|
44
45
|
// prettier-ignore
|
|
45
46
|
function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
|
|
46
|
-
const protoN = schema
|
|
47
|
+
const protoN = getNonEditionRuntime(schema, enumeration.file);
|
|
47
48
|
f.print(makeJsDoc(enumeration));
|
|
48
49
|
f.print("export const ", enumeration, " = ", protoN, ".makeEnum(")
|
|
49
50
|
f.print(` "`, enumeration.typeName, `",`)
|
|
@@ -65,7 +66,7 @@ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
|
|
|
65
66
|
|
|
66
67
|
// prettier-ignore
|
|
67
68
|
function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage) {
|
|
68
|
-
const protoN = schema
|
|
69
|
+
const protoN = getNonEditionRuntime(schema, message.file);
|
|
69
70
|
f.print(makeJsDoc(message));
|
|
70
71
|
f.print("export const ", message, " = ", protoN, ".makeMessageType(")
|
|
71
72
|
f.print(` `, literalString(message.typeName), `,`)
|
|
@@ -100,7 +101,7 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
|
|
|
100
101
|
|
|
101
102
|
// prettier-ignore
|
|
102
103
|
export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
|
|
103
|
-
const protoN = schema
|
|
104
|
+
const protoN = getNonEditionRuntime(schema, field.parent.file);
|
|
104
105
|
const e: Printable = [];
|
|
105
106
|
e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
|
|
106
107
|
if (field.jsonName !== undefined) {
|
|
@@ -168,7 +169,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
168
169
|
ScalarType: rtScalarType,
|
|
169
170
|
protoInt64,
|
|
170
171
|
} = schema.runtime;
|
|
171
|
-
const protoN = schema
|
|
172
|
+
const protoN = getNonEditionRuntime(schema, message.file);
|
|
172
173
|
switch (ref.typeName) {
|
|
173
174
|
case "google.protobuf.Any":
|
|
174
175
|
f.print(message, ".prototype.toJson = function toJson(options) {")
|
|
@@ -263,7 +264,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
263
264
|
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
264
265
|
f.print(" }");
|
|
265
266
|
f.print(` const slash = url.lastIndexOf("/");`);
|
|
266
|
-
f.print(" const name = slash
|
|
267
|
+
f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
|
|
267
268
|
f.print(" if (!name.length) {");
|
|
268
269
|
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
269
270
|
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
|
|
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
|
|
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
|
|
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
|
|
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(" }");
|