@bufbuild/protoplugin 1.9.0 → 2.0.0-alpha.2
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 +11 -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 +39 -35
- 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 +3 -3
- 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 +72 -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 +23 -0
- package/dist/cjs/source-code-info.js +286 -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 +8 -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 +36 -33
- 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 +3 -3
- 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 +74 -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 +23 -0
- package/dist/esm/source-code-info.js +278 -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,64 @@ 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
|
+
// Our goal is to provide options with source retention to plugin authors.
|
|
96
|
+
// CodeGeneratorRequest.proto_file elides options with source retention for
|
|
97
|
+
// files to generate. For these files, we take the file from source_file_descriptors,
|
|
98
|
+
// which does include options with source retention.
|
|
99
|
+
const allProtoWithSourceOptions = request.protoFile.map((protoFile) => {
|
|
100
|
+
const sourceFile = request.sourceFileDescriptors.find((s) => s.name == protoFile.name);
|
|
101
|
+
return sourceFile !== null && sourceFile !== void 0 ? sourceFile : protoFile;
|
|
102
|
+
});
|
|
103
|
+
const registry = (0, reflect_1.createFileRegistry)((0, protobuf_1.create)(wkt_1.FileDescriptorSetDesc, {
|
|
104
|
+
file: allProtoWithSourceOptions,
|
|
105
|
+
}));
|
|
106
|
+
const allFiles = [];
|
|
107
|
+
const filesToGenerate = [];
|
|
108
|
+
for (const file of registry.files) {
|
|
109
|
+
allFiles.push(file);
|
|
110
|
+
if (request.fileToGenerate.includes(file.proto.name)) {
|
|
111
|
+
filesToGenerate.push(file);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { allFiles, filesToGenerate };
|
|
115
|
+
}
|
|
116
|
+
function editionToString(edition) {
|
|
117
|
+
if (edition in wkt_1.Edition) {
|
|
118
|
+
return wkt_1.Edition[edition].replace(/^EDITION_/, "");
|
|
67
119
|
}
|
|
68
|
-
return
|
|
120
|
+
return `unknown (${edition})`;
|
|
69
121
|
}
|
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,23 @@
|
|
|
1
|
+
import { type AnyDesc, type DescComments, type DescEnumValue, type DescExtension, type DescField, type 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
|
+
* Get feature options set on the element in the protobuf source. This returns
|
|
16
|
+
* compact (e.g. fields) or regular options (e.g. files) as an array of strings.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getFeatureOptionStrings(desc: AnyDesc): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Return a string that matches the definition of a field in the protobuf
|
|
21
|
+
* source. Does not take custom options into account.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getDeclarationString(desc: DescField | DescExtension | DescEnumValue): string;
|
|
@@ -0,0 +1,286 @@
|
|
|
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.getFeatureOptionStrings = 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
|
+
wkt_1.FileDescriptorProtoDesc.field.package.number,
|
|
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
|
+
wkt_1.FileDescriptorProtoDesc.field.syntax.number,
|
|
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
|
+
wkt_1.DescriptorProtoDesc.field.enumType.number,
|
|
50
|
+
desc.parent.proto.enumType.indexOf(desc.proto),
|
|
51
|
+
]
|
|
52
|
+
: [
|
|
53
|
+
wkt_1.FileDescriptorProtoDesc.field.enumType.number,
|
|
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
|
+
wkt_1.DescriptorProtoDesc.field.oneofDecl.number,
|
|
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
|
+
wkt_1.DescriptorProtoDesc.field.nestedType.number,
|
|
71
|
+
desc.parent.proto.nestedType.indexOf(desc.proto),
|
|
72
|
+
]
|
|
73
|
+
: [
|
|
74
|
+
wkt_1.FileDescriptorProtoDesc.field.messageType.number,
|
|
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
|
+
wkt_1.EnumDescriptorProtoDesc.field.value.number,
|
|
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
|
+
wkt_1.DescriptorProtoDesc.field.field.number,
|
|
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
|
+
wkt_1.DescriptorProtoDesc.field.extension.number,
|
|
100
|
+
desc.parent.proto.extension.indexOf(desc.proto),
|
|
101
|
+
]
|
|
102
|
+
: [
|
|
103
|
+
wkt_1.FileDescriptorProtoDesc.field.extension.number,
|
|
104
|
+
desc.file.proto.extension.indexOf(desc.proto),
|
|
105
|
+
];
|
|
106
|
+
file = desc.file;
|
|
107
|
+
break;
|
|
108
|
+
case "service":
|
|
109
|
+
path = [
|
|
110
|
+
wkt_1.FileDescriptorProtoDesc.field.service.number,
|
|
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
|
+
wkt_1.ServiceDescriptorProtoDesc.field.method.number,
|
|
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
|
+
* Get feature options set on the element in the protobuf source. This returns
|
|
129
|
+
* compact (e.g. fields) or regular options (e.g. files) as an array of strings.
|
|
130
|
+
*/
|
|
131
|
+
function getFeatureOptionStrings(desc) {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
const strings = [];
|
|
134
|
+
const features = (_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.features;
|
|
135
|
+
if (features !== undefined) {
|
|
136
|
+
const r = (0, reflect_1.reflect)(wkt_1.FeatureSetDesc, features);
|
|
137
|
+
for (const f of r.fields) {
|
|
138
|
+
if (f.fieldKind != "enum" || !r.isSet(f)) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const val = r.get(f);
|
|
142
|
+
const name = (_b = f.enum.values.find((v) => v.number == val)) === null || _b === void 0 ? void 0 : _b.name;
|
|
143
|
+
if (name !== undefined) {
|
|
144
|
+
strings.push(`features.${f.name} = ${name}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return strings;
|
|
149
|
+
}
|
|
150
|
+
exports.getFeatureOptionStrings = getFeatureOptionStrings;
|
|
151
|
+
/**
|
|
152
|
+
* Return a string that matches the definition of a field in the protobuf
|
|
153
|
+
* source. Does not take custom options into account.
|
|
154
|
+
*/
|
|
155
|
+
function getDeclarationString(desc) {
|
|
156
|
+
var _a;
|
|
157
|
+
if (desc.kind === "enum_value") {
|
|
158
|
+
let str = `${desc.name} = ${desc.number}`;
|
|
159
|
+
if (((_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) === true) {
|
|
160
|
+
str += " [deprecated = true]";
|
|
161
|
+
}
|
|
162
|
+
return str;
|
|
163
|
+
}
|
|
164
|
+
const parts = [];
|
|
165
|
+
function typeName(f) {
|
|
166
|
+
if (f.message) {
|
|
167
|
+
return f.message.typeName;
|
|
168
|
+
}
|
|
169
|
+
if (f.enum) {
|
|
170
|
+
return f.enum.typeName;
|
|
171
|
+
}
|
|
172
|
+
return reflect_1.ScalarType[f.scalar].toLowerCase();
|
|
173
|
+
}
|
|
174
|
+
switch (desc.fieldKind) {
|
|
175
|
+
case "scalar":
|
|
176
|
+
case "enum":
|
|
177
|
+
case "message":
|
|
178
|
+
if (fieldHasRequiredKeyword(desc)) {
|
|
179
|
+
parts.push("required");
|
|
180
|
+
}
|
|
181
|
+
if (fieldHasOptionalKeyword(desc)) {
|
|
182
|
+
parts.push("optional");
|
|
183
|
+
}
|
|
184
|
+
parts.push(typeName(desc));
|
|
185
|
+
break;
|
|
186
|
+
case "list":
|
|
187
|
+
parts.push("repeated", typeName(desc));
|
|
188
|
+
break;
|
|
189
|
+
case "map": {
|
|
190
|
+
const k = reflect_1.ScalarType[desc.mapKey].toLowerCase();
|
|
191
|
+
const v = typeName(desc);
|
|
192
|
+
parts.push(`map<${k}, ${v}>`);
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
parts.push(desc.name, "=", desc.number.toString());
|
|
197
|
+
const options = [];
|
|
198
|
+
const protoOptions = desc.proto.options;
|
|
199
|
+
if (protoOptions !== undefined &&
|
|
200
|
+
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.packed)) {
|
|
201
|
+
options.push(`packed = ${protoOptions.packed.toString()}`);
|
|
202
|
+
}
|
|
203
|
+
if ((0, protobuf_1.isFieldSet)(desc.proto, wkt_1.FieldDescriptorProtoDesc.field.defaultValue)) {
|
|
204
|
+
let defaultValue = desc.proto.defaultValue;
|
|
205
|
+
if (desc.proto.type == wkt_1.FieldDescriptorProto_Type.BYTES ||
|
|
206
|
+
desc.proto.type == wkt_1.FieldDescriptorProto_Type.STRING) {
|
|
207
|
+
defaultValue = '"' + defaultValue.replace('"', '\\"') + '"';
|
|
208
|
+
}
|
|
209
|
+
options.push(`default = ${defaultValue}`);
|
|
210
|
+
}
|
|
211
|
+
if (desc.kind == "field" && desc.jsonName !== (0, reflect_1.protoCamelCase)(desc.name)) {
|
|
212
|
+
options.push(`json_name = "${desc.jsonName}"`);
|
|
213
|
+
}
|
|
214
|
+
if (protoOptions !== undefined &&
|
|
215
|
+
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.jstype)) {
|
|
216
|
+
options.push(`jstype = ${wkt_1.FieldOptions_JSType[protoOptions.jstype]}`);
|
|
217
|
+
}
|
|
218
|
+
if (protoOptions !== undefined &&
|
|
219
|
+
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.deprecated)) {
|
|
220
|
+
options.push(`deprecated = true`);
|
|
221
|
+
}
|
|
222
|
+
options.push(...getFeatureOptionStrings(desc));
|
|
223
|
+
if (options.length > 0) {
|
|
224
|
+
parts.push("[" + options.join(", ") + "]");
|
|
225
|
+
}
|
|
226
|
+
return parts.join(" ");
|
|
227
|
+
}
|
|
228
|
+
exports.getDeclarationString = getDeclarationString;
|
|
229
|
+
/**
|
|
230
|
+
* Whether this field was declared with `required` in the protobuf source.
|
|
231
|
+
*/
|
|
232
|
+
function fieldHasRequiredKeyword(field) {
|
|
233
|
+
const edition = (field.kind == "extension" ? field.file : field.parent.file)
|
|
234
|
+
.edition;
|
|
235
|
+
return (edition == wkt_1.Edition.EDITION_PROTO2 &&
|
|
236
|
+
field.proto.label == wkt_1.FieldDescriptorProto_Label.REQUIRED);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Whether this field was declared with `optional` in the protobuf source.
|
|
240
|
+
* Note that message fields are always optional. It is impossible to determine
|
|
241
|
+
* whether the keyword was used.
|
|
242
|
+
*/
|
|
243
|
+
function fieldHasOptionalKeyword(field) {
|
|
244
|
+
const edition = (field.kind == "extension" ? field.file : field.parent.file)
|
|
245
|
+
.edition;
|
|
246
|
+
if (edition == wkt_1.Edition.EDITION_PROTO2) {
|
|
247
|
+
return (!field.oneof && field.proto.label == wkt_1.FieldDescriptorProto_Label.OPTIONAL);
|
|
248
|
+
}
|
|
249
|
+
if (edition == wkt_1.Edition.EDITION_PROTO3) {
|
|
250
|
+
return field.proto.proto3Optional;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Find comments.
|
|
256
|
+
*/
|
|
257
|
+
function findComments(sourceCodeInfo, sourcePath) {
|
|
258
|
+
if (!sourceCodeInfo) {
|
|
259
|
+
return {
|
|
260
|
+
leadingDetached: [],
|
|
261
|
+
sourcePath,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
for (const location of sourceCodeInfo.location) {
|
|
265
|
+
if (location.path.length !== sourcePath.length) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (location.path.some((value, index) => sourcePath[index] !== value)) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
leadingDetached: location.leadingDetachedComments,
|
|
273
|
+
leading: (0, protobuf_1.isFieldSet)(location, wkt_1.SourceCodeInfo_LocationDesc.field.leadingComments)
|
|
274
|
+
? location.leadingComments
|
|
275
|
+
: undefined,
|
|
276
|
+
trailing: (0, protobuf_1.isFieldSet)(location, wkt_1.SourceCodeInfo_LocationDesc.field.trailingComments)
|
|
277
|
+
? location.trailingComments
|
|
278
|
+
: undefined,
|
|
279
|
+
sourcePath,
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
leadingDetached: [],
|
|
284
|
+
sourcePath,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
@@ -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, getFeatureOptionStrings, } 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) {
|
|
@@ -60,11 +61,14 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
|
|
|
60
61
|
break;
|
|
61
62
|
}
|
|
62
63
|
}
|
|
64
|
+
for (const o of getFeatureOptionStrings(file)) {
|
|
65
|
+
builder.push(`// option ${o};\n`);
|
|
66
|
+
}
|
|
63
67
|
builder.push("/* eslint-disable */\n");
|
|
64
68
|
if (tsNoCheck) {
|
|
65
69
|
builder.push("// @ts-nocheck\n");
|
|
66
70
|
}
|
|
67
71
|
builder.push("\n");
|
|
68
|
-
writeLeadingComments(
|
|
72
|
+
writeLeadingComments(getPackageComments(file));
|
|
69
73
|
return trimSuffix(builder.join(""), "\n");
|
|
70
74
|
}
|