@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
|
@@ -15,37 +15,35 @@
|
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.createSchema = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
+
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
19
|
+
const reflect_1 = require("@bufbuild/protobuf/reflect");
|
|
18
20
|
const generated_file_js_1 = require("./generated-file.js");
|
|
19
|
-
const runtime_imports_js_1 = require("./runtime-imports.js");
|
|
20
21
|
const import_symbol_js_1 = require("./import-symbol.js");
|
|
21
22
|
const import_path_js_1 = require("./import-path.js");
|
|
22
23
|
const file_preamble_js_1 = require("./file-preamble.js");
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
27
|
-
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
|
|
28
|
-
const runtime = (0, runtime_imports_js_1.createRuntimeImports)(parameter.bootstrapWkt);
|
|
29
|
-
const createTypeImport = (desc) => {
|
|
30
|
-
const name = protobuf_1.codegenInfo.localName(desc);
|
|
31
|
-
const from = (0, import_path_js_1.makeImportPath)(desc.file, parameter.bootstrapWkt, filesToGenerate);
|
|
32
|
-
return (0, import_symbol_js_1.createImportSymbol)(name, from);
|
|
33
|
-
};
|
|
34
|
-
const createPreamble = (descFile) => (0, file_preamble_js_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
|
|
24
|
+
const names_js_1 = require("./names.js");
|
|
25
|
+
const runtime_imports_js_1 = require("./runtime-imports.js");
|
|
26
|
+
function createSchema(request, parameter, pluginName, pluginVersion, minimumEdition, maximumEdition) {
|
|
27
|
+
const { allFiles, filesToGenerate } = getFilesToGenerate(request, minimumEdition, maximumEdition);
|
|
35
28
|
let target;
|
|
36
29
|
const generatedFiles = [];
|
|
30
|
+
const runtime = (0, runtime_imports_js_1.createRuntimeImports)(parameter.bootstrapWkt);
|
|
31
|
+
const resolveDescImport = (desc, typeOnly) => (0, import_symbol_js_1.createImportSymbol)((0, names_js_1.localDescName)(desc), (0, names_js_1.generateFilePath)(desc.kind == "file" ? desc : desc.file, parameter.bootstrapWkt, filesToGenerate), typeOnly);
|
|
32
|
+
const resolveShapeImport = (desc) => (0, import_symbol_js_1.createImportSymbol)((0, names_js_1.localShapeName)(desc), (0, names_js_1.generateFilePath)(desc.file, parameter.bootstrapWkt, filesToGenerate), true);
|
|
33
|
+
const createPreamble = (descFile) => (0, file_preamble_js_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
|
|
34
|
+
const rewriteImport = (importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, parameter.rewriteImports, parameter.importExtension);
|
|
37
35
|
return {
|
|
38
36
|
targets: parameter.targets,
|
|
39
|
-
runtime,
|
|
40
37
|
proto: request,
|
|
41
38
|
files: filesToGenerate,
|
|
42
|
-
allFiles:
|
|
39
|
+
allFiles: allFiles,
|
|
40
|
+
typesInFile: reflect_1.nestedTypes,
|
|
43
41
|
generateFile(name) {
|
|
44
42
|
if (target === undefined) {
|
|
45
43
|
throw new Error("prepareGenerate() must be called before generateFile()");
|
|
46
44
|
}
|
|
47
45
|
const genFile = (0, generated_file_js_1.createGeneratedFile)(name, (0, import_path_js_1.deriveImportPath)(name), target === "js" ? parameter.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
|
|
48
|
-
|
|
46
|
+
rewriteImport, resolveDescImport, resolveShapeImport, createPreamble, runtime);
|
|
49
47
|
generatedFiles.push(genFile);
|
|
50
48
|
return genFile;
|
|
51
49
|
},
|
|
@@ -60,10 +58,56 @@ function createSchema(request, parameter, pluginName, pluginVersion, featureSetD
|
|
|
60
58
|
};
|
|
61
59
|
}
|
|
62
60
|
exports.createSchema = createSchema;
|
|
63
|
-
function
|
|
64
|
-
|
|
61
|
+
function getFilesToGenerate(request, minimumEdition, maximumEdition) {
|
|
62
|
+
if (minimumEdition > maximumEdition) {
|
|
63
|
+
throw new Error(`configured minimumEdition ${editionToString(minimumEdition)} > maximumEdition ${editionToString(maximumEdition)} - please contact plugin author`);
|
|
64
|
+
}
|
|
65
|
+
const missing = request.fileToGenerate.filter((fileToGenerate) => !request.protoFile.find((f) => f.name === fileToGenerate));
|
|
65
66
|
if (missing.length) {
|
|
66
|
-
throw `files_to_generate missing in the request: ${missing.join(", ")}
|
|
67
|
+
throw new Error(`files_to_generate missing in the request: ${missing.join(", ")}`);
|
|
68
|
+
}
|
|
69
|
+
for (const file of request.protoFile) {
|
|
70
|
+
if (request.fileToGenerate.includes(file.name)) {
|
|
71
|
+
let edition;
|
|
72
|
+
switch (file.syntax) {
|
|
73
|
+
case "":
|
|
74
|
+
case "proto2":
|
|
75
|
+
edition = wkt_1.Edition.EDITION_PROTO2;
|
|
76
|
+
break;
|
|
77
|
+
case "proto3":
|
|
78
|
+
edition = wkt_1.Edition.EDITION_PROTO3;
|
|
79
|
+
break;
|
|
80
|
+
case "editions":
|
|
81
|
+
edition = file.edition;
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
edition = wkt_1.Edition.EDITION_UNKNOWN;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
if (edition < minimumEdition) {
|
|
88
|
+
throw new Error(`${file.name}: unsupported edition ${editionToString(edition)} - the earliest supported edition is ${editionToString(minimumEdition)}`);
|
|
89
|
+
}
|
|
90
|
+
if (edition > maximumEdition) {
|
|
91
|
+
throw new Error(`${file.name}: unsupported edition ${editionToString(edition)} - the latest supported edition is ${editionToString(maximumEdition)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const registry = (0, reflect_1.createFileRegistry)((0, protobuf_1.create)(wkt_1.FileDescriptorSetDesc, {
|
|
96
|
+
file: request.protoFile,
|
|
97
|
+
}));
|
|
98
|
+
const allFiles = [];
|
|
99
|
+
const filesToGenerate = [];
|
|
100
|
+
for (const file of registry.files) {
|
|
101
|
+
allFiles.push(file);
|
|
102
|
+
if (request.fileToGenerate.includes(file.proto.name)) {
|
|
103
|
+
filesToGenerate.push(file);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { allFiles, filesToGenerate };
|
|
107
|
+
}
|
|
108
|
+
function editionToString(edition) {
|
|
109
|
+
if (edition in wkt_1.Edition) {
|
|
110
|
+
return wkt_1.Edition[edition].replace(/^EDITION_/, "");
|
|
67
111
|
}
|
|
68
|
-
return
|
|
112
|
+
return `unknown (${edition})`;
|
|
69
113
|
}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import type { Schema as SchemaInternal } from "./ecmascript/schema.js";
|
|
2
1
|
export type { Plugin } from "./plugin.js";
|
|
3
2
|
export { runNodeJs } from "./run-node.js";
|
|
4
3
|
export { createEcmaScriptPlugin } from "./create-es-plugin.js";
|
|
5
|
-
|
|
6
|
-
* @deprecated Please use Schema from @bufbuild/protoplugin/ecmascript instead
|
|
7
|
-
*/
|
|
8
|
-
export type Schema = SchemaInternal;
|
|
4
|
+
export { getComments, getDeclarationString, getPackageComments, getSyntaxComments, } from "./source-code-info.js";
|
package/dist/cjs/index.js
CHANGED
|
@@ -13,8 +13,13 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.createEcmaScriptPlugin = exports.runNodeJs = void 0;
|
|
16
|
+
exports.getSyntaxComments = exports.getPackageComments = exports.getDeclarationString = exports.getComments = exports.createEcmaScriptPlugin = exports.runNodeJs = void 0;
|
|
17
17
|
var run_node_js_1 = require("./run-node.js");
|
|
18
18
|
Object.defineProperty(exports, "runNodeJs", { enumerable: true, get: function () { return run_node_js_1.runNodeJs; } });
|
|
19
19
|
var create_es_plugin_js_1 = require("./create-es-plugin.js");
|
|
20
20
|
Object.defineProperty(exports, "createEcmaScriptPlugin", { enumerable: true, get: function () { return create_es_plugin_js_1.createEcmaScriptPlugin; } });
|
|
21
|
+
var source_code_info_js_1 = require("./source-code-info.js");
|
|
22
|
+
Object.defineProperty(exports, "getComments", { enumerable: true, get: function () { return source_code_info_js_1.getComments; } });
|
|
23
|
+
Object.defineProperty(exports, "getDeclarationString", { enumerable: true, get: function () { return source_code_info_js_1.getDeclarationString; } });
|
|
24
|
+
Object.defineProperty(exports, "getPackageComments", { enumerable: true, get: function () { return source_code_info_js_1.getPackageComments; } });
|
|
25
|
+
Object.defineProperty(exports, "getSyntaxComments", { enumerable: true, get: function () { return source_code_info_js_1.getSyntaxComments; } });
|
package/dist/cjs/plugin.d.ts
CHANGED
package/dist/cjs/run-node.js
CHANGED
|
@@ -14,8 +14,9 @@
|
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.runNodeJs = void 0;
|
|
17
|
-
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
17
|
const error_js_1 = require("./error.js");
|
|
18
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
19
|
+
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
19
20
|
/**
|
|
20
21
|
* Run a plugin with Node.js.
|
|
21
22
|
*
|
|
@@ -40,9 +41,9 @@ function runNodeJs(plugin) {
|
|
|
40
41
|
}
|
|
41
42
|
readBytes(process.stdin)
|
|
42
43
|
.then((data) => {
|
|
43
|
-
const req = protobuf_1.
|
|
44
|
+
const req = (0, protobuf_1.fromBinary)(wkt_1.CodeGeneratorRequestDesc, data);
|
|
44
45
|
const res = plugin.run(req);
|
|
45
|
-
return writeBytes(process.stdout,
|
|
46
|
+
return writeBytes(process.stdout, (0, protobuf_1.toBinary)(wkt_1.CodeGeneratorResponseDesc, res));
|
|
46
47
|
})
|
|
47
48
|
.then(() => process.exit(0))
|
|
48
49
|
.catch((reason) => {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DescField, DescEnumValue, DescExtension, DescComments, AnyDesc, DescFile } from "@bufbuild/protobuf";
|
|
2
|
+
/**
|
|
3
|
+
* Get comments on the package element in the protobuf source.
|
|
4
|
+
*/
|
|
5
|
+
export declare function getPackageComments(desc: DescFile): DescComments;
|
|
6
|
+
/**
|
|
7
|
+
* Get comments on the syntax element in the protobuf source.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getSyntaxComments(desc: DescFile): DescComments;
|
|
10
|
+
/**
|
|
11
|
+
* Get comments on the element in the protobuf source.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getComments(desc: Exclude<AnyDesc, DescFile>): DescComments;
|
|
14
|
+
/**
|
|
15
|
+
* Return a string that matches the definition of a field in the protobuf
|
|
16
|
+
* source. Does not take custom options into account.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getDeclarationString(desc: DescField | DescExtension | DescEnumValue): string;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021-2024 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.getDeclarationString = exports.getComments = exports.getSyntaxComments = exports.getPackageComments = void 0;
|
|
17
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
+
const reflect_1 = require("@bufbuild/protobuf/reflect");
|
|
19
|
+
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
20
|
+
/**
|
|
21
|
+
* Get comments on the package element in the protobuf source.
|
|
22
|
+
*/
|
|
23
|
+
function getPackageComments(desc) {
|
|
24
|
+
return findComments(desc.proto.sourceCodeInfo, [
|
|
25
|
+
FieldNumber.FileDescriptorProto_Package,
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
exports.getPackageComments = getPackageComments;
|
|
29
|
+
/**
|
|
30
|
+
* Get comments on the syntax element in the protobuf source.
|
|
31
|
+
*/
|
|
32
|
+
function getSyntaxComments(desc) {
|
|
33
|
+
return findComments(desc.proto.sourceCodeInfo, [
|
|
34
|
+
FieldNumber.FileDescriptorProto_Syntax,
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
exports.getSyntaxComments = getSyntaxComments;
|
|
38
|
+
/**
|
|
39
|
+
* Get comments on the element in the protobuf source.
|
|
40
|
+
*/
|
|
41
|
+
function getComments(desc) {
|
|
42
|
+
let path = [];
|
|
43
|
+
let file;
|
|
44
|
+
switch (desc.kind) {
|
|
45
|
+
case "enum":
|
|
46
|
+
path = desc.parent
|
|
47
|
+
? [
|
|
48
|
+
...getComments(desc.parent).sourcePath,
|
|
49
|
+
FieldNumber.DescriptorProto_EnumType,
|
|
50
|
+
desc.parent.proto.enumType.indexOf(desc.proto),
|
|
51
|
+
]
|
|
52
|
+
: [
|
|
53
|
+
FieldNumber.FileDescriptorProto_EnumType,
|
|
54
|
+
desc.file.proto.enumType.indexOf(desc.proto),
|
|
55
|
+
];
|
|
56
|
+
file = desc.file;
|
|
57
|
+
break;
|
|
58
|
+
case "oneof":
|
|
59
|
+
path = [
|
|
60
|
+
...getComments(desc.parent).sourcePath,
|
|
61
|
+
FieldNumber.DescriptorProto_OneofDecl,
|
|
62
|
+
desc.parent.proto.oneofDecl.indexOf(desc.proto),
|
|
63
|
+
];
|
|
64
|
+
file = desc.parent.file;
|
|
65
|
+
break;
|
|
66
|
+
case "message":
|
|
67
|
+
path = desc.parent
|
|
68
|
+
? [
|
|
69
|
+
...getComments(desc.parent).sourcePath,
|
|
70
|
+
FieldNumber.DescriptorProto_NestedType,
|
|
71
|
+
desc.parent.proto.nestedType.indexOf(desc.proto),
|
|
72
|
+
]
|
|
73
|
+
: [
|
|
74
|
+
FieldNumber.FileDescriptorProto_MessageType,
|
|
75
|
+
desc.file.proto.messageType.indexOf(desc.proto),
|
|
76
|
+
];
|
|
77
|
+
file = desc.file;
|
|
78
|
+
break;
|
|
79
|
+
case "enum_value":
|
|
80
|
+
path = [
|
|
81
|
+
...getComments(desc.parent).sourcePath,
|
|
82
|
+
FieldNumber.EnumDescriptorProto_Value,
|
|
83
|
+
desc.parent.proto.value.indexOf(desc.proto),
|
|
84
|
+
];
|
|
85
|
+
file = desc.parent.file;
|
|
86
|
+
break;
|
|
87
|
+
case "field":
|
|
88
|
+
path = [
|
|
89
|
+
...getComments(desc.parent).sourcePath,
|
|
90
|
+
FieldNumber.DescriptorProto_Field,
|
|
91
|
+
desc.parent.proto.field.indexOf(desc.proto),
|
|
92
|
+
];
|
|
93
|
+
file = desc.parent.file;
|
|
94
|
+
break;
|
|
95
|
+
case "extension":
|
|
96
|
+
path = desc.parent
|
|
97
|
+
? [
|
|
98
|
+
...getComments(desc.parent).sourcePath,
|
|
99
|
+
FieldNumber.DescriptorProto_Extension,
|
|
100
|
+
desc.parent.proto.extension.indexOf(desc.proto),
|
|
101
|
+
]
|
|
102
|
+
: [
|
|
103
|
+
FieldNumber.FileDescriptorProto_Extension,
|
|
104
|
+
desc.file.proto.extension.indexOf(desc.proto),
|
|
105
|
+
];
|
|
106
|
+
file = desc.file;
|
|
107
|
+
break;
|
|
108
|
+
case "service":
|
|
109
|
+
path = [
|
|
110
|
+
FieldNumber.FileDescriptorProto_Service,
|
|
111
|
+
desc.file.proto.service.indexOf(desc.proto),
|
|
112
|
+
];
|
|
113
|
+
file = desc.file;
|
|
114
|
+
break;
|
|
115
|
+
case "rpc":
|
|
116
|
+
path = [
|
|
117
|
+
...getComments(desc.parent).sourcePath,
|
|
118
|
+
FieldNumber.ServiceDescriptorProto_Method,
|
|
119
|
+
desc.parent.proto.method.indexOf(desc.proto),
|
|
120
|
+
];
|
|
121
|
+
file = desc.parent.file;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
return findComments(file.proto.sourceCodeInfo, path);
|
|
125
|
+
}
|
|
126
|
+
exports.getComments = getComments;
|
|
127
|
+
/**
|
|
128
|
+
* Return a string that matches the definition of a field in the protobuf
|
|
129
|
+
* source. Does not take custom options into account.
|
|
130
|
+
*/
|
|
131
|
+
function getDeclarationString(desc) {
|
|
132
|
+
var _a;
|
|
133
|
+
if (desc.kind === "enum_value") {
|
|
134
|
+
let str = `${desc.name} = ${desc.number}`;
|
|
135
|
+
if (((_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) === true) {
|
|
136
|
+
str += " [deprecated = true]";
|
|
137
|
+
}
|
|
138
|
+
return str;
|
|
139
|
+
}
|
|
140
|
+
const file = desc.kind === "extension" ? desc.file : desc.parent.file;
|
|
141
|
+
const parts = [];
|
|
142
|
+
function typeName(f) {
|
|
143
|
+
if (f.message) {
|
|
144
|
+
return f.message.typeName;
|
|
145
|
+
}
|
|
146
|
+
if (f.enum) {
|
|
147
|
+
return f.enum.typeName;
|
|
148
|
+
}
|
|
149
|
+
return reflect_1.ScalarType[f.scalar].toLowerCase();
|
|
150
|
+
}
|
|
151
|
+
switch (desc.fieldKind) {
|
|
152
|
+
case "scalar":
|
|
153
|
+
case "enum":
|
|
154
|
+
case "message":
|
|
155
|
+
if (file.edition === wkt_1.Edition.EDITION_PROTO2 &&
|
|
156
|
+
(0, protobuf_1.isFieldSet)(wkt_1.FieldDescriptorProtoDesc, desc.proto, "label") &&
|
|
157
|
+
desc.proto.label == wkt_1.FieldDescriptorProto_Label.REQUIRED) {
|
|
158
|
+
parts.push("required");
|
|
159
|
+
}
|
|
160
|
+
if (desc.optional) {
|
|
161
|
+
parts.push("optional");
|
|
162
|
+
}
|
|
163
|
+
parts.push(typeName(desc));
|
|
164
|
+
break;
|
|
165
|
+
case "list":
|
|
166
|
+
parts.push("repeated", typeName(desc));
|
|
167
|
+
break;
|
|
168
|
+
case "map": {
|
|
169
|
+
const k = reflect_1.ScalarType[desc.mapKey].toLowerCase();
|
|
170
|
+
const v = typeName(desc);
|
|
171
|
+
parts.push(`map<${k}, ${v}>`);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
parts.push(desc.name, "=", desc.number.toString());
|
|
176
|
+
const options = [];
|
|
177
|
+
const protoOptions = desc.proto.options;
|
|
178
|
+
if (protoOptions !== undefined &&
|
|
179
|
+
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "packed")) {
|
|
180
|
+
options.push(`packed = ${protoOptions.packed.toString()}`);
|
|
181
|
+
}
|
|
182
|
+
if ((0, protobuf_1.isFieldSet)(wkt_1.FieldDescriptorProtoDesc, desc.proto, "defaultValue")) {
|
|
183
|
+
let defaultValue = desc.proto.defaultValue;
|
|
184
|
+
if (desc.proto.type == wkt_1.FieldDescriptorProto_Type.BYTES ||
|
|
185
|
+
desc.proto.type == wkt_1.FieldDescriptorProto_Type.STRING) {
|
|
186
|
+
defaultValue = '"' + defaultValue.replace('"', '\\"') + '"';
|
|
187
|
+
}
|
|
188
|
+
options.push(`default = ${defaultValue}`);
|
|
189
|
+
}
|
|
190
|
+
if (desc.kind == "field" && desc.jsonName !== (0, reflect_1.protoCamelCase)(desc.name)) {
|
|
191
|
+
options.push(`json_name = "${desc.jsonName}"`);
|
|
192
|
+
}
|
|
193
|
+
if (protoOptions !== undefined &&
|
|
194
|
+
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "jstype")) {
|
|
195
|
+
options.push(`jstype = ${wkt_1.FieldOptions_JSType[protoOptions.jstype]}`);
|
|
196
|
+
}
|
|
197
|
+
if (protoOptions !== undefined &&
|
|
198
|
+
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "deprecated")) {
|
|
199
|
+
options.push(`deprecated = true`);
|
|
200
|
+
}
|
|
201
|
+
if (options.length > 0) {
|
|
202
|
+
parts.push("[" + options.join(", ") + "]");
|
|
203
|
+
}
|
|
204
|
+
return parts.join(" ");
|
|
205
|
+
}
|
|
206
|
+
exports.getDeclarationString = getDeclarationString;
|
|
207
|
+
/**
|
|
208
|
+
* Find comments.
|
|
209
|
+
*/
|
|
210
|
+
function findComments(sourceCodeInfo, sourcePath) {
|
|
211
|
+
if (!sourceCodeInfo) {
|
|
212
|
+
return {
|
|
213
|
+
leadingDetached: [],
|
|
214
|
+
sourcePath,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
for (const location of sourceCodeInfo.location) {
|
|
218
|
+
if (location.path.length !== sourcePath.length) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (location.path.some((value, index) => sourcePath[index] !== value)) {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
leadingDetached: location.leadingDetachedComments,
|
|
226
|
+
leading: (0, protobuf_1.isFieldSet)(wkt_1.SourceCodeInfo_LocationDesc, location, "leadingComments")
|
|
227
|
+
? location.leadingComments
|
|
228
|
+
: undefined,
|
|
229
|
+
trailing: (0, protobuf_1.isFieldSet)(wkt_1.SourceCodeInfo_LocationDesc, location, "trailingComments")
|
|
230
|
+
? location.trailingComments
|
|
231
|
+
: undefined,
|
|
232
|
+
sourcePath,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
leadingDetached: [],
|
|
237
|
+
sourcePath,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The following field numbers are used to find comments in
|
|
242
|
+
* google.protobuf.SourceCodeInfo.
|
|
243
|
+
*/
|
|
244
|
+
var FieldNumber;
|
|
245
|
+
(function (FieldNumber) {
|
|
246
|
+
FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package";
|
|
247
|
+
FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType";
|
|
248
|
+
FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType";
|
|
249
|
+
FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service";
|
|
250
|
+
FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension";
|
|
251
|
+
FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax";
|
|
252
|
+
FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field";
|
|
253
|
+
FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType";
|
|
254
|
+
FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType";
|
|
255
|
+
FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension";
|
|
256
|
+
FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl";
|
|
257
|
+
FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value";
|
|
258
|
+
FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method";
|
|
259
|
+
})(FieldNumber || (FieldNumber = {}));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import type { SupportedEdition } from "@bufbuild/protobuf";
|
|
1
2
|
import type { Schema } from "./ecmascript/schema.js";
|
|
2
3
|
import type { FileInfo } from "./ecmascript/generated-file.js";
|
|
3
4
|
import type { Plugin } from "./plugin.js";
|
|
4
|
-
import type { FeatureSetDefaults } from "@bufbuild/protobuf";
|
|
5
5
|
interface PluginInit {
|
|
6
6
|
/**
|
|
7
7
|
* Name of this code generator plugin.
|
|
@@ -17,19 +17,15 @@ interface PluginInit {
|
|
|
17
17
|
*/
|
|
18
18
|
parseOption?: (key: string, value: string) => void;
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* The earliest edition supported by this plugin. Defaults to the minimum
|
|
21
|
+
* edition supported by @bufbuild/protobuf.
|
|
21
22
|
*/
|
|
22
|
-
|
|
23
|
+
minimumEdition?: SupportedEdition;
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* This option can be used to limit support for specific editions, by providing
|
|
28
|
-
* your own google.protobuf.FeatureSetDefaults generated by protoc with the
|
|
29
|
-
* flags --experimental_edition_defaults_out, --experimental_edition_defaults_minimum,
|
|
30
|
-
* and --experimental_edition_defaults_maximum.
|
|
25
|
+
* The latest edition supported by this plugin. Defaults to the maximum
|
|
26
|
+
* edition supported by @bufbuild/protobuf.
|
|
31
27
|
*/
|
|
32
|
-
|
|
28
|
+
maximumEdition?: SupportedEdition;
|
|
33
29
|
/**
|
|
34
30
|
* A function which will generate TypeScript files based on proto input.
|
|
35
31
|
* This function will be invoked by the plugin framework when the plugin runs.
|
|
@@ -11,10 +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 { create, protoInt64 } from "@bufbuild/protobuf";
|
|
15
|
+
import { minimumEdition as minimumEditionUpstream, maximumEdition as maximumEditionUpstream, } from "@bufbuild/protobuf/reflect";
|
|
16
|
+
import { CodeGeneratorResponse_Feature, CodeGeneratorResponseDesc, } from "@bufbuild/protobuf/wkt";
|
|
14
17
|
import { createSchema } from "./ecmascript/schema.js";
|
|
15
18
|
import { transpile } from "./ecmascript/transpile.js";
|
|
16
19
|
import { parseParameter } from "./ecmascript/parameter.js";
|
|
17
|
-
import { CodeGeneratorResponse, CodeGeneratorResponse_Feature, protoInt64, } from "@bufbuild/protobuf";
|
|
18
20
|
/**
|
|
19
21
|
* Create a new code generator plugin for ECMAScript.
|
|
20
22
|
* The plugin can generate JavaScript, TypeScript, or TypeScript declaration
|
|
@@ -27,9 +29,11 @@ export function createEcmaScriptPlugin(init) {
|
|
|
27
29
|
name: init.name,
|
|
28
30
|
version: init.version,
|
|
29
31
|
run(req) {
|
|
30
|
-
var _a, _b;
|
|
32
|
+
var _a, _b, _c;
|
|
33
|
+
const minimumEdition = (_a = init.minimumEdition) !== null && _a !== void 0 ? _a : minimumEditionUpstream;
|
|
34
|
+
const maximumEdition = (_b = init.maximumEdition) !== null && _b !== void 0 ? _b : maximumEditionUpstream;
|
|
31
35
|
const parameter = parseParameter(req.parameter, init.parseOption);
|
|
32
|
-
const schema = createSchema(req, parameter, init.name, init.version,
|
|
36
|
+
const schema = createSchema(req, parameter, init.name, init.version, minimumEdition, maximumEdition);
|
|
33
37
|
const targetTs = schema.targets.includes("ts");
|
|
34
38
|
const targetJs = schema.targets.includes("js");
|
|
35
39
|
const targetDts = schema.targets.includes("dts");
|
|
@@ -88,23 +92,21 @@ export function createEcmaScriptPlugin(init) {
|
|
|
88
92
|
// but no generate function was provided. This also means that we will
|
|
89
93
|
// have generated .ts files above.
|
|
90
94
|
if (transpileJs || transpileDts) {
|
|
91
|
-
const transpileFn = (
|
|
95
|
+
const transpileFn = (_c = init.transpile) !== null && _c !== void 0 ? _c : transpile;
|
|
92
96
|
// Transpile the TypeScript files and add to the master list of files
|
|
93
97
|
const transpiledFiles = transpileFn(tsFiles, transpileJs, transpileDts, parameter.jsImportStyle);
|
|
94
98
|
files.push(...transpiledFiles);
|
|
95
99
|
}
|
|
96
|
-
return toResponse(files,
|
|
100
|
+
return toResponse(files, minimumEdition, maximumEdition);
|
|
97
101
|
},
|
|
98
102
|
};
|
|
99
103
|
}
|
|
100
|
-
function toResponse(files,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return new CodeGeneratorResponse({
|
|
107
|
-
supportedFeatures: protoInt64.parse(supportedFeatures),
|
|
104
|
+
function toResponse(files, minimumEdition, maximumEdition) {
|
|
105
|
+
return create(CodeGeneratorResponseDesc, {
|
|
106
|
+
supportedFeatures: protoInt64.parse(CodeGeneratorResponse_Feature.PROTO3_OPTIONAL |
|
|
107
|
+
CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS),
|
|
108
|
+
minimumEdition,
|
|
109
|
+
maximumEdition,
|
|
108
110
|
file: files.map((f) => {
|
|
109
111
|
if (f.preamble !== undefined) {
|
|
110
112
|
f.content = f.preamble + "\n" + f.content;
|
|
@@ -11,7 +11,8 @@
|
|
|
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 { Edition } from "@bufbuild/protobuf";
|
|
14
|
+
import { Edition } from "@bufbuild/protobuf/wkt";
|
|
15
|
+
import { getPackageComments, getSyntaxComments } from "../source-code-info.js";
|
|
15
16
|
export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
|
|
16
17
|
const builder = [];
|
|
17
18
|
const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
|
|
@@ -31,14 +32,14 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
|
|
|
31
32
|
builder.push("\n");
|
|
32
33
|
}
|
|
33
34
|
};
|
|
34
|
-
writeLeadingComments(
|
|
35
|
+
writeLeadingComments(getSyntaxComments(file));
|
|
35
36
|
builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
|
|
36
37
|
if (parameter !== "") {
|
|
37
38
|
builder.push(` with parameter "${parameter}"`);
|
|
38
39
|
}
|
|
39
40
|
builder.push("\n");
|
|
40
41
|
builder.push(`// @generated from file ${file.name}.proto (`);
|
|
41
|
-
if (file.proto.package
|
|
42
|
+
if (file.proto.package.length > 0) {
|
|
42
43
|
builder.push(`package ${file.proto.package}, `);
|
|
43
44
|
}
|
|
44
45
|
switch (file.edition) {
|
|
@@ -65,6 +66,6 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
|
|
|
65
66
|
builder.push("// @ts-nocheck\n");
|
|
66
67
|
}
|
|
67
68
|
builder.push("\n");
|
|
68
|
-
writeLeadingComments(
|
|
69
|
+
writeLeadingComments(getPackageComments(file));
|
|
69
70
|
return trimSuffix(builder.join(""), "\n");
|
|
70
71
|
}
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage } from "@bufbuild/protobuf";
|
|
1
|
+
import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
|
|
2
2
|
import type { ImportSymbol } from "./import-symbol.js";
|
|
3
|
+
import type { Printable } from "./printable.js";
|
|
3
4
|
import type { RuntimeImports } from "./runtime-imports.js";
|
|
4
|
-
import type { JSDocBlock } from "./jsdoc.js";
|
|
5
|
-
import type { ExportDeclaration, LiteralProtoInt64, LiteralString, RefDescEnum, RefDescMessage } from "./opaque-printables.js";
|
|
6
|
-
/**
|
|
7
|
-
* All types that can be passed to GeneratedFile.print()
|
|
8
|
-
*/
|
|
9
|
-
export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | LiteralString | LiteralProtoInt64 | RefDescMessage | RefDescEnum | DescMessage | DescEnum | DescExtension | Printable[];
|
|
10
5
|
/**
|
|
11
6
|
* FileInfo represents an intermediate type using for transpiling TypeScript internally.
|
|
12
7
|
*/
|
|
@@ -49,10 +44,6 @@ export interface GeneratedFile {
|
|
|
49
44
|
* See print(Printable[]) for behavior when printing Printable items.
|
|
50
45
|
*/
|
|
51
46
|
print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
|
|
52
|
-
/**
|
|
53
|
-
* @deprecated Please use createImportSymbol() from @bufbuild/protoplugin/ecmascript instead
|
|
54
|
-
*/
|
|
55
|
-
export(name: string): ImportSymbol;
|
|
56
47
|
/**
|
|
57
48
|
* Create a string literal.
|
|
58
49
|
*/
|
|
@@ -61,13 +52,13 @@ export interface GeneratedFile {
|
|
|
61
52
|
* Create a JSDoc comment block with the given text. Line breaks and white-space
|
|
62
53
|
* stay intact.
|
|
63
54
|
*/
|
|
64
|
-
jsDoc(text: string, indentation?: string):
|
|
55
|
+
jsDoc(text: string, indentation?: string): Printable;
|
|
65
56
|
/**
|
|
66
57
|
* Create a JSDoc comment block for the given message, enumeration, or other
|
|
67
58
|
* descriptor. The comment block will contain the original comments from the
|
|
68
59
|
* protobuf source, and annotations such as `@generated from message MyMessage`.
|
|
69
60
|
*/
|
|
70
|
-
jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string):
|
|
61
|
+
jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): Printable;
|
|
71
62
|
/**
|
|
72
63
|
* Create a printable export statement. For example:
|
|
73
64
|
*
|
|
@@ -84,11 +75,15 @@ export interface GeneratedFile {
|
|
|
84
75
|
* statement. If the plugin option `js_import_style=legacy_commonjs` is set,
|
|
85
76
|
* exports will automatically be generated for CommonJS.
|
|
86
77
|
*/
|
|
87
|
-
exportDecl(declaration: string, name: string
|
|
78
|
+
exportDecl(declaration: string, name: string): Printable;
|
|
88
79
|
/**
|
|
89
80
|
* Import a message or enumeration generated by protoc-gen-es.
|
|
90
81
|
*/
|
|
91
|
-
|
|
82
|
+
importShape(desc: DescMessage | DescEnum): ImportSymbol;
|
|
83
|
+
/**
|
|
84
|
+
* Import a descriptor generated by protoc-gen-es.
|
|
85
|
+
*/
|
|
86
|
+
importDesc(desc: DescMessage | DescEnum | DescExtension | DescService | DescFile, typeOnly?: boolean): ImportSymbol;
|
|
92
87
|
/**
|
|
93
88
|
* Import any symbol from a file or package.
|
|
94
89
|
*
|
|
@@ -105,12 +100,16 @@ export interface GeneratedFile {
|
|
|
105
100
|
* formulate your own imports and exports based on this property.
|
|
106
101
|
*/
|
|
107
102
|
readonly jsImportStyle: "module" | "legacy_commonjs";
|
|
103
|
+
/**
|
|
104
|
+
* Provides some symbols from the runtime library @bufbuild/protobuf.
|
|
105
|
+
*/
|
|
106
|
+
readonly runtime: RuntimeImports;
|
|
108
107
|
}
|
|
109
108
|
export interface GeneratedFileController extends GeneratedFile {
|
|
110
109
|
getFileInfo(): FileInfo;
|
|
111
110
|
}
|
|
112
|
-
type
|
|
113
|
-
type
|
|
114
|
-
type
|
|
115
|
-
export
|
|
116
|
-
export
|
|
111
|
+
export type ResolveDescImportFn = (desc: DescMessage | DescEnum | DescExtension | DescService | DescFile, typeOnly?: boolean) => ImportSymbol;
|
|
112
|
+
export type ResolveShapeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
|
|
113
|
+
export type RewriteImportFn = (path: string) => string;
|
|
114
|
+
export type CreatePreambleFn = (descFile: DescFile) => string;
|
|
115
|
+
export declare function createGeneratedFile(name: string, importPath: string, jsImportStyle: "module" | "legacy_commonjs", rewriteImport: RewriteImportFn, resolveDescImport: ResolveDescImportFn, resolveShapeImport: ResolveShapeImportFn, createPreamble: CreatePreambleFn, runtime: RuntimeImports): GeneratedFileController;
|