@bufbuild/protoplugin 0.0.9
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 +38 -0
- package/dist/cjs/create-es-plugin.js +117 -0
- package/dist/cjs/ecmascript/gencommon.js +338 -0
- package/dist/cjs/ecmascript/generated-file.js +300 -0
- package/dist/cjs/ecmascript/import-symbol.js +34 -0
- package/dist/cjs/ecmascript/index.js +30 -0
- package/dist/cjs/ecmascript/runtime-imports.js +46 -0
- package/dist/cjs/ecmascript/schema.js +84 -0
- package/dist/cjs/ecmascript/target.js +15 -0
- package/dist/cjs/error.js +34 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugin.js +15 -0
- package/dist/cjs/run-node.js +84 -0
- package/dist/esm/create-es-plugin.js +113 -0
- package/dist/esm/ecmascript/gencommon.js +327 -0
- package/dist/esm/ecmascript/generated-file.js +296 -0
- package/dist/esm/ecmascript/import-symbol.js +30 -0
- package/dist/esm/ecmascript/index.js +21 -0
- package/dist/esm/ecmascript/runtime-imports.js +42 -0
- package/dist/esm/ecmascript/schema.js +80 -0
- package/dist/esm/ecmascript/target.js +14 -0
- package/dist/esm/error.js +29 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/plugin.js +14 -0
- package/dist/esm/run-node.js +80 -0
- package/dist/types/create-es-plugin.d.ts +21 -0
- package/dist/types/ecmascript/gencommon.d.ts +23 -0
- package/dist/types/ecmascript/generated-file.d.ts +63 -0
- package/dist/types/ecmascript/import-symbol.d.ts +39 -0
- package/dist/types/ecmascript/index.d.ts +7 -0
- package/dist/types/ecmascript/runtime-imports.d.ts +21 -0
- package/dist/types/ecmascript/schema.d.ts +41 -0
- package/dist/types/ecmascript/target.d.ts +4 -0
- package/dist/types/error.d.ts +4 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/plugin.d.ts +18 -0
- package/dist/types/run-node.d.ts +12 -0
- package/package.json +48 -0
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
import { createSchema } from "./ecmascript/schema.js";
|
|
15
|
+
import { CodeGeneratorResponse } from "@bufbuild/protobuf";
|
|
16
|
+
import { PluginOptionError } from "./error.js";
|
|
17
|
+
/**
|
|
18
|
+
* Create a new code generator plugin for ECMAScript.
|
|
19
|
+
* The plugin can generate JavaScript, TypeScript, or TypeScript declaration
|
|
20
|
+
* files.
|
|
21
|
+
*/
|
|
22
|
+
export function createEcmaScriptPlugin(init, generateFn) {
|
|
23
|
+
return {
|
|
24
|
+
name: init.name,
|
|
25
|
+
version: init.version,
|
|
26
|
+
run(req) {
|
|
27
|
+
const { targets, tsNocheck, bootstrapWkt } = parseParameter(req.parameter, init.parseOption);
|
|
28
|
+
const { schema, toResponse } = createSchema(req, targets, init.name, init.version, tsNocheck, bootstrapWkt);
|
|
29
|
+
generateFn(schema);
|
|
30
|
+
const res = new CodeGeneratorResponse();
|
|
31
|
+
toResponse(res);
|
|
32
|
+
return res;
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function parseParameter(parameter, parseOption) {
|
|
37
|
+
let targets = ["js", "dts"];
|
|
38
|
+
let tsNocheck = true;
|
|
39
|
+
let bootstrapWkt = false;
|
|
40
|
+
for (const { key, value } of splitParameter(parameter)) {
|
|
41
|
+
switch (key) {
|
|
42
|
+
case "target":
|
|
43
|
+
targets = [];
|
|
44
|
+
for (const rawTarget of value.split("+")) {
|
|
45
|
+
switch (rawTarget) {
|
|
46
|
+
case "js":
|
|
47
|
+
case "ts":
|
|
48
|
+
case "dts":
|
|
49
|
+
if (targets.indexOf(rawTarget) < 0) {
|
|
50
|
+
targets.push(rawTarget);
|
|
51
|
+
}
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
throw new PluginOptionError(`${key}=${value}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
value.split("+");
|
|
58
|
+
break;
|
|
59
|
+
case "ts_nocheck":
|
|
60
|
+
switch (value) {
|
|
61
|
+
case "true":
|
|
62
|
+
case "1":
|
|
63
|
+
tsNocheck = true;
|
|
64
|
+
break;
|
|
65
|
+
case "false":
|
|
66
|
+
case "0":
|
|
67
|
+
tsNocheck = false;
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
throw new PluginOptionError(`${key}=${value}`);
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
case "bootstrap_wkt":
|
|
74
|
+
switch (value) {
|
|
75
|
+
case "true":
|
|
76
|
+
case "1":
|
|
77
|
+
bootstrapWkt = true;
|
|
78
|
+
break;
|
|
79
|
+
case "false":
|
|
80
|
+
case "0":
|
|
81
|
+
bootstrapWkt = false;
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
throw new PluginOptionError(`${key}=${value}`);
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
if (parseOption === undefined) {
|
|
89
|
+
throw new PluginOptionError(`${key}=${value}`);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
parseOption(key, value);
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
throw new PluginOptionError(`${key}=${value}`, e);
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { targets, tsNocheck, bootstrapWkt };
|
|
101
|
+
}
|
|
102
|
+
function splitParameter(parameter) {
|
|
103
|
+
if (parameter == undefined) {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
return parameter.split(",").map((pair) => {
|
|
107
|
+
const i = pair.indexOf("=");
|
|
108
|
+
return {
|
|
109
|
+
key: i === -1 ? pair : pair.substring(0, i),
|
|
110
|
+
value: i === -1 ? "" : pair.substring(i + 1),
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
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
|
+
import { codegenInfo, ScalarType, } from "@bufbuild/protobuf";
|
|
15
|
+
const { localName, getUnwrappedFieldType, scalarDefaultValue } = codegenInfo;
|
|
16
|
+
export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
|
|
17
|
+
const builder = [];
|
|
18
|
+
const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
|
|
19
|
+
const writeLeadingComments = (comments) => {
|
|
20
|
+
for (let comment of comments.leadingDetached) {
|
|
21
|
+
comment = trimSuffix(comment, "\n");
|
|
22
|
+
for (const line of comment.split("\n")) {
|
|
23
|
+
builder.push(`//${line}\n`);
|
|
24
|
+
}
|
|
25
|
+
builder.push("\n");
|
|
26
|
+
}
|
|
27
|
+
if (comments.leading !== undefined) {
|
|
28
|
+
const comment = trimSuffix(comments.leading, "\n");
|
|
29
|
+
for (const line of comment.split("\n")) {
|
|
30
|
+
builder.push(`//${line}\n`);
|
|
31
|
+
}
|
|
32
|
+
builder.push("\n");
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
writeLeadingComments(file.getSyntaxComments());
|
|
36
|
+
builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
|
|
37
|
+
if (parameter !== undefined) {
|
|
38
|
+
builder.push(` with parameter "${parameter}"`);
|
|
39
|
+
}
|
|
40
|
+
builder.push("\n");
|
|
41
|
+
builder.push(`// @generated from file ${file.name}.proto (`);
|
|
42
|
+
if (file.proto.package !== undefined) {
|
|
43
|
+
builder.push(`package ${file.proto.package}, `);
|
|
44
|
+
}
|
|
45
|
+
builder.push(`syntax ${file.syntax})\n`);
|
|
46
|
+
builder.push("/* eslint-disable */\n");
|
|
47
|
+
if (tsNoCheck) {
|
|
48
|
+
builder.push("/* @ts-nocheck */\n");
|
|
49
|
+
}
|
|
50
|
+
builder.push("\n");
|
|
51
|
+
writeLeadingComments(file.getPackageComments());
|
|
52
|
+
return trimSuffix(builder.join(""), "\n");
|
|
53
|
+
}
|
|
54
|
+
export function createJsDocBlock(text, indentation = "") {
|
|
55
|
+
if (text.trim().length == 0) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
let lines = text.split("\n");
|
|
59
|
+
if (lines.length === 0) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
lines = lines.map((l) => l.split("*/").join("*\\/"));
|
|
63
|
+
lines = lines.map((l) => (l.length > 0 ? " " + l : l));
|
|
64
|
+
// prettier-ignore
|
|
65
|
+
return [
|
|
66
|
+
`${indentation}/**\n`,
|
|
67
|
+
...lines.map((l) => `${indentation} *${l}\n`),
|
|
68
|
+
`${indentation} */`
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
export function makeJsDoc(desc, indentation = "") {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
const comments = desc.getComments();
|
|
74
|
+
let text = "";
|
|
75
|
+
if (comments.leading !== undefined) {
|
|
76
|
+
text += comments.leading;
|
|
77
|
+
if (text.endsWith("\n")) {
|
|
78
|
+
text = text.substring(0, text.length - 1);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (comments.trailing !== undefined) {
|
|
82
|
+
if (text.length > 0) {
|
|
83
|
+
text += "\n\n";
|
|
84
|
+
}
|
|
85
|
+
text += comments.trailing;
|
|
86
|
+
if (text.endsWith("\n")) {
|
|
87
|
+
text = text.substring(0, text.length - 1);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (text.length > 0) {
|
|
91
|
+
text += "\n\n";
|
|
92
|
+
}
|
|
93
|
+
text = text
|
|
94
|
+
.split("\n")
|
|
95
|
+
.map((line) => (line.startsWith(" ") ? line.substring(1) : line))
|
|
96
|
+
.join("\n");
|
|
97
|
+
switch (desc.kind) {
|
|
98
|
+
case "enum_value":
|
|
99
|
+
text += `@generated from enum value: ${desc.declarationString()};`;
|
|
100
|
+
break;
|
|
101
|
+
case "scalar_field":
|
|
102
|
+
case "enum_field":
|
|
103
|
+
case "message_field":
|
|
104
|
+
case "map_field":
|
|
105
|
+
text += `@generated from field: ${desc.declarationString()};`;
|
|
106
|
+
break;
|
|
107
|
+
default:
|
|
108
|
+
text += `@generated from ${desc.toString()}`;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
let deprecated = desc.deprecated;
|
|
112
|
+
switch (desc.kind) {
|
|
113
|
+
case "enum":
|
|
114
|
+
case "message":
|
|
115
|
+
case "service":
|
|
116
|
+
deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
|
|
117
|
+
break;
|
|
118
|
+
default:
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
if (deprecated) {
|
|
122
|
+
text += "\n@deprecated";
|
|
123
|
+
}
|
|
124
|
+
if (text.length > 0) {
|
|
125
|
+
return createJsDocBlock(text, indentation);
|
|
126
|
+
}
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Returns an expression for the TypeScript typing of a field,
|
|
131
|
+
* and whether the property should be optional.
|
|
132
|
+
*/
|
|
133
|
+
export function getFieldTyping(field, file) {
|
|
134
|
+
const typing = [];
|
|
135
|
+
let optional = false;
|
|
136
|
+
switch (field.kind) {
|
|
137
|
+
case "scalar_field":
|
|
138
|
+
typing.push(scalarTypeScriptType(field.scalar));
|
|
139
|
+
optional = field.optional;
|
|
140
|
+
break;
|
|
141
|
+
case "message_field": {
|
|
142
|
+
const baseType = getUnwrappedFieldType(field);
|
|
143
|
+
if (baseType !== undefined) {
|
|
144
|
+
typing.push(scalarTypeScriptType(baseType));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
typing.push(file.import(field.message).toTypeOnly());
|
|
148
|
+
}
|
|
149
|
+
optional = true;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case "enum_field":
|
|
153
|
+
typing.push(file.import(field.enum).toTypeOnly());
|
|
154
|
+
optional = field.optional;
|
|
155
|
+
break;
|
|
156
|
+
case "map_field": {
|
|
157
|
+
let keyType;
|
|
158
|
+
switch (field.mapKey) {
|
|
159
|
+
case ScalarType.INT32:
|
|
160
|
+
case ScalarType.FIXED32:
|
|
161
|
+
case ScalarType.UINT32:
|
|
162
|
+
case ScalarType.SFIXED32:
|
|
163
|
+
case ScalarType.SINT32:
|
|
164
|
+
keyType = "number";
|
|
165
|
+
break;
|
|
166
|
+
default:
|
|
167
|
+
keyType = "string";
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
let valueType;
|
|
171
|
+
switch (field.mapValue.kind) {
|
|
172
|
+
case "scalar":
|
|
173
|
+
valueType = scalarTypeScriptType(field.mapValue.scalar);
|
|
174
|
+
break;
|
|
175
|
+
case "message":
|
|
176
|
+
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
177
|
+
break;
|
|
178
|
+
case "enum":
|
|
179
|
+
valueType = file.import(field.mapValue.enum).toTypeOnly();
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
183
|
+
optional = false;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (field.repeated) {
|
|
188
|
+
typing.push("[]");
|
|
189
|
+
optional = false;
|
|
190
|
+
}
|
|
191
|
+
return { typing, optional };
|
|
192
|
+
}
|
|
193
|
+
export function scalarTypeScriptType(type) {
|
|
194
|
+
switch (type) {
|
|
195
|
+
case ScalarType.STRING:
|
|
196
|
+
return "string";
|
|
197
|
+
case ScalarType.BOOL:
|
|
198
|
+
return "boolean";
|
|
199
|
+
case ScalarType.UINT64:
|
|
200
|
+
case ScalarType.SFIXED64:
|
|
201
|
+
case ScalarType.FIXED64:
|
|
202
|
+
case ScalarType.SINT64:
|
|
203
|
+
case ScalarType.INT64:
|
|
204
|
+
return "bigint";
|
|
205
|
+
case ScalarType.BYTES:
|
|
206
|
+
return "Uint8Array";
|
|
207
|
+
default:
|
|
208
|
+
return "number";
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
export function literalString(value) {
|
|
212
|
+
return ('"' +
|
|
213
|
+
value
|
|
214
|
+
.split("\\")
|
|
215
|
+
.join("\\\\")
|
|
216
|
+
.split('"')
|
|
217
|
+
.join('\\"')
|
|
218
|
+
.split("\r")
|
|
219
|
+
.join("\\r")
|
|
220
|
+
.split("\n")
|
|
221
|
+
.join("\\n") +
|
|
222
|
+
'"');
|
|
223
|
+
}
|
|
224
|
+
export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
|
|
225
|
+
switch (field.kind) {
|
|
226
|
+
case "enum_field": {
|
|
227
|
+
const value = field.enum.values.find((v) => v.number === field.getDefaultValue());
|
|
228
|
+
if (value !== undefined) {
|
|
229
|
+
return [value.parent, ".", localName(value)];
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "scalar_field": {
|
|
234
|
+
const defaultValue = field.getDefaultValue();
|
|
235
|
+
if (defaultValue === undefined) {
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
switch (field.scalar) {
|
|
239
|
+
case ScalarType.FLOAT:
|
|
240
|
+
case ScalarType.DOUBLE: {
|
|
241
|
+
return defaultValue;
|
|
242
|
+
}
|
|
243
|
+
case ScalarType.INT64:
|
|
244
|
+
case ScalarType.SINT64:
|
|
245
|
+
case ScalarType.SFIXED64:
|
|
246
|
+
return [protoInt64Symbol, `.parse("${defaultValue.toString()}")`];
|
|
247
|
+
case ScalarType.UINT64:
|
|
248
|
+
case ScalarType.FIXED64:
|
|
249
|
+
return [protoInt64Symbol, `.uParse("${defaultValue.toString()}")`];
|
|
250
|
+
case ScalarType.INT32:
|
|
251
|
+
case ScalarType.FIXED32:
|
|
252
|
+
case ScalarType.UINT32:
|
|
253
|
+
case ScalarType.SFIXED32:
|
|
254
|
+
case ScalarType.SINT32:
|
|
255
|
+
return defaultValue;
|
|
256
|
+
case ScalarType.BOOL: {
|
|
257
|
+
return defaultValue;
|
|
258
|
+
}
|
|
259
|
+
case ScalarType.STRING: {
|
|
260
|
+
if (typeof defaultValue == "string") {
|
|
261
|
+
return literalString(defaultValue);
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
case ScalarType.BYTES: {
|
|
266
|
+
if (defaultValue instanceof Uint8Array) {
|
|
267
|
+
return defaultValue;
|
|
268
|
+
}
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
default:
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
export function getFieldIntrinsicDefaultValue(field) {
|
|
280
|
+
if (field.repeated) {
|
|
281
|
+
return {
|
|
282
|
+
defaultValue: "[]",
|
|
283
|
+
typingInferrable: false,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
if (field.kind == "map_field") {
|
|
287
|
+
return {
|
|
288
|
+
defaultValue: "{}",
|
|
289
|
+
typingInferrable: false,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
let defaultValue = undefined;
|
|
293
|
+
let typingInferrable = false;
|
|
294
|
+
if (field.parent.file.syntax == "proto3") {
|
|
295
|
+
switch (field.kind) {
|
|
296
|
+
case "enum_field": {
|
|
297
|
+
if (!field.optional) {
|
|
298
|
+
const zeroValue = field.enum.values.find((v) => v.number === 0);
|
|
299
|
+
if (zeroValue === undefined) {
|
|
300
|
+
throw new Error("invalid proto3 enum: missing 0 value");
|
|
301
|
+
}
|
|
302
|
+
defaultValue = [field.enum, ".", localName(zeroValue)];
|
|
303
|
+
typingInferrable = true;
|
|
304
|
+
}
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
case "scalar_field":
|
|
308
|
+
if (!field.optional) {
|
|
309
|
+
typingInferrable = true;
|
|
310
|
+
if (field.scalar === ScalarType.STRING) {
|
|
311
|
+
defaultValue = literalString("");
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
|
|
315
|
+
defaultValue = scalarDefaultValue(field.scalar);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
default:
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
defaultValue,
|
|
325
|
+
typingInferrable,
|
|
326
|
+
};
|
|
327
|
+
}
|