@bufbuild/protoplugin 2.0.0-alpha.4 → 2.0.0-beta.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/README.md +12 -12
- package/dist/cjs/create-es-plugin.d.ts +7 -6
- package/dist/cjs/create-es-plugin.js +3 -3
- package/dist/cjs/generated-file.d.ts +17 -5
- package/dist/cjs/generated-file.js +18 -11
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/names.d.ts +15 -2
- package/dist/cjs/names.js +107 -32
- package/dist/cjs/parameter.d.ts +67 -6
- package/dist/cjs/parameter.js +23 -11
- package/dist/cjs/printable.d.ts +5 -1
- package/dist/cjs/run-node.js +2 -2
- package/dist/cjs/schema.d.ts +9 -4
- package/dist/cjs/schema.js +12 -10
- package/dist/cjs/source-code-info.js +20 -20
- package/dist/cjs/transpile.js +2 -2
- package/dist/esm/create-es-plugin.d.ts +7 -6
- package/dist/esm/create-es-plugin.js +4 -4
- package/dist/esm/generated-file.d.ts +17 -5
- package/dist/esm/generated-file.js +18 -11
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/names.d.ts +15 -2
- package/dist/esm/names.js +103 -29
- package/dist/esm/parameter.d.ts +67 -6
- package/dist/esm/parameter.js +23 -11
- package/dist/esm/printable.d.ts +5 -1
- package/dist/esm/run-node.js +3 -3
- package/dist/esm/schema.d.ts +9 -4
- package/dist/esm/schema.js +14 -12
- package/dist/esm/source-code-info.js +21 -21
- package/dist/esm/transpile.js +2 -2
- package/package.json +10 -7
package/dist/esm/names.js
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
import { wktPublicImportPaths } from "@bufbuild/protobuf/codegenv1";
|
|
15
15
|
import { nestedTypes } from "@bufbuild/protobuf/reflect";
|
|
16
16
|
import { safeIdentifier } from "./safe-identifier.js";
|
|
17
|
+
/**
|
|
18
|
+
* Return a file path for the give file descriptor.
|
|
19
|
+
*/
|
|
17
20
|
export function generateFilePath(file, bootstrapWkt, filesToGenerate) {
|
|
18
21
|
// Well-known types are published with the runtime package. We usually want
|
|
19
22
|
// the generated code to import them from the runtime package, with the
|
|
@@ -30,84 +33,155 @@ export function generateFilePath(file, bootstrapWkt, filesToGenerate) {
|
|
|
30
33
|
}
|
|
31
34
|
return "./" + file.name + "_pb.js";
|
|
32
35
|
}
|
|
33
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Return a safe identifier for a generated descriptor.
|
|
38
|
+
*/
|
|
39
|
+
export function generatedDescName(desc) {
|
|
34
40
|
const file = desc.kind == "file" ? desc : desc.file;
|
|
35
41
|
const { descNames } = allNames(file);
|
|
36
42
|
const name = descNames.get(desc);
|
|
37
43
|
if (name === undefined) {
|
|
38
|
-
throw new Error(`unable to determine unique
|
|
44
|
+
throw new Error(`unable to determine unique descriptor name for ${desc.toString()}`);
|
|
39
45
|
}
|
|
40
46
|
return name;
|
|
41
47
|
}
|
|
42
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Return a safe identifier for a generated shape.
|
|
50
|
+
*/
|
|
51
|
+
export function generatedShapeName(desc) {
|
|
43
52
|
const { shapeNames } = allNames(desc.file);
|
|
44
53
|
const name = shapeNames.get(desc);
|
|
45
54
|
if (name === undefined) {
|
|
46
|
-
throw new Error(`unable to determine unique
|
|
55
|
+
throw new Error(`unable to determine unique shape name for ${desc.toString()}`);
|
|
47
56
|
}
|
|
48
57
|
return name;
|
|
49
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Return a safe identifier for a generated JSON type.
|
|
61
|
+
*/
|
|
62
|
+
export function generatedJsonTypeName(desc) {
|
|
63
|
+
const { jsonTypeNames } = allNames(desc.file);
|
|
64
|
+
const name = jsonTypeNames.get(desc);
|
|
65
|
+
if (name === undefined) {
|
|
66
|
+
throw new Error(`unable to determine unique json type name for ${desc.toString()}`);
|
|
67
|
+
}
|
|
68
|
+
return name;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Compute the ideal name for a generated descriptor.
|
|
72
|
+
*/
|
|
50
73
|
function idealDescName(desc, i) {
|
|
51
74
|
const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
|
|
75
|
+
if (desc.kind == "file") {
|
|
76
|
+
const name = "file_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_");
|
|
77
|
+
return safeIdentifier(name + escape);
|
|
78
|
+
}
|
|
52
79
|
switch (desc.kind) {
|
|
53
|
-
case "file":
|
|
54
|
-
return (safeIdentifier("fileDesc_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_")) + escape);
|
|
55
80
|
case "enum":
|
|
56
|
-
return
|
|
81
|
+
return safeIdentifier(identifier(desc) + "Schema" + escape);
|
|
57
82
|
case "message":
|
|
58
|
-
return
|
|
83
|
+
return safeIdentifier(identifier(desc) + "Schema" + escape);
|
|
59
84
|
case "extension":
|
|
60
|
-
return
|
|
85
|
+
return safeIdentifier(identifier(desc) + escape);
|
|
61
86
|
case "service":
|
|
62
|
-
return
|
|
87
|
+
return safeIdentifier(identifier(desc) + escape);
|
|
63
88
|
}
|
|
64
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Compute the ideal name for a generated shape.
|
|
92
|
+
*/
|
|
65
93
|
function idealShapeName(desc, i) {
|
|
66
94
|
const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
|
|
67
|
-
return
|
|
95
|
+
return safeIdentifier(identifier(desc) + escape);
|
|
68
96
|
}
|
|
69
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Compute the ideal name for a generated JSON type.
|
|
99
|
+
*/
|
|
100
|
+
function idealJsonTypeName(desc, i) {
|
|
101
|
+
const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
|
|
102
|
+
return safeIdentifier(identifier(desc) + "Json" + escape);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Return an identifier for the given descriptor based on its type name.
|
|
106
|
+
*
|
|
107
|
+
* The type name for a protobuf message is the package name (if any), plus
|
|
108
|
+
* the names of parent messages it is nested in (if any), plus the name of
|
|
109
|
+
* the element, separated by dots. For example: foo.bar.ParentMsg.MyEnum.
|
|
110
|
+
*
|
|
111
|
+
* ECMAScript does not have packages or namespaces, so we need a single
|
|
112
|
+
* identifier. Our convention is to drop the package name, and to join other
|
|
113
|
+
* parts of the name with an underscore. For example: ParentMsg_MyEnum.
|
|
114
|
+
*/
|
|
115
|
+
function identifier(desc) {
|
|
70
116
|
const pkg = desc.file.proto.package;
|
|
71
117
|
const offset = pkg.length > 0 ? pkg.length + 1 : 0;
|
|
72
|
-
const
|
|
73
|
-
return
|
|
118
|
+
const nameWithoutPkg = desc.typeName.substring(offset);
|
|
119
|
+
return nameWithoutPkg.replace(/\./g, "_");
|
|
74
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Compute all ideal names for the elements in the file, resolving name clashes.
|
|
123
|
+
*/
|
|
75
124
|
function allNames(file) {
|
|
76
125
|
const taken = new Set();
|
|
126
|
+
// In the first pass, register shape names
|
|
77
127
|
const shapeNames = new Map();
|
|
128
|
+
for (const desc of nestedTypes(file)) {
|
|
129
|
+
if (desc.kind != "enum" && desc.kind != "message") {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
let name;
|
|
133
|
+
for (let i = 0;; i++) {
|
|
134
|
+
name = idealShapeName(desc, i);
|
|
135
|
+
if (!taken.has(name)) {
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
taken.add(name);
|
|
140
|
+
shapeNames.set(desc, name);
|
|
141
|
+
}
|
|
142
|
+
// In the second pass, register desc names
|
|
78
143
|
const descNames = new Map();
|
|
79
144
|
for (const desc of [file, ...nestedTypes(file)]) {
|
|
145
|
+
let name;
|
|
80
146
|
switch (desc.kind) {
|
|
81
147
|
case "enum":
|
|
82
148
|
case "message": {
|
|
83
|
-
let descName;
|
|
84
|
-
let shapeName;
|
|
85
149
|
for (let i = 0;; i++) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (!taken.has(descName) && !taken.has(shapeName)) {
|
|
150
|
+
name = idealDescName(desc, i);
|
|
151
|
+
if (!taken.has(name)) {
|
|
89
152
|
break;
|
|
90
153
|
}
|
|
91
154
|
}
|
|
92
|
-
taken.add(descName);
|
|
93
|
-
taken.add(shapeName);
|
|
94
|
-
descNames.set(desc, descName);
|
|
95
|
-
shapeNames.set(desc, shapeName);
|
|
96
155
|
break;
|
|
97
156
|
}
|
|
98
157
|
default: {
|
|
99
|
-
let descName;
|
|
100
158
|
for (let i = 0;; i++) {
|
|
101
|
-
|
|
102
|
-
if (!taken.has(
|
|
159
|
+
name = idealDescName(desc, i);
|
|
160
|
+
if (!taken.has(name)) {
|
|
103
161
|
break;
|
|
104
162
|
}
|
|
105
163
|
}
|
|
106
|
-
taken.add(descName);
|
|
107
|
-
descNames.set(desc, descName);
|
|
108
164
|
break;
|
|
109
165
|
}
|
|
110
166
|
}
|
|
167
|
+
taken.add(name);
|
|
168
|
+
descNames.set(desc, name);
|
|
169
|
+
}
|
|
170
|
+
// In the third pass, register json type names
|
|
171
|
+
const jsonTypeNames = new Map();
|
|
172
|
+
for (const desc of nestedTypes(file)) {
|
|
173
|
+
if (desc.kind != "enum" && desc.kind != "message") {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
let name;
|
|
177
|
+
for (let i = 0;; i++) {
|
|
178
|
+
name = idealJsonTypeName(desc, i);
|
|
179
|
+
if (!taken.has(name)) {
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
taken.add(name);
|
|
184
|
+
jsonTypeNames.set(desc, name);
|
|
111
185
|
}
|
|
112
|
-
return { shapeNames, descNames };
|
|
186
|
+
return { shapeNames, jsonTypeNames, descNames };
|
|
113
187
|
}
|
package/dist/esm/parameter.d.ts
CHANGED
|
@@ -1,13 +1,74 @@
|
|
|
1
1
|
import type { Target } from "./target.js";
|
|
2
2
|
import type { RewriteImports } from "./import-path.js";
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Standard plugin options that every ECMAScript plugin supports.
|
|
5
|
+
*/
|
|
6
|
+
export interface EcmaScriptPluginOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Controls whether the plugin generates JavaScript, TypeScript,
|
|
9
|
+
* or TypeScript declaration files.
|
|
10
|
+
*
|
|
11
|
+
* The default is ["js", "dts].
|
|
12
|
+
*/
|
|
4
13
|
targets: Target[];
|
|
14
|
+
/**
|
|
15
|
+
* Add an extension to every import, for example ".js" or ".ts".
|
|
16
|
+
*
|
|
17
|
+
* The default is "".
|
|
18
|
+
*/
|
|
19
|
+
importExtension: string;
|
|
20
|
+
/**
|
|
21
|
+
* Generate `import` statements or `require()` calls.
|
|
22
|
+
*
|
|
23
|
+
* The default is "module".
|
|
24
|
+
*/
|
|
25
|
+
jsImportStyle: "module" | "legacy_commonjs";
|
|
26
|
+
/**
|
|
27
|
+
* Generate an annotation at the top of each file to skip type checks:
|
|
28
|
+
* `// @ts-nocheck`.
|
|
29
|
+
*
|
|
30
|
+
* The default is false.
|
|
31
|
+
*/
|
|
5
32
|
tsNocheck: boolean;
|
|
6
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Prune empty files from the output.
|
|
35
|
+
*
|
|
36
|
+
* The default is false.
|
|
37
|
+
*/
|
|
7
38
|
keepEmptyFiles: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
bootstrapWkt: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
8
46
|
rewriteImports: RewriteImports;
|
|
9
|
-
importExtension: string;
|
|
10
|
-
jsImportStyle: "module" | "legacy_commonjs";
|
|
11
|
-
sanitizedParameter: string;
|
|
12
47
|
}
|
|
13
|
-
export
|
|
48
|
+
export interface ParsedParameter<T> {
|
|
49
|
+
parsed: T & EcmaScriptPluginOptions;
|
|
50
|
+
sanitized: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Raw options to parse.
|
|
54
|
+
*
|
|
55
|
+
* For example, if a plugin is run with the options foo=123,bar,baz=a,baz=b
|
|
56
|
+
* the raw options are:
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* [
|
|
60
|
+
* { key: "foo", value: "123" },
|
|
61
|
+
* { key: "bar", value: "" },
|
|
62
|
+
* { key: "baz", value: "a" },
|
|
63
|
+
* { key: "baz", value: "b" },
|
|
64
|
+
* ]
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* If your plugin does not recognize an option, it must throw an Error in
|
|
68
|
+
* parseOptions.
|
|
69
|
+
*/
|
|
70
|
+
export type RawPluginOptions = {
|
|
71
|
+
key: string;
|
|
72
|
+
value: string;
|
|
73
|
+
}[];
|
|
74
|
+
export declare function parseParameter<T extends object>(parameter: string, parseExtraOptions: ((rawOptions: RawPluginOptions) => T) | undefined): ParsedParameter<T>;
|
package/dist/esm/parameter.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { PluginOptionError } from "./error.js";
|
|
15
|
-
export function parseParameter(parameter,
|
|
15
|
+
export function parseParameter(parameter, parseExtraOptions) {
|
|
16
16
|
let targets = ["js", "dts"];
|
|
17
17
|
let tsNocheck = false;
|
|
18
18
|
let bootstrapWkt = false;
|
|
@@ -20,6 +20,8 @@ export function parseParameter(parameter, parseExtraOption) {
|
|
|
20
20
|
const rewriteImports = [];
|
|
21
21
|
let importExtension = "";
|
|
22
22
|
let jsImportStyle = "module";
|
|
23
|
+
const extraParameters = [];
|
|
24
|
+
const extraParametersRaw = [];
|
|
23
25
|
const rawParameters = [];
|
|
24
26
|
for (const { key, value, raw } of splitParameter(parameter)) {
|
|
25
27
|
// Whether this key/value plugin parameter pair should be
|
|
@@ -115,23 +117,19 @@ export function parseParameter(parameter, parseExtraOption) {
|
|
|
115
117
|
break;
|
|
116
118
|
}
|
|
117
119
|
default:
|
|
118
|
-
if (
|
|
120
|
+
if (parseExtraOptions === undefined) {
|
|
119
121
|
throw new PluginOptionError(raw);
|
|
120
122
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
catch (e) {
|
|
125
|
-
throw new PluginOptionError(raw, e);
|
|
126
|
-
}
|
|
123
|
+
extraParameters.push({ key, value });
|
|
124
|
+
extraParametersRaw.push(raw);
|
|
127
125
|
break;
|
|
128
126
|
}
|
|
129
127
|
if (!sanitize) {
|
|
130
128
|
rawParameters.push(raw);
|
|
131
129
|
}
|
|
132
130
|
}
|
|
133
|
-
const
|
|
134
|
-
|
|
131
|
+
const sanitizedParameters = rawParameters.join(",");
|
|
132
|
+
const ecmaScriptPluginOptions = {
|
|
135
133
|
targets,
|
|
136
134
|
tsNocheck,
|
|
137
135
|
bootstrapWkt,
|
|
@@ -139,8 +137,22 @@ export function parseParameter(parameter, parseExtraOption) {
|
|
|
139
137
|
importExtension,
|
|
140
138
|
jsImportStyle,
|
|
141
139
|
keepEmptyFiles,
|
|
142
|
-
sanitizedParameter,
|
|
143
140
|
};
|
|
141
|
+
if (parseExtraOptions === undefined || extraParameters.length === 0) {
|
|
142
|
+
return {
|
|
143
|
+
parsed: ecmaScriptPluginOptions,
|
|
144
|
+
sanitized: sanitizedParameters,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
return {
|
|
149
|
+
parsed: Object.assign(ecmaScriptPluginOptions, parseExtraOptions(extraParameters)),
|
|
150
|
+
sanitized: sanitizedParameters,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
throw new PluginOptionError(extraParametersRaw.join(","), err);
|
|
155
|
+
}
|
|
144
156
|
}
|
|
145
157
|
function splitParameter(parameter) {
|
|
146
158
|
if (parameter.length == 0) {
|
package/dist/esm/printable.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ImportSymbol } from "./import-symbol.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* All types that can be passed to GeneratedFile.print()
|
|
5
5
|
*/
|
|
6
|
-
export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportStatement | JSDocBlock | LiteralString | LiteralProtoInt64 | DescImport | ShapeImport | Printable[];
|
|
6
|
+
export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportStatement | JSDocBlock | LiteralString | LiteralProtoInt64 | DescImport | ShapeImport | JsonTypeImport | Printable[];
|
|
7
7
|
export type ExportStatement = {
|
|
8
8
|
kind: "es_export_stmt";
|
|
9
9
|
name: string;
|
|
@@ -28,6 +28,10 @@ export type ShapeImport = {
|
|
|
28
28
|
readonly kind: "es_shape_ref";
|
|
29
29
|
desc: DescMessage | DescEnum;
|
|
30
30
|
};
|
|
31
|
+
export type JsonTypeImport = {
|
|
32
|
+
readonly kind: "es_json_type_ref";
|
|
33
|
+
desc: DescMessage | DescEnum;
|
|
34
|
+
};
|
|
31
35
|
export type JSDocBlock = {
|
|
32
36
|
readonly kind: "es_jsdoc";
|
|
33
37
|
text: string;
|
package/dist/esm/run-node.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { isPluginOptionError, reasonToString } from "./error.js";
|
|
15
15
|
import { fromBinary, toBinary } from "@bufbuild/protobuf";
|
|
16
|
-
import {
|
|
16
|
+
import { CodeGeneratorRequestSchema, CodeGeneratorResponseSchema, } from "@bufbuild/protobuf/wkt";
|
|
17
17
|
/**
|
|
18
18
|
* Run a plugin with Node.js.
|
|
19
19
|
*
|
|
@@ -38,9 +38,9 @@ export function runNodeJs(plugin) {
|
|
|
38
38
|
}
|
|
39
39
|
readBytes(process.stdin)
|
|
40
40
|
.then((data) => {
|
|
41
|
-
const req = fromBinary(
|
|
41
|
+
const req = fromBinary(CodeGeneratorRequestSchema, data);
|
|
42
42
|
const res = plugin.run(req);
|
|
43
|
-
return writeBytes(process.stdout, toBinary(
|
|
43
|
+
return writeBytes(process.stdout, toBinary(CodeGeneratorResponseSchema, res));
|
|
44
44
|
})
|
|
45
45
|
.then(() => process.exit(0))
|
|
46
46
|
.catch((reason) => {
|
package/dist/esm/schema.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ import type { CodeGeneratorRequest } from "@bufbuild/protobuf/wkt";
|
|
|
3
3
|
import { Edition } from "@bufbuild/protobuf/wkt";
|
|
4
4
|
import type { FileInfo, GeneratedFile } from "./generated-file.js";
|
|
5
5
|
import type { Target } from "./target.js";
|
|
6
|
-
import type { ParsedParameter } from "./parameter.js";
|
|
6
|
+
import type { EcmaScriptPluginOptions, ParsedParameter } from "./parameter.js";
|
|
7
7
|
/**
|
|
8
8
|
* Schema describes the files and types that the plugin is requested to
|
|
9
9
|
* generate.
|
|
10
10
|
*/
|
|
11
|
-
export interface Schema {
|
|
11
|
+
export interface Schema<Options extends object = object> {
|
|
12
12
|
/**
|
|
13
13
|
* The files we are asked to generate.
|
|
14
14
|
*/
|
|
@@ -21,6 +21,11 @@ export interface Schema {
|
|
|
21
21
|
* The plugin option `target`. A code generator should support all targets.
|
|
22
22
|
*/
|
|
23
23
|
readonly targets: readonly Target[];
|
|
24
|
+
/**
|
|
25
|
+
* Parsed plugin options. They include the standard options for all
|
|
26
|
+
* plugins, and options parsed by your plugin.
|
|
27
|
+
*/
|
|
28
|
+
readonly options: Options & EcmaScriptPluginOptions;
|
|
24
29
|
/**
|
|
25
30
|
* Generate a new file with the given name.
|
|
26
31
|
*/
|
|
@@ -35,9 +40,9 @@ export interface Schema {
|
|
|
35
40
|
*/
|
|
36
41
|
readonly proto: CodeGeneratorRequest;
|
|
37
42
|
}
|
|
38
|
-
interface SchemaController extends Schema {
|
|
43
|
+
interface SchemaController<Options extends object> extends Schema<Options> {
|
|
39
44
|
getFileInfo: () => FileInfo[];
|
|
40
45
|
prepareGenerate(target: Target): void;
|
|
41
46
|
}
|
|
42
|
-
export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter
|
|
47
|
+
export declare function createSchema<T extends object>(request: CodeGeneratorRequest, parameter: ParsedParameter<T>, pluginName: string, pluginVersion: string, minimumEdition: Edition, maximumEdition: Edition): SchemaController<T>;
|
|
43
48
|
export {};
|
package/dist/esm/schema.js
CHANGED
|
@@ -12,42 +12,44 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { create, createFileRegistry } from "@bufbuild/protobuf";
|
|
15
|
-
import { Edition,
|
|
15
|
+
import { Edition, FileDescriptorSetSchema } from "@bufbuild/protobuf/wkt";
|
|
16
16
|
import { nestedTypes } from "@bufbuild/protobuf/reflect";
|
|
17
17
|
import { createGeneratedFile } from "./generated-file.js";
|
|
18
18
|
import { createImportSymbol } from "./import-symbol.js";
|
|
19
19
|
import { deriveImportPath, rewriteImportPath } from "./import-path.js";
|
|
20
20
|
import { makeFilePreamble } from "./file-preamble.js";
|
|
21
|
-
import {
|
|
21
|
+
import { generatedDescName, generatedShapeName, generateFilePath, generatedJsonTypeName, } from "./names.js";
|
|
22
22
|
import { createRuntimeImports } from "./runtime-imports.js";
|
|
23
23
|
export function createSchema(request, parameter, pluginName, pluginVersion, minimumEdition, maximumEdition) {
|
|
24
24
|
const { allFiles, filesToGenerate } = getFilesToGenerate(request, minimumEdition, maximumEdition);
|
|
25
25
|
let target;
|
|
26
26
|
const generatedFiles = [];
|
|
27
|
-
const runtime = createRuntimeImports(parameter.bootstrapWkt);
|
|
28
|
-
const resolveDescImport = (desc, typeOnly) => createImportSymbol(
|
|
29
|
-
const resolveShapeImport = (desc) => createImportSymbol(
|
|
30
|
-
const
|
|
31
|
-
const
|
|
27
|
+
const runtime = createRuntimeImports(parameter.parsed.bootstrapWkt);
|
|
28
|
+
const resolveDescImport = (desc, typeOnly) => createImportSymbol(generatedDescName(desc), generateFilePath(desc.kind == "file" ? desc : desc.file, parameter.parsed.bootstrapWkt, filesToGenerate), typeOnly);
|
|
29
|
+
const resolveShapeImport = (desc) => createImportSymbol(generatedShapeName(desc), generateFilePath(desc.file, parameter.parsed.bootstrapWkt, filesToGenerate), true);
|
|
30
|
+
const resolveJsonImport = (desc) => createImportSymbol(generatedJsonTypeName(desc), generateFilePath(desc.file, parameter.parsed.bootstrapWkt, filesToGenerate), true);
|
|
31
|
+
const createPreamble = (descFile) => makeFilePreamble(descFile, pluginName, pluginVersion, parameter.sanitized, parameter.parsed.tsNocheck);
|
|
32
|
+
const rewriteImport = (importPath) => rewriteImportPath(importPath, parameter.parsed.rewriteImports, parameter.parsed.importExtension);
|
|
32
33
|
return {
|
|
33
|
-
targets: parameter.targets,
|
|
34
|
+
targets: parameter.parsed.targets,
|
|
34
35
|
proto: request,
|
|
35
36
|
files: filesToGenerate,
|
|
36
37
|
allFiles: allFiles,
|
|
38
|
+
options: parameter.parsed,
|
|
37
39
|
typesInFile: nestedTypes,
|
|
38
40
|
generateFile(name) {
|
|
39
41
|
if (target === undefined) {
|
|
40
42
|
throw new Error("prepareGenerate() must be called before generateFile()");
|
|
41
43
|
}
|
|
42
|
-
const genFile = createGeneratedFile(name, deriveImportPath(name), target === "js" ? parameter.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
|
|
43
|
-
rewriteImport, resolveDescImport, resolveShapeImport, createPreamble, runtime);
|
|
44
|
+
const genFile = createGeneratedFile(name, deriveImportPath(name), target === "js" ? parameter.parsed.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
|
|
45
|
+
rewriteImport, resolveDescImport, resolveShapeImport, resolveJsonImport, createPreamble, runtime);
|
|
44
46
|
generatedFiles.push(genFile);
|
|
45
47
|
return genFile;
|
|
46
48
|
},
|
|
47
49
|
getFileInfo() {
|
|
48
50
|
return generatedFiles
|
|
49
51
|
.map((f) => f.getFileInfo())
|
|
50
|
-
.filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
|
|
52
|
+
.filter((fi) => parameter.parsed.keepEmptyFiles || fi.content.length > 0);
|
|
51
53
|
},
|
|
52
54
|
prepareGenerate(newTarget) {
|
|
53
55
|
target = newTarget;
|
|
@@ -96,7 +98,7 @@ function getFilesToGenerate(request, minimumEdition, maximumEdition) {
|
|
|
96
98
|
const sourceFile = request.sourceFileDescriptors.find((s) => s.name == protoFile.name);
|
|
97
99
|
return sourceFile !== null && sourceFile !== void 0 ? sourceFile : protoFile;
|
|
98
100
|
});
|
|
99
|
-
const registry = createFileRegistry(create(
|
|
101
|
+
const registry = createFileRegistry(create(FileDescriptorSetSchema, {
|
|
100
102
|
file: allProtoWithSourceOptions,
|
|
101
103
|
}));
|
|
102
104
|
const allFiles = [];
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { isFieldSet, ScalarType, } from "@bufbuild/protobuf";
|
|
15
15
|
import { protoCamelCase, reflect } from "@bufbuild/protobuf/reflect";
|
|
16
|
-
import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type,
|
|
16
|
+
import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoSchema, FieldOptions_JSType, FieldOptionsSchema, FeatureSetSchema, SourceCodeInfo_LocationSchema, FileDescriptorProtoSchema, DescriptorProtoSchema, EnumDescriptorProtoSchema, ServiceDescriptorProtoSchema, } from "@bufbuild/protobuf/wkt";
|
|
17
17
|
/**
|
|
18
18
|
* Get comments on the package element in the protobuf source.
|
|
19
19
|
*/
|
|
20
20
|
export function getPackageComments(desc) {
|
|
21
21
|
return findComments(desc.proto.sourceCodeInfo, [
|
|
22
|
-
|
|
22
|
+
FileDescriptorProtoSchema.field.package.number,
|
|
23
23
|
]);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -27,7 +27,7 @@ export function getPackageComments(desc) {
|
|
|
27
27
|
*/
|
|
28
28
|
export function getSyntaxComments(desc) {
|
|
29
29
|
return findComments(desc.proto.sourceCodeInfo, [
|
|
30
|
-
|
|
30
|
+
FileDescriptorProtoSchema.field.syntax.number,
|
|
31
31
|
]);
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
@@ -41,11 +41,11 @@ export function getComments(desc) {
|
|
|
41
41
|
path = desc.parent
|
|
42
42
|
? [
|
|
43
43
|
...getComments(desc.parent).sourcePath,
|
|
44
|
-
|
|
44
|
+
DescriptorProtoSchema.field.enumType.number,
|
|
45
45
|
desc.parent.proto.enumType.indexOf(desc.proto),
|
|
46
46
|
]
|
|
47
47
|
: [
|
|
48
|
-
|
|
48
|
+
FileDescriptorProtoSchema.field.enumType.number,
|
|
49
49
|
desc.file.proto.enumType.indexOf(desc.proto),
|
|
50
50
|
];
|
|
51
51
|
file = desc.file;
|
|
@@ -53,7 +53,7 @@ export function getComments(desc) {
|
|
|
53
53
|
case "oneof":
|
|
54
54
|
path = [
|
|
55
55
|
...getComments(desc.parent).sourcePath,
|
|
56
|
-
|
|
56
|
+
DescriptorProtoSchema.field.oneofDecl.number,
|
|
57
57
|
desc.parent.proto.oneofDecl.indexOf(desc.proto),
|
|
58
58
|
];
|
|
59
59
|
file = desc.parent.file;
|
|
@@ -62,11 +62,11 @@ export function getComments(desc) {
|
|
|
62
62
|
path = desc.parent
|
|
63
63
|
? [
|
|
64
64
|
...getComments(desc.parent).sourcePath,
|
|
65
|
-
|
|
65
|
+
DescriptorProtoSchema.field.nestedType.number,
|
|
66
66
|
desc.parent.proto.nestedType.indexOf(desc.proto),
|
|
67
67
|
]
|
|
68
68
|
: [
|
|
69
|
-
|
|
69
|
+
FileDescriptorProtoSchema.field.messageType.number,
|
|
70
70
|
desc.file.proto.messageType.indexOf(desc.proto),
|
|
71
71
|
];
|
|
72
72
|
file = desc.file;
|
|
@@ -74,7 +74,7 @@ export function getComments(desc) {
|
|
|
74
74
|
case "enum_value":
|
|
75
75
|
path = [
|
|
76
76
|
...getComments(desc.parent).sourcePath,
|
|
77
|
-
|
|
77
|
+
EnumDescriptorProtoSchema.field.value.number,
|
|
78
78
|
desc.parent.proto.value.indexOf(desc.proto),
|
|
79
79
|
];
|
|
80
80
|
file = desc.parent.file;
|
|
@@ -82,7 +82,7 @@ export function getComments(desc) {
|
|
|
82
82
|
case "field":
|
|
83
83
|
path = [
|
|
84
84
|
...getComments(desc.parent).sourcePath,
|
|
85
|
-
|
|
85
|
+
DescriptorProtoSchema.field.field.number,
|
|
86
86
|
desc.parent.proto.field.indexOf(desc.proto),
|
|
87
87
|
];
|
|
88
88
|
file = desc.parent.file;
|
|
@@ -91,18 +91,18 @@ export function getComments(desc) {
|
|
|
91
91
|
path = desc.parent
|
|
92
92
|
? [
|
|
93
93
|
...getComments(desc.parent).sourcePath,
|
|
94
|
-
|
|
94
|
+
DescriptorProtoSchema.field.extension.number,
|
|
95
95
|
desc.parent.proto.extension.indexOf(desc.proto),
|
|
96
96
|
]
|
|
97
97
|
: [
|
|
98
|
-
|
|
98
|
+
FileDescriptorProtoSchema.field.extension.number,
|
|
99
99
|
desc.file.proto.extension.indexOf(desc.proto),
|
|
100
100
|
];
|
|
101
101
|
file = desc.file;
|
|
102
102
|
break;
|
|
103
103
|
case "service":
|
|
104
104
|
path = [
|
|
105
|
-
|
|
105
|
+
FileDescriptorProtoSchema.field.service.number,
|
|
106
106
|
desc.file.proto.service.indexOf(desc.proto),
|
|
107
107
|
];
|
|
108
108
|
file = desc.file;
|
|
@@ -110,7 +110,7 @@ export function getComments(desc) {
|
|
|
110
110
|
case "rpc":
|
|
111
111
|
path = [
|
|
112
112
|
...getComments(desc.parent).sourcePath,
|
|
113
|
-
|
|
113
|
+
ServiceDescriptorProtoSchema.field.method.number,
|
|
114
114
|
desc.parent.proto.method.indexOf(desc.proto),
|
|
115
115
|
];
|
|
116
116
|
file = desc.parent.file;
|
|
@@ -127,7 +127,7 @@ export function getFeatureOptionStrings(desc) {
|
|
|
127
127
|
const strings = [];
|
|
128
128
|
const features = (_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.features;
|
|
129
129
|
if (features !== undefined) {
|
|
130
|
-
const r = reflect(
|
|
130
|
+
const r = reflect(FeatureSetSchema, features);
|
|
131
131
|
for (const f of r.fields) {
|
|
132
132
|
if (f.fieldKind != "enum" || !r.isSet(f)) {
|
|
133
133
|
continue;
|
|
@@ -190,10 +190,10 @@ export function getDeclarationString(desc) {
|
|
|
190
190
|
const options = [];
|
|
191
191
|
const protoOptions = desc.proto.options;
|
|
192
192
|
if (protoOptions !== undefined &&
|
|
193
|
-
isFieldSet(protoOptions,
|
|
193
|
+
isFieldSet(protoOptions, FieldOptionsSchema.field.packed)) {
|
|
194
194
|
options.push(`packed = ${protoOptions.packed.toString()}`);
|
|
195
195
|
}
|
|
196
|
-
if (isFieldSet(desc.proto,
|
|
196
|
+
if (isFieldSet(desc.proto, FieldDescriptorProtoSchema.field.defaultValue)) {
|
|
197
197
|
let defaultValue = desc.proto.defaultValue;
|
|
198
198
|
if (desc.proto.type == FieldDescriptorProto_Type.BYTES ||
|
|
199
199
|
desc.proto.type == FieldDescriptorProto_Type.STRING) {
|
|
@@ -205,11 +205,11 @@ export function getDeclarationString(desc) {
|
|
|
205
205
|
options.push(`json_name = "${desc.jsonName}"`);
|
|
206
206
|
}
|
|
207
207
|
if (protoOptions !== undefined &&
|
|
208
|
-
isFieldSet(protoOptions,
|
|
208
|
+
isFieldSet(protoOptions, FieldOptionsSchema.field.jstype)) {
|
|
209
209
|
options.push(`jstype = ${FieldOptions_JSType[protoOptions.jstype]}`);
|
|
210
210
|
}
|
|
211
211
|
if (protoOptions !== undefined &&
|
|
212
|
-
isFieldSet(protoOptions,
|
|
212
|
+
isFieldSet(protoOptions, FieldOptionsSchema.field.deprecated)) {
|
|
213
213
|
options.push(`deprecated = true`);
|
|
214
214
|
}
|
|
215
215
|
options.push(...getFeatureOptionStrings(desc));
|
|
@@ -262,10 +262,10 @@ function findComments(sourceCodeInfo, sourcePath) {
|
|
|
262
262
|
}
|
|
263
263
|
return {
|
|
264
264
|
leadingDetached: location.leadingDetachedComments,
|
|
265
|
-
leading: isFieldSet(location,
|
|
265
|
+
leading: isFieldSet(location, SourceCodeInfo_LocationSchema.field.leadingComments)
|
|
266
266
|
? location.leadingComments
|
|
267
267
|
: undefined,
|
|
268
|
-
trailing: isFieldSet(location,
|
|
268
|
+
trailing: isFieldSet(location, SourceCodeInfo_LocationSchema.field.trailingComments)
|
|
269
269
|
? location.trailingComments
|
|
270
270
|
: undefined,
|
|
271
271
|
sourcePath,
|
package/dist/esm/transpile.js
CHANGED
|
@@ -20,7 +20,7 @@ const defaultOptions = {
|
|
|
20
20
|
strict: false,
|
|
21
21
|
// modules
|
|
22
22
|
module: ts.ModuleKind.ES2020,
|
|
23
|
-
moduleResolution: ts.ModuleResolutionKind.
|
|
23
|
+
moduleResolution: ts.ModuleResolutionKind.Node10,
|
|
24
24
|
noResolve: true,
|
|
25
25
|
resolveJsonModule: false,
|
|
26
26
|
// emit
|
|
@@ -33,7 +33,7 @@ const defaultOptions = {
|
|
|
33
33
|
checkJs: false,
|
|
34
34
|
// Language and Environment
|
|
35
35
|
lib: [],
|
|
36
|
-
moduleDetection:
|
|
36
|
+
moduleDetection: ts.ModuleDetectionKind.Force,
|
|
37
37
|
target: ts.ScriptTarget.ES2017,
|
|
38
38
|
// Completeness
|
|
39
39
|
skipLibCheck: true,
|