@bufbuild/protoplugin 1.9.0 → 2.0.0-alpha.1
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/create-es-plugin.d.ts +7 -11
- package/dist/cjs/create-es-plugin.js +15 -13
- package/dist/cjs/ecmascript/file-preamble.js +8 -7
- package/dist/cjs/ecmascript/generated-file.d.ts +19 -20
- package/dist/cjs/ecmascript/generated-file.js +95 -108
- package/dist/cjs/ecmascript/import-path.d.ts +0 -6
- package/dist/cjs/ecmascript/import-path.js +1 -22
- package/dist/cjs/ecmascript/import-symbol.d.ts +1 -3
- package/dist/cjs/ecmascript/import-symbol.js +3 -5
- package/dist/cjs/ecmascript/index.d.ts +3 -16
- package/dist/cjs/ecmascript/index.js +3 -29
- package/dist/cjs/ecmascript/jsdoc.d.ts +2 -13
- package/dist/cjs/ecmascript/jsdoc.js +23 -31
- package/dist/cjs/ecmascript/names.d.ts +4 -0
- package/dist/cjs/ecmascript/names.js +119 -0
- package/dist/cjs/ecmascript/parameter.d.ts +1 -1
- package/dist/cjs/ecmascript/parameter.js +2 -2
- package/dist/cjs/ecmascript/printable.d.ts +36 -0
- package/dist/cjs/ecmascript/runtime-imports.d.ts +12 -23
- package/dist/cjs/ecmascript/runtime-imports.js +28 -29
- package/dist/cjs/ecmascript/safe-identifier.d.ts +6 -0
- package/dist/cjs/ecmascript/safe-identifier.js +90 -0
- package/dist/cjs/ecmascript/schema.d.ts +9 -8
- package/dist/cjs/ecmascript/schema.js +64 -20
- package/dist/cjs/index.d.ts +1 -5
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/plugin.d.ts +1 -1
- package/dist/cjs/run-node.js +4 -3
- package/dist/cjs/source-code-info.d.ts +18 -0
- package/dist/cjs/source-code-info.js +259 -0
- package/dist/esm/create-es-plugin.d.ts +7 -11
- package/dist/esm/create-es-plugin.js +15 -13
- package/dist/esm/ecmascript/file-preamble.js +5 -4
- package/dist/esm/ecmascript/generated-file.d.ts +19 -20
- package/dist/esm/ecmascript/generated-file.js +97 -110
- package/dist/esm/ecmascript/import-path.d.ts +0 -6
- package/dist/esm/ecmascript/import-path.js +0 -20
- package/dist/esm/ecmascript/import-symbol.d.ts +1 -3
- package/dist/esm/ecmascript/import-symbol.js +3 -5
- package/dist/esm/ecmascript/index.d.ts +3 -16
- package/dist/esm/ecmascript/index.js +1 -18
- package/dist/esm/ecmascript/jsdoc.d.ts +2 -13
- package/dist/esm/ecmascript/jsdoc.js +20 -29
- package/dist/esm/ecmascript/names.d.ts +4 -0
- package/dist/esm/ecmascript/names.js +113 -0
- package/dist/esm/ecmascript/parameter.d.ts +1 -1
- package/dist/esm/ecmascript/parameter.js +2 -2
- package/dist/esm/ecmascript/printable.d.ts +36 -0
- package/dist/esm/ecmascript/{opaque-printables.js → printable.js} +1 -1
- package/dist/esm/ecmascript/runtime-imports.d.ts +12 -23
- package/dist/esm/ecmascript/runtime-imports.js +28 -29
- package/dist/esm/ecmascript/safe-identifier.d.ts +6 -0
- package/dist/esm/ecmascript/safe-identifier.js +86 -0
- package/dist/esm/ecmascript/schema.d.ts +9 -8
- package/dist/esm/ecmascript/schema.js +66 -22
- package/dist/esm/index.d.ts +1 -5
- package/dist/esm/index.js +1 -0
- package/dist/esm/plugin.d.ts +1 -1
- package/dist/esm/run-node.js +4 -3
- package/dist/esm/source-code-info.d.ts +18 -0
- package/dist/esm/source-code-info.js +252 -0
- package/package.json +2 -2
- package/dist/cjs/ecmascript/legacy-custom-options.d.ts +0 -40
- package/dist/cjs/ecmascript/legacy-custom-options.js +0 -121
- package/dist/cjs/ecmascript/legacy-gencommon.d.ts +0 -25
- package/dist/cjs/ecmascript/legacy-gencommon.js +0 -237
- package/dist/cjs/ecmascript/opaque-printables.d.ts +0 -27
- package/dist/cjs/ecmascript/reify-wkt.d.ts +0 -100
- package/dist/cjs/ecmascript/reify-wkt.js +0 -170
- package/dist/esm/ecmascript/legacy-custom-options.d.ts +0 -40
- package/dist/esm/ecmascript/legacy-custom-options.js +0 -115
- package/dist/esm/ecmascript/legacy-gencommon.d.ts +0 -25
- package/dist/esm/ecmascript/legacy-gencommon.js +0 -230
- package/dist/esm/ecmascript/opaque-printables.d.ts +0 -27
- package/dist/esm/ecmascript/reify-wkt.d.ts +0 -100
- package/dist/esm/ecmascript/reify-wkt.js +0 -166
- /package/dist/cjs/ecmascript/{opaque-printables.js → printable.js} +0 -0
|
@@ -11,11 +11,12 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import {
|
|
14
|
+
import { protoInt64 } from "@bufbuild/protobuf";
|
|
15
|
+
import { LongType, ScalarType } from "@bufbuild/protobuf/reflect";
|
|
15
16
|
import { createImportSymbol } from "./import-symbol.js";
|
|
16
17
|
import { makeImportPathRelative } from "./import-path.js";
|
|
17
|
-
import {
|
|
18
|
-
export function createGeneratedFile(name, importPath, jsImportStyle,
|
|
18
|
+
import { createJsDocTextFromDesc, formatJsDocBlock } from "./jsdoc.js";
|
|
19
|
+
export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImport, resolveDescImport, resolveShapeImport, createPreamble, runtime) {
|
|
19
20
|
let preamble;
|
|
20
21
|
const el = [];
|
|
21
22
|
return {
|
|
@@ -36,38 +37,51 @@ export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImpo
|
|
|
36
37
|
? [printableOrFragments, ...rest]
|
|
37
38
|
: rest;
|
|
38
39
|
}
|
|
39
|
-
printableToEl(
|
|
40
|
+
printableToEl({
|
|
41
|
+
el,
|
|
42
|
+
runtime,
|
|
43
|
+
resolveDescImport,
|
|
44
|
+
resolveShapeImport,
|
|
45
|
+
}, printables);
|
|
40
46
|
el.push("\n");
|
|
41
47
|
},
|
|
42
|
-
export(name) {
|
|
43
|
-
return createImportSymbol(name, importPath);
|
|
44
|
-
},
|
|
45
48
|
exportDecl(declaration, name) {
|
|
46
49
|
return {
|
|
47
|
-
kind: "
|
|
48
|
-
declaration,
|
|
50
|
+
kind: "es_export_stmt",
|
|
49
51
|
name,
|
|
52
|
+
declaration,
|
|
50
53
|
};
|
|
51
54
|
},
|
|
52
55
|
string(string) {
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
return {
|
|
57
|
+
kind: "es_string",
|
|
58
|
+
value: string,
|
|
59
|
+
};
|
|
55
60
|
},
|
|
56
61
|
jsDoc(textOrDesc, indentation) {
|
|
57
|
-
return
|
|
62
|
+
return {
|
|
63
|
+
kind: "es_jsdoc",
|
|
64
|
+
text: typeof textOrDesc == "string"
|
|
65
|
+
? textOrDesc
|
|
66
|
+
: createJsDocTextFromDesc(textOrDesc),
|
|
67
|
+
indentation,
|
|
68
|
+
};
|
|
58
69
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
importDesc(desc, typeOnly = false) {
|
|
71
|
+
return resolveDescImport(desc, typeOnly);
|
|
72
|
+
},
|
|
73
|
+
importShape(desc) {
|
|
74
|
+
return resolveShapeImport(desc);
|
|
75
|
+
},
|
|
76
|
+
import(name, from) {
|
|
77
|
+
return createImportSymbol(name, from);
|
|
65
78
|
},
|
|
66
79
|
jsImportStyle,
|
|
80
|
+
runtime,
|
|
67
81
|
getFileInfo() {
|
|
68
82
|
return {
|
|
69
83
|
name,
|
|
70
|
-
content: elToContent(el, importPath,
|
|
84
|
+
content: elToContent(el, importPath, rewriteImport, jsImportStyle == "legacy_commonjs"),
|
|
71
85
|
preamble,
|
|
72
86
|
};
|
|
73
87
|
},
|
|
@@ -142,10 +156,11 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
|
|
|
142
156
|
}
|
|
143
157
|
return c.join("");
|
|
144
158
|
}
|
|
145
|
-
function printableToEl(
|
|
159
|
+
function printableToEl(opt, printables) {
|
|
160
|
+
const { el } = opt;
|
|
146
161
|
for (const p of printables) {
|
|
147
162
|
if (Array.isArray(p)) {
|
|
148
|
-
printableToEl(
|
|
163
|
+
printableToEl(opt, p);
|
|
149
164
|
}
|
|
150
165
|
else {
|
|
151
166
|
switch (typeof p) {
|
|
@@ -153,52 +168,88 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
|
|
|
153
168
|
el.push(p);
|
|
154
169
|
break;
|
|
155
170
|
case "number":
|
|
156
|
-
|
|
171
|
+
if (Number.isNaN(p)) {
|
|
172
|
+
el.push("globalThis.NaN");
|
|
173
|
+
}
|
|
174
|
+
else if (p === Number.POSITIVE_INFINITY) {
|
|
175
|
+
el.push("globalThis.Infinity");
|
|
176
|
+
}
|
|
177
|
+
else if (p === Number.NEGATIVE_INFINITY) {
|
|
178
|
+
el.push("-globalThis.Infinity");
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
el.push(p.toString(10));
|
|
182
|
+
}
|
|
157
183
|
break;
|
|
158
184
|
case "boolean":
|
|
159
185
|
el.push(p.toString());
|
|
160
186
|
break;
|
|
161
187
|
case "bigint":
|
|
162
|
-
|
|
188
|
+
if (p == protoInt64.zero) {
|
|
189
|
+
// Loose comparison will match between 0n and 0.
|
|
190
|
+
el.push(opt.runtime.protoInt64, ".zero");
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
el.push(opt.runtime.protoInt64, p > 0 ? ".uParse(" : ".parse(", escapeString(p.toString()), ")");
|
|
194
|
+
}
|
|
163
195
|
break;
|
|
164
196
|
case "object":
|
|
165
197
|
if (p instanceof Uint8Array) {
|
|
166
|
-
|
|
198
|
+
if (p.length === 0) {
|
|
199
|
+
el.push("new Uint8Array(0)");
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
el.push("new Uint8Array([");
|
|
203
|
+
const strings = [];
|
|
204
|
+
for (const n of p) {
|
|
205
|
+
strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
|
|
206
|
+
}
|
|
207
|
+
el.push(strings.join(", "));
|
|
208
|
+
el.push("])");
|
|
209
|
+
}
|
|
167
210
|
break;
|
|
168
211
|
}
|
|
169
212
|
switch (p.kind) {
|
|
170
213
|
case "es_symbol":
|
|
214
|
+
case "es_export_stmt":
|
|
171
215
|
el.push(p);
|
|
172
216
|
break;
|
|
217
|
+
case "es_desc_ref":
|
|
218
|
+
el.push(opt.resolveDescImport(p.desc, p.typeOnly));
|
|
219
|
+
break;
|
|
220
|
+
case "es_shape_ref":
|
|
221
|
+
el.push(opt.resolveShapeImport(p.desc));
|
|
222
|
+
break;
|
|
173
223
|
case "es_jsdoc":
|
|
174
|
-
el.push(p.
|
|
224
|
+
el.push(formatJsDocBlock(p.text, p.indentation));
|
|
175
225
|
break;
|
|
176
226
|
case "es_string":
|
|
177
227
|
el.push(escapeString(p.value));
|
|
178
228
|
break;
|
|
179
229
|
case "es_proto_int64":
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
el.push(createTypeImport(p));
|
|
230
|
+
switch (p.longType) {
|
|
231
|
+
case LongType.STRING:
|
|
232
|
+
el.push(escapeString(p.value.toString()));
|
|
233
|
+
break;
|
|
234
|
+
case LongType.BIGINT:
|
|
235
|
+
if (p.value == protoInt64.zero) {
|
|
236
|
+
// Loose comparison will match between 0n and 0.
|
|
237
|
+
el.push(opt.runtime.protoInt64, ".zero");
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
switch (p.type) {
|
|
241
|
+
case ScalarType.UINT64:
|
|
242
|
+
case ScalarType.FIXED64:
|
|
243
|
+
el.push(opt.runtime.protoInt64, ".uParse(", escapeString(p.value.toString()), ")");
|
|
244
|
+
break;
|
|
245
|
+
default:
|
|
246
|
+
el.push(opt.runtime.protoInt64, ".parse(", escapeString(p.value.toString()), ")");
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
201
250
|
break;
|
|
251
|
+
default:
|
|
252
|
+
throw `cannot print ${typeof p}`;
|
|
202
253
|
}
|
|
203
254
|
break;
|
|
204
255
|
default:
|
|
@@ -323,70 +374,6 @@ function processImports(el, importerPath, rewriteImportPath, makeImportStatement
|
|
|
323
374
|
}
|
|
324
375
|
return symbolToIdentifier;
|
|
325
376
|
}
|
|
326
|
-
function elBigint(el, value, runtimeImports) {
|
|
327
|
-
if (value == protoInt64.zero) {
|
|
328
|
-
// Loose comparison will match between 0n and 0.
|
|
329
|
-
el.push(runtimeImports.protoInt64, ".zero");
|
|
330
|
-
}
|
|
331
|
-
else {
|
|
332
|
-
el.push(runtimeImports.protoInt64, value > 0 ? ".uParse(" : ".parse(", escapeString(value.toString()), ")");
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
function elNumber(el, value, runtimeImports) {
|
|
336
|
-
if (Number.isNaN(value)) {
|
|
337
|
-
el.push(runtimeImports.protoDouble, ".NaN");
|
|
338
|
-
}
|
|
339
|
-
else if (value === Number.POSITIVE_INFINITY) {
|
|
340
|
-
el.push(runtimeImports.protoDouble, ".POSITIVE_INFINITY");
|
|
341
|
-
}
|
|
342
|
-
else if (value === Number.NEGATIVE_INFINITY) {
|
|
343
|
-
el.push(runtimeImports.protoDouble, ".NEGATIVE_INFINITY");
|
|
344
|
-
}
|
|
345
|
-
else {
|
|
346
|
-
el.push(value.toString(10));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
function elUint8Array(el, value) {
|
|
350
|
-
if (value.length === 0) {
|
|
351
|
-
el.push("new Uint8Array(0)");
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
el.push("new Uint8Array([");
|
|
355
|
-
const strings = [];
|
|
356
|
-
for (const n of value) {
|
|
357
|
-
strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
|
|
358
|
-
}
|
|
359
|
-
el.push(strings.join(", "));
|
|
360
|
-
el.push("])");
|
|
361
|
-
}
|
|
362
|
-
function elProtoInt64(el, literal, runtimeImports) {
|
|
363
|
-
switch (literal.longType) {
|
|
364
|
-
case LongType.STRING:
|
|
365
|
-
el.push(escapeString(literal.value.toString()));
|
|
366
|
-
break;
|
|
367
|
-
case LongType.BIGINT:
|
|
368
|
-
if (literal.value == protoInt64.zero) {
|
|
369
|
-
// Loose comparison will match between 0n and 0.
|
|
370
|
-
el.push(runtimeImports.protoInt64, ".zero");
|
|
371
|
-
break;
|
|
372
|
-
}
|
|
373
|
-
switch (literal.type) {
|
|
374
|
-
case ScalarType.UINT64:
|
|
375
|
-
case ScalarType.FIXED64:
|
|
376
|
-
el.push(runtimeImports.protoInt64);
|
|
377
|
-
el.push(".uParse(");
|
|
378
|
-
el.push(escapeString(literal.value.toString()));
|
|
379
|
-
el.push(")");
|
|
380
|
-
break;
|
|
381
|
-
default:
|
|
382
|
-
el.push(runtimeImports.protoInt64);
|
|
383
|
-
el.push(".parse(");
|
|
384
|
-
el.push(escapeString(literal.value.toString()));
|
|
385
|
-
el.push(")");
|
|
386
|
-
break;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
377
|
function escapeString(value) {
|
|
391
378
|
return ('"' +
|
|
392
379
|
value
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { DescFile } from "@bufbuild/protobuf";
|
|
2
1
|
/**
|
|
3
2
|
* A configuration for rewriting import paths, a feature mainly used for
|
|
4
3
|
* remote code generation in the BSR npm registry, which makes it possible
|
|
@@ -44,11 +43,6 @@ export type RewriteImports = {
|
|
|
44
43
|
* to the given import extension.
|
|
45
44
|
*/
|
|
46
45
|
export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
|
|
47
|
-
/**
|
|
48
|
-
* Returns the import path for files generated by the base type generator
|
|
49
|
-
* protoc-gen-es.
|
|
50
|
-
*/
|
|
51
|
-
export declare function makeImportPath(file: DescFile, bootstrapWkt: boolean, filesToGenerate: DescFile[]): string;
|
|
52
46
|
export declare const relativePathRE: RegExp;
|
|
53
47
|
/**
|
|
54
48
|
* Derives an ECMAScript module import path from a file path. For example,
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import { codegenInfo } from "@bufbuild/protobuf";
|
|
15
14
|
const cache = new WeakMap();
|
|
16
15
|
/**
|
|
17
16
|
* Apply import rewrites to the given import path, and change all .js extensions
|
|
@@ -81,25 +80,6 @@ function starToRegExp(star) {
|
|
|
81
80
|
r.push("$");
|
|
82
81
|
return new RegExp(r.join(""));
|
|
83
82
|
}
|
|
84
|
-
/**
|
|
85
|
-
* Returns the import path for files generated by the base type generator
|
|
86
|
-
* protoc-gen-es.
|
|
87
|
-
*/
|
|
88
|
-
export function makeImportPath(file, bootstrapWkt, filesToGenerate) {
|
|
89
|
-
// Well-known types are published with the runtime package. We usually want
|
|
90
|
-
// the generated code to import them from the runtime package, with the
|
|
91
|
-
// following exceptions:
|
|
92
|
-
// 1. We are bootstrapping the runtime package via the plugin option
|
|
93
|
-
// "bootstrap_wkt". In that case, we cannot refer to the runtime package
|
|
94
|
-
// itself.
|
|
95
|
-
// 2. We were explicitly asked to generate the well-known type.
|
|
96
|
-
if (!bootstrapWkt &&
|
|
97
|
-
!filesToGenerate.includes(file) &&
|
|
98
|
-
codegenInfo.wktSourceFiles.includes(file.name + ".proto")) {
|
|
99
|
-
return codegenInfo.packageName;
|
|
100
|
-
}
|
|
101
|
-
return "./" + file.name + "_pb.js";
|
|
102
|
-
}
|
|
103
83
|
export const relativePathRE = /^\.{1,2}\//;
|
|
104
84
|
/**
|
|
105
85
|
* Derives an ECMAScript module import path from a file path. For example,
|
|
@@ -29,11 +29,9 @@ export type ImportSymbol = {
|
|
|
29
29
|
/**
|
|
30
30
|
* The unique ID based on name and from, disregarding typeOnly.
|
|
31
31
|
*/
|
|
32
|
-
readonly id:
|
|
32
|
+
readonly id: `import("${string}").${string}`;
|
|
33
33
|
};
|
|
34
34
|
/**
|
|
35
35
|
* Create a new import symbol.
|
|
36
36
|
*/
|
|
37
37
|
export declare function createImportSymbol(name: string, from: string, typeOnly?: boolean): ImportSymbol;
|
|
38
|
-
type EsSymbolId = string;
|
|
39
|
-
export {};
|
|
@@ -15,16 +15,14 @@
|
|
|
15
15
|
* Create a new import symbol.
|
|
16
16
|
*/
|
|
17
17
|
export function createImportSymbol(name, from, typeOnly) {
|
|
18
|
-
|
|
19
|
-
const s = {
|
|
18
|
+
return {
|
|
20
19
|
kind: "es_symbol",
|
|
21
20
|
name,
|
|
22
21
|
from,
|
|
23
|
-
typeOnly: false,
|
|
24
|
-
id
|
|
22
|
+
typeOnly: typeOnly !== null && typeOnly !== void 0 ? typeOnly : false,
|
|
23
|
+
id: `import("${from}").${name}`,
|
|
25
24
|
toTypeOnly() {
|
|
26
25
|
return Object.assign(Object.assign({}, this), { typeOnly: true });
|
|
27
26
|
},
|
|
28
27
|
};
|
|
29
|
-
return typeOnly === true ? s.toTypeOnly() : s;
|
|
30
28
|
}
|
|
@@ -1,20 +1,7 @@
|
|
|
1
|
-
import type { AnyDesc, DescExtension, DescFile } from "@bufbuild/protobuf";
|
|
2
|
-
import type { Printable } from "./generated-file.js";
|
|
3
|
-
export { reifyWkt } from "./reify-wkt.js";
|
|
4
1
|
export type { Target } from "./target.js";
|
|
5
2
|
export type { Schema } from "./schema.js";
|
|
6
|
-
export type {
|
|
7
|
-
export type { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
|
|
3
|
+
export type { GeneratedFile, FileInfo } from "./generated-file.js";
|
|
8
4
|
export type { ImportSymbol } from "./import-symbol.js";
|
|
9
5
|
export { createImportSymbol } from "./import-symbol.js";
|
|
10
|
-
export
|
|
11
|
-
export {
|
|
12
|
-
export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./legacy-custom-options.js";
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
15
|
-
*/
|
|
16
|
-
export declare function makeJsDoc(desc: Exclude<AnyDesc, DescFile | DescExtension>, indentation?: string): Printable;
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
19
|
-
*/
|
|
20
|
-
export declare function createJsDocBlock(text: string, indentation?: string): Printable;
|
|
6
|
+
export type { Printable } from "./printable.js";
|
|
7
|
+
export { safeIdentifier } from "./safe-identifier.js";
|
|
@@ -11,22 +11,5 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import { codegenInfo } from "@bufbuild/protobuf";
|
|
15
|
-
import { createJsDocBlock as createJsDocBlockInternal } from "./jsdoc.js";
|
|
16
|
-
export { reifyWkt } from "./reify-wkt.js";
|
|
17
14
|
export { createImportSymbol } from "./import-symbol.js";
|
|
18
|
-
export
|
|
19
|
-
export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, literalString, } from "./legacy-gencommon.js";
|
|
20
|
-
export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./legacy-custom-options.js";
|
|
21
|
-
/**
|
|
22
|
-
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
23
|
-
*/
|
|
24
|
-
export function makeJsDoc(desc, indentation = "") {
|
|
25
|
-
return createJsDocBlockInternal(desc, indentation).toString();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated Please use GeneratedFile.jsDoc() instead
|
|
29
|
-
*/
|
|
30
|
-
export function createJsDocBlock(text, indentation = "") {
|
|
31
|
-
return createJsDocBlockInternal(text, indentation).toString();
|
|
32
|
-
}
|
|
15
|
+
export { safeIdentifier } from "./safe-identifier.js";
|
|
@@ -1,14 +1,3 @@
|
|
|
1
1
|
import type { AnyDesc, DescFile } from "@bufbuild/protobuf";
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @deprecated In a future release, we will make this property optional.
|
|
6
|
-
*/
|
|
7
|
-
text: string;
|
|
8
|
-
indentation?: string;
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated In a future release, we will remove this method.
|
|
11
|
-
*/
|
|
12
|
-
toString(): string;
|
|
13
|
-
};
|
|
14
|
-
export declare function createJsDocBlock(textOrDesc: string | Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
|
|
2
|
+
export declare function createJsDocTextFromDesc(desc: Exclude<AnyDesc, DescFile>): string;
|
|
3
|
+
export declare function formatJsDocBlock(text: string, indentation: string | undefined): string;
|
|
@@ -11,31 +11,9 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
export function
|
|
16
|
-
const
|
|
17
|
-
return {
|
|
18
|
-
kind: "es_jsdoc",
|
|
19
|
-
text,
|
|
20
|
-
indentation,
|
|
21
|
-
toString() {
|
|
22
|
-
if (text.trim().length == 0) {
|
|
23
|
-
return "";
|
|
24
|
-
}
|
|
25
|
-
let lines = text.split("\n");
|
|
26
|
-
if (lines.length === 0) {
|
|
27
|
-
return "";
|
|
28
|
-
}
|
|
29
|
-
lines = lines.map((l) => l.split("*/").join("*\\/"));
|
|
30
|
-
lines = lines.map((l) => (l.length > 0 ? " " + l : l));
|
|
31
|
-
const i = indentation !== null && indentation !== void 0 ? indentation : "";
|
|
32
|
-
return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
function createTextForDesc(desc) {
|
|
37
|
-
var _a, _b;
|
|
38
|
-
const comments = desc.getComments();
|
|
14
|
+
import { getComments, getDeclarationString } from "../source-code-info.js";
|
|
15
|
+
export function createJsDocTextFromDesc(desc) {
|
|
16
|
+
const comments = getComments(desc);
|
|
39
17
|
let text = "";
|
|
40
18
|
if (comments.leading !== undefined) {
|
|
41
19
|
text += comments.leading;
|
|
@@ -61,13 +39,13 @@ function createTextForDesc(desc) {
|
|
|
61
39
|
.join("\n");
|
|
62
40
|
switch (desc.kind) {
|
|
63
41
|
case "enum_value":
|
|
64
|
-
text += `@generated from enum value: ${desc
|
|
42
|
+
text += `@generated from enum value: ${getDeclarationString(desc)};`;
|
|
65
43
|
break;
|
|
66
44
|
case "field":
|
|
67
|
-
text += `@generated from field: ${desc
|
|
45
|
+
text += `@generated from field: ${getDeclarationString(desc)};`;
|
|
68
46
|
break;
|
|
69
47
|
case "extension":
|
|
70
|
-
text += `@generated from extension: ${desc
|
|
48
|
+
text += `@generated from extension: ${getDeclarationString(desc)};`;
|
|
71
49
|
break;
|
|
72
50
|
default:
|
|
73
51
|
text += `@generated from ${desc.toString()}`;
|
|
@@ -78,7 +56,7 @@ function createTextForDesc(desc) {
|
|
|
78
56
|
case "enum":
|
|
79
57
|
case "message":
|
|
80
58
|
case "service":
|
|
81
|
-
deprecated = deprecated ||
|
|
59
|
+
deprecated = deprecated || desc.file.deprecated;
|
|
82
60
|
break;
|
|
83
61
|
default:
|
|
84
62
|
break;
|
|
@@ -88,3 +66,16 @@ function createTextForDesc(desc) {
|
|
|
88
66
|
}
|
|
89
67
|
return text;
|
|
90
68
|
}
|
|
69
|
+
export function formatJsDocBlock(text, indentation) {
|
|
70
|
+
if (text.trim().length == 0) {
|
|
71
|
+
return "";
|
|
72
|
+
}
|
|
73
|
+
let lines = text.split("\n");
|
|
74
|
+
if (lines.length === 0) {
|
|
75
|
+
return "";
|
|
76
|
+
}
|
|
77
|
+
lines = lines.map((l) => l.split("*/").join("*\\/"));
|
|
78
|
+
lines = lines.map((l) => (l.length > 0 ? " " + l : l));
|
|
79
|
+
const i = indentation !== null && indentation !== void 0 ? indentation : "";
|
|
80
|
+
return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
|
|
81
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
|
|
2
|
+
export declare function generateFilePath(file: DescFile, bootstrapWkt: boolean, filesToGenerate: DescFile[]): string;
|
|
3
|
+
export declare function localDescName(desc: DescFile | DescEnum | DescMessage | DescExtension | DescService): string;
|
|
4
|
+
export declare function localShapeName(desc: DescEnum | DescMessage): string;
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
import { wktPublicImportPaths } from "@bufbuild/protobuf/codegenv1";
|
|
15
|
+
import { nestedTypes } from "@bufbuild/protobuf/reflect";
|
|
16
|
+
import { safeIdentifier } from "./safe-identifier.js";
|
|
17
|
+
export function generateFilePath(file, bootstrapWkt, filesToGenerate) {
|
|
18
|
+
// Well-known types are published with the runtime package. We usually want
|
|
19
|
+
// the generated code to import them from the runtime package, with the
|
|
20
|
+
// following exceptions:
|
|
21
|
+
// 1. We are bootstrapping the runtime package via the plugin option
|
|
22
|
+
// "bootstrap_wkt". In that case, we cannot refer to the runtime package
|
|
23
|
+
// itself.
|
|
24
|
+
// 2. We were explicitly asked to generate the well-known type.
|
|
25
|
+
const wktFrom = wktPublicImportPaths[file.proto.name];
|
|
26
|
+
if (wktFrom !== undefined &&
|
|
27
|
+
!bootstrapWkt &&
|
|
28
|
+
!filesToGenerate.find((f) => f.name === file.name)) {
|
|
29
|
+
return wktFrom;
|
|
30
|
+
}
|
|
31
|
+
return "./" + file.name + "_pb.js";
|
|
32
|
+
}
|
|
33
|
+
export function localDescName(desc) {
|
|
34
|
+
const file = desc.kind == "file" ? desc : desc.file;
|
|
35
|
+
const { descNames } = allNames(file);
|
|
36
|
+
const name = descNames.get(desc);
|
|
37
|
+
if (name === undefined) {
|
|
38
|
+
throw new Error(`unable to determine unique identifier for ${desc.toString()}`);
|
|
39
|
+
}
|
|
40
|
+
return name;
|
|
41
|
+
}
|
|
42
|
+
export function localShapeName(desc) {
|
|
43
|
+
const { shapeNames } = allNames(desc.file);
|
|
44
|
+
const name = shapeNames.get(desc);
|
|
45
|
+
if (name === undefined) {
|
|
46
|
+
throw new Error(`unable to determine unique identifier for ${desc.toString()}`);
|
|
47
|
+
}
|
|
48
|
+
return name;
|
|
49
|
+
}
|
|
50
|
+
function idealDescName(desc, i) {
|
|
51
|
+
const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
|
|
52
|
+
switch (desc.kind) {
|
|
53
|
+
case "file":
|
|
54
|
+
return (safeIdentifier("fileDesc_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_")) + escape);
|
|
55
|
+
case "enum":
|
|
56
|
+
return baseName(desc) + "Desc" + escape;
|
|
57
|
+
case "message":
|
|
58
|
+
return baseName(desc) + "Desc" + escape;
|
|
59
|
+
case "extension":
|
|
60
|
+
return baseName(desc) + escape;
|
|
61
|
+
case "service":
|
|
62
|
+
return baseName(desc) + escape;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function idealShapeName(desc, i) {
|
|
66
|
+
const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
|
|
67
|
+
return baseName(desc) + escape;
|
|
68
|
+
}
|
|
69
|
+
function baseName(desc) {
|
|
70
|
+
const pkg = desc.file.proto.package;
|
|
71
|
+
const offset = pkg.length > 0 ? pkg.length + 1 : 0;
|
|
72
|
+
const name = desc.typeName.substring(offset).replace(/\./g, "_");
|
|
73
|
+
return safeIdentifier(name);
|
|
74
|
+
}
|
|
75
|
+
function allNames(file) {
|
|
76
|
+
const taken = new Set();
|
|
77
|
+
const shapeNames = new Map();
|
|
78
|
+
const descNames = new Map();
|
|
79
|
+
for (const desc of [file, ...nestedTypes(file)]) {
|
|
80
|
+
switch (desc.kind) {
|
|
81
|
+
case "enum":
|
|
82
|
+
case "message": {
|
|
83
|
+
let descName;
|
|
84
|
+
let shapeName;
|
|
85
|
+
for (let i = 0;; i++) {
|
|
86
|
+
descName = idealDescName(desc, i);
|
|
87
|
+
shapeName = idealShapeName(desc, i);
|
|
88
|
+
if (!taken.has(descName) && !taken.has(shapeName)) {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
taken.add(descName);
|
|
93
|
+
taken.add(shapeName);
|
|
94
|
+
descNames.set(desc, descName);
|
|
95
|
+
shapeNames.set(desc, shapeName);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
default: {
|
|
99
|
+
let descName;
|
|
100
|
+
for (let i = 0;; i++) {
|
|
101
|
+
descName = idealDescName(desc, i);
|
|
102
|
+
if (!taken.has(descName)) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
taken.add(descName);
|
|
107
|
+
descNames.set(desc, descName);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { shapeNames, descNames };
|
|
113
|
+
}
|
|
@@ -10,4 +10,4 @@ export interface ParsedParameter {
|
|
|
10
10
|
jsImportStyle: "module" | "legacy_commonjs";
|
|
11
11
|
sanitizedParameter: string;
|
|
12
12
|
}
|
|
13
|
-
export declare function parseParameter(parameter: string
|
|
13
|
+
export declare function parseParameter(parameter: string, parseExtraOption: ((key: string, value: string) => void) | undefined): ParsedParameter;
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
import { PluginOptionError } from "../error.js";
|
|
15
15
|
export function parseParameter(parameter, parseExtraOption) {
|
|
16
16
|
let targets = ["js", "dts"];
|
|
17
|
-
let tsNocheck =
|
|
17
|
+
let tsNocheck = false;
|
|
18
18
|
let bootstrapWkt = false;
|
|
19
19
|
let keepEmptyFiles = false;
|
|
20
20
|
const rewriteImports = [];
|
|
@@ -143,7 +143,7 @@ export function parseParameter(parameter, parseExtraOption) {
|
|
|
143
143
|
};
|
|
144
144
|
}
|
|
145
145
|
function splitParameter(parameter) {
|
|
146
|
-
if (parameter ==
|
|
146
|
+
if (parameter.length == 0) {
|
|
147
147
|
return [];
|
|
148
148
|
}
|
|
149
149
|
return parameter.split(",").map((raw) => {
|