@bufbuild/protoplugin 1.8.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.
Files changed (79) hide show
  1. package/dist/cjs/create-es-plugin.d.ts +8 -12
  2. package/dist/cjs/create-es-plugin.js +15 -13
  3. package/dist/cjs/ecmascript/file-preamble.js +8 -7
  4. package/dist/cjs/ecmascript/generated-file.d.ts +19 -20
  5. package/dist/cjs/ecmascript/generated-file.js +95 -108
  6. package/dist/cjs/ecmascript/import-path.d.ts +0 -6
  7. package/dist/cjs/ecmascript/import-path.js +1 -22
  8. package/dist/cjs/ecmascript/import-symbol.d.ts +1 -3
  9. package/dist/cjs/ecmascript/import-symbol.js +3 -5
  10. package/dist/cjs/ecmascript/index.d.ts +3 -16
  11. package/dist/cjs/ecmascript/index.js +3 -29
  12. package/dist/cjs/ecmascript/jsdoc.d.ts +2 -13
  13. package/dist/cjs/ecmascript/jsdoc.js +23 -31
  14. package/dist/cjs/ecmascript/names.d.ts +4 -0
  15. package/dist/cjs/ecmascript/names.js +119 -0
  16. package/dist/cjs/ecmascript/parameter.d.ts +3 -3
  17. package/dist/cjs/ecmascript/parameter.js +2 -2
  18. package/dist/cjs/ecmascript/printable.d.ts +36 -0
  19. package/dist/cjs/ecmascript/runtime-imports.d.ts +12 -23
  20. package/dist/cjs/ecmascript/runtime-imports.js +28 -29
  21. package/dist/cjs/ecmascript/safe-identifier.d.ts +6 -0
  22. package/dist/cjs/ecmascript/safe-identifier.js +90 -0
  23. package/dist/cjs/ecmascript/schema.d.ts +10 -9
  24. package/dist/cjs/ecmascript/schema.js +64 -20
  25. package/dist/cjs/ecmascript/transpile.js +1 -1
  26. package/dist/cjs/index.d.ts +2 -6
  27. package/dist/cjs/index.js +6 -1
  28. package/dist/cjs/plugin.d.ts +1 -1
  29. package/dist/cjs/run-node.js +4 -3
  30. package/dist/cjs/source-code-info.d.ts +18 -0
  31. package/dist/cjs/source-code-info.js +259 -0
  32. package/dist/esm/create-es-plugin.d.ts +8 -12
  33. package/dist/esm/create-es-plugin.js +15 -13
  34. package/dist/esm/ecmascript/file-preamble.js +5 -4
  35. package/dist/esm/ecmascript/generated-file.d.ts +19 -20
  36. package/dist/esm/ecmascript/generated-file.js +97 -110
  37. package/dist/esm/ecmascript/import-path.d.ts +0 -6
  38. package/dist/esm/ecmascript/import-path.js +0 -20
  39. package/dist/esm/ecmascript/import-symbol.d.ts +1 -3
  40. package/dist/esm/ecmascript/import-symbol.js +3 -5
  41. package/dist/esm/ecmascript/index.d.ts +3 -16
  42. package/dist/esm/ecmascript/index.js +1 -18
  43. package/dist/esm/ecmascript/jsdoc.d.ts +2 -13
  44. package/dist/esm/ecmascript/jsdoc.js +20 -29
  45. package/dist/esm/ecmascript/names.d.ts +4 -0
  46. package/dist/esm/ecmascript/names.js +113 -0
  47. package/dist/esm/ecmascript/parameter.d.ts +3 -3
  48. package/dist/esm/ecmascript/parameter.js +2 -2
  49. package/dist/esm/ecmascript/printable.d.ts +36 -0
  50. package/dist/esm/ecmascript/{opaque-printables.js → printable.js} +1 -1
  51. package/dist/esm/ecmascript/runtime-imports.d.ts +12 -23
  52. package/dist/esm/ecmascript/runtime-imports.js +28 -29
  53. package/dist/esm/ecmascript/safe-identifier.d.ts +6 -0
  54. package/dist/esm/ecmascript/safe-identifier.js +86 -0
  55. package/dist/esm/ecmascript/schema.d.ts +10 -9
  56. package/dist/esm/ecmascript/schema.js +66 -22
  57. package/dist/esm/ecmascript/transpile.js +1 -1
  58. package/dist/esm/index.d.ts +2 -6
  59. package/dist/esm/index.js +1 -0
  60. package/dist/esm/plugin.d.ts +1 -1
  61. package/dist/esm/run-node.js +4 -3
  62. package/dist/esm/source-code-info.d.ts +18 -0
  63. package/dist/esm/source-code-info.js +252 -0
  64. package/package.json +4 -4
  65. package/dist/cjs/ecmascript/legacy-custom-options.d.ts +0 -40
  66. package/dist/cjs/ecmascript/legacy-custom-options.js +0 -121
  67. package/dist/cjs/ecmascript/legacy-gencommon.d.ts +0 -26
  68. package/dist/cjs/ecmascript/legacy-gencommon.js +0 -237
  69. package/dist/cjs/ecmascript/opaque-printables.d.ts +0 -26
  70. package/dist/cjs/ecmascript/reify-wkt.d.ts +0 -100
  71. package/dist/cjs/ecmascript/reify-wkt.js +0 -170
  72. package/dist/esm/ecmascript/legacy-custom-options.d.ts +0 -40
  73. package/dist/esm/ecmascript/legacy-custom-options.js +0 -115
  74. package/dist/esm/ecmascript/legacy-gencommon.d.ts +0 -26
  75. package/dist/esm/ecmascript/legacy-gencommon.js +0 -230
  76. package/dist/esm/ecmascript/opaque-printables.d.ts +0 -26
  77. package/dist/esm/ecmascript/reify-wkt.d.ts +0 -100
  78. package/dist/esm/ecmascript/reify-wkt.js +0 -166
  79. /package/dist/cjs/ecmascript/{opaque-printables.js → printable.js} +0 -0
@@ -13,34 +13,8 @@
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.createJsDocBlock = exports.makeJsDoc = exports.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.literalString = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.localName = exports.createImportSymbol = exports.reifyWkt = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
- const jsdoc_js_1 = require("./jsdoc.js");
19
- var reify_wkt_js_1 = require("./reify-wkt.js");
20
- Object.defineProperty(exports, "reifyWkt", { enumerable: true, get: function () { return reify_wkt_js_1.reifyWkt; } });
16
+ exports.safeIdentifier = exports.createImportSymbol = void 0;
21
17
  var import_symbol_js_1 = require("./import-symbol.js");
22
18
  Object.defineProperty(exports, "createImportSymbol", { enumerable: true, get: function () { return import_symbol_js_1.createImportSymbol; } });
23
- exports.localName = protobuf_1.codegenInfo.localName;
24
- var legacy_gencommon_js_1 = require("./legacy-gencommon.js");
25
- Object.defineProperty(exports, "getFieldExplicitDefaultValue", { enumerable: true, get: function () { return legacy_gencommon_js_1.getFieldExplicitDefaultValue; } });
26
- Object.defineProperty(exports, "getFieldIntrinsicDefaultValue", { enumerable: true, get: function () { return legacy_gencommon_js_1.getFieldIntrinsicDefaultValue; } });
27
- Object.defineProperty(exports, "getFieldTyping", { enumerable: true, get: function () { return legacy_gencommon_js_1.getFieldTyping; } });
28
- Object.defineProperty(exports, "literalString", { enumerable: true, get: function () { return legacy_gencommon_js_1.literalString; } });
29
- var legacy_custom_options_js_1 = require("./legacy-custom-options.js");
30
- Object.defineProperty(exports, "findCustomScalarOption", { enumerable: true, get: function () { return legacy_custom_options_js_1.findCustomScalarOption; } });
31
- Object.defineProperty(exports, "findCustomMessageOption", { enumerable: true, get: function () { return legacy_custom_options_js_1.findCustomMessageOption; } });
32
- Object.defineProperty(exports, "findCustomEnumOption", { enumerable: true, get: function () { return legacy_custom_options_js_1.findCustomEnumOption; } });
33
- /**
34
- * @deprecated Please use GeneratedFile.jsDoc() instead
35
- */
36
- function makeJsDoc(desc, indentation = "") {
37
- return (0, jsdoc_js_1.createJsDocBlock)(desc, indentation).toString();
38
- }
39
- exports.makeJsDoc = makeJsDoc;
40
- /**
41
- * @deprecated Please use GeneratedFile.jsDoc() instead
42
- */
43
- function createJsDocBlock(text, indentation = "") {
44
- return (0, jsdoc_js_1.createJsDocBlock)(text, indentation).toString();
45
- }
46
- exports.createJsDocBlock = createJsDocBlock;
19
+ var safe_identifier_js_1 = require("./safe-identifier.js");
20
+ Object.defineProperty(exports, "safeIdentifier", { enumerable: true, get: function () { return safe_identifier_js_1.safeIdentifier; } });
@@ -1,14 +1,3 @@
1
1
  import type { AnyDesc, DescFile } from "@bufbuild/protobuf";
2
- export type JSDocBlock = {
3
- readonly kind: "es_jsdoc";
4
- /**
5
- * @deprecated In a future release, we will make this property optional.
6
- */
7
- text: string;
8
- indentation?: string;
9
- /**
10
- * @deprecated In a future release, we will remove this method.
11
- */
12
- toString(): string;
13
- };
14
- export declare function createJsDocBlock(textOrDesc: string | Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
2
+ export declare function createJsDocTextFromDesc(desc: Exclude<AnyDesc, DescFile>): string;
3
+ export declare function formatJsDocBlock(text: string, indentation: string | undefined): string;
@@ -13,33 +13,10 @@
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.createJsDocBlock = void 0;
17
- // TODO simplify type JSDocBlock to bring it in line with others in opaque-printables.ts
18
- function createJsDocBlock(textOrDesc, indentation) {
19
- const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
20
- return {
21
- kind: "es_jsdoc",
22
- text,
23
- indentation,
24
- toString() {
25
- if (text.trim().length == 0) {
26
- return "";
27
- }
28
- let lines = text.split("\n");
29
- if (lines.length === 0) {
30
- return "";
31
- }
32
- lines = lines.map((l) => l.split("*/").join("*\\/"));
33
- lines = lines.map((l) => (l.length > 0 ? " " + l : l));
34
- const i = indentation !== null && indentation !== void 0 ? indentation : "";
35
- return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
36
- },
37
- };
38
- }
39
- exports.createJsDocBlock = createJsDocBlock;
40
- function createTextForDesc(desc) {
41
- var _a, _b;
42
- const comments = desc.getComments();
16
+ exports.formatJsDocBlock = exports.createJsDocTextFromDesc = void 0;
17
+ const source_code_info_js_1 = require("../source-code-info.js");
18
+ function createJsDocTextFromDesc(desc) {
19
+ const comments = (0, source_code_info_js_1.getComments)(desc);
43
20
  let text = "";
44
21
  if (comments.leading !== undefined) {
45
22
  text += comments.leading;
@@ -65,13 +42,13 @@ function createTextForDesc(desc) {
65
42
  .join("\n");
66
43
  switch (desc.kind) {
67
44
  case "enum_value":
68
- text += `@generated from enum value: ${desc.declarationString()};`;
45
+ text += `@generated from enum value: ${(0, source_code_info_js_1.getDeclarationString)(desc)};`;
69
46
  break;
70
47
  case "field":
71
- text += `@generated from field: ${desc.declarationString()};`;
48
+ text += `@generated from field: ${(0, source_code_info_js_1.getDeclarationString)(desc)};`;
72
49
  break;
73
50
  case "extension":
74
- text += `@generated from extension: ${desc.declarationString()};`;
51
+ text += `@generated from extension: ${(0, source_code_info_js_1.getDeclarationString)(desc)};`;
75
52
  break;
76
53
  default:
77
54
  text += `@generated from ${desc.toString()}`;
@@ -82,7 +59,7 @@ function createTextForDesc(desc) {
82
59
  case "enum":
83
60
  case "message":
84
61
  case "service":
85
- deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
62
+ deprecated = deprecated || desc.file.deprecated;
86
63
  break;
87
64
  default:
88
65
  break;
@@ -92,3 +69,18 @@ function createTextForDesc(desc) {
92
69
  }
93
70
  return text;
94
71
  }
72
+ exports.createJsDocTextFromDesc = createJsDocTextFromDesc;
73
+ function formatJsDocBlock(text, indentation) {
74
+ if (text.trim().length == 0) {
75
+ return "";
76
+ }
77
+ let lines = text.split("\n");
78
+ if (lines.length === 0) {
79
+ return "";
80
+ }
81
+ lines = lines.map((l) => l.split("*/").join("*\\/"));
82
+ lines = lines.map((l) => (l.length > 0 ? " " + l : l));
83
+ const i = indentation !== null && indentation !== void 0 ? indentation : "";
84
+ return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
85
+ }
86
+ exports.formatJsDocBlock = formatJsDocBlock;
@@ -0,0 +1,4 @@
1
+ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
2
+ export declare function generateFilePath(file: DescFile, bootstrapWkt: boolean, filesToGenerate: DescFile[]): string;
3
+ export declare function localDescName(desc: DescFile | DescEnum | DescMessage | DescExtension | DescService): string;
4
+ export declare function localShapeName(desc: DescEnum | DescMessage): string;
@@ -0,0 +1,119 @@
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.localShapeName = exports.localDescName = exports.generateFilePath = void 0;
17
+ const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
18
+ const reflect_1 = require("@bufbuild/protobuf/reflect");
19
+ const safe_identifier_js_1 = require("./safe-identifier.js");
20
+ function generateFilePath(file, bootstrapWkt, filesToGenerate) {
21
+ // Well-known types are published with the runtime package. We usually want
22
+ // the generated code to import them from the runtime package, with the
23
+ // following exceptions:
24
+ // 1. We are bootstrapping the runtime package via the plugin option
25
+ // "bootstrap_wkt". In that case, we cannot refer to the runtime package
26
+ // itself.
27
+ // 2. We were explicitly asked to generate the well-known type.
28
+ const wktFrom = codegenv1_1.wktPublicImportPaths[file.proto.name];
29
+ if (wktFrom !== undefined &&
30
+ !bootstrapWkt &&
31
+ !filesToGenerate.find((f) => f.name === file.name)) {
32
+ return wktFrom;
33
+ }
34
+ return "./" + file.name + "_pb.js";
35
+ }
36
+ exports.generateFilePath = generateFilePath;
37
+ function localDescName(desc) {
38
+ const file = desc.kind == "file" ? desc : desc.file;
39
+ const { descNames } = allNames(file);
40
+ const name = descNames.get(desc);
41
+ if (name === undefined) {
42
+ throw new Error(`unable to determine unique identifier for ${desc.toString()}`);
43
+ }
44
+ return name;
45
+ }
46
+ exports.localDescName = localDescName;
47
+ function localShapeName(desc) {
48
+ const { shapeNames } = allNames(desc.file);
49
+ const name = shapeNames.get(desc);
50
+ if (name === undefined) {
51
+ throw new Error(`unable to determine unique identifier for ${desc.toString()}`);
52
+ }
53
+ return name;
54
+ }
55
+ exports.localShapeName = localShapeName;
56
+ function idealDescName(desc, i) {
57
+ const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
58
+ switch (desc.kind) {
59
+ case "file":
60
+ return ((0, safe_identifier_js_1.safeIdentifier)("fileDesc_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_")) + escape);
61
+ case "enum":
62
+ return baseName(desc) + "Desc" + escape;
63
+ case "message":
64
+ return baseName(desc) + "Desc" + escape;
65
+ case "extension":
66
+ return baseName(desc) + escape;
67
+ case "service":
68
+ return baseName(desc) + escape;
69
+ }
70
+ }
71
+ function idealShapeName(desc, i) {
72
+ const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
73
+ return baseName(desc) + escape;
74
+ }
75
+ function baseName(desc) {
76
+ const pkg = desc.file.proto.package;
77
+ const offset = pkg.length > 0 ? pkg.length + 1 : 0;
78
+ const name = desc.typeName.substring(offset).replace(/\./g, "_");
79
+ return (0, safe_identifier_js_1.safeIdentifier)(name);
80
+ }
81
+ function allNames(file) {
82
+ const taken = new Set();
83
+ const shapeNames = new Map();
84
+ const descNames = new Map();
85
+ for (const desc of [file, ...(0, reflect_1.nestedTypes)(file)]) {
86
+ switch (desc.kind) {
87
+ case "enum":
88
+ case "message": {
89
+ let descName;
90
+ let shapeName;
91
+ for (let i = 0;; i++) {
92
+ descName = idealDescName(desc, i);
93
+ shapeName = idealShapeName(desc, i);
94
+ if (!taken.has(descName) && !taken.has(shapeName)) {
95
+ break;
96
+ }
97
+ }
98
+ taken.add(descName);
99
+ taken.add(shapeName);
100
+ descNames.set(desc, descName);
101
+ shapeNames.set(desc, shapeName);
102
+ break;
103
+ }
104
+ default: {
105
+ let descName;
106
+ for (let i = 0;; i++) {
107
+ descName = idealDescName(desc, i);
108
+ if (!taken.has(descName)) {
109
+ break;
110
+ }
111
+ }
112
+ taken.add(descName);
113
+ descNames.set(desc, descName);
114
+ break;
115
+ }
116
+ }
117
+ }
118
+ return { shapeNames, descNames };
119
+ }
@@ -1,5 +1,5 @@
1
- import { Target } from "./target.js";
2
- import { RewriteImports } from "./import-path.js";
1
+ import type { Target } from "./target.js";
2
+ import type { RewriteImports } from "./import-path.js";
3
3
  export interface ParsedParameter {
4
4
  targets: Target[];
5
5
  tsNocheck: boolean;
@@ -10,4 +10,4 @@ export interface ParsedParameter {
10
10
  jsImportStyle: "module" | "legacy_commonjs";
11
11
  sanitizedParameter: string;
12
12
  }
13
- export declare function parseParameter(parameter: string | undefined, parseExtraOption: ((key: string, value: string) => void) | undefined): ParsedParameter;
13
+ export declare function parseParameter(parameter: string, parseExtraOption: ((key: string, value: string) => void) | undefined): ParsedParameter;
@@ -17,7 +17,7 @@ exports.parseParameter = void 0;
17
17
  const error_js_1 = require("../error.js");
18
18
  function parseParameter(parameter, parseExtraOption) {
19
19
  let targets = ["js", "dts"];
20
- let tsNocheck = true;
20
+ let tsNocheck = false;
21
21
  let bootstrapWkt = false;
22
22
  let keepEmptyFiles = false;
23
23
  const rewriteImports = [];
@@ -147,7 +147,7 @@ function parseParameter(parameter, parseExtraOption) {
147
147
  }
148
148
  exports.parseParameter = parseParameter;
149
149
  function splitParameter(parameter) {
150
- if (parameter == undefined) {
150
+ if (parameter.length == 0) {
151
151
  return [];
152
152
  }
153
153
  return parameter.split(",").map((raw) => {
@@ -0,0 +1,36 @@
1
+ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
2
+ import { ScalarType, LongType } from "@bufbuild/protobuf/reflect";
3
+ import type { ImportSymbol } from "./import-symbol.js";
4
+ /**
5
+ * All types that can be passed to GeneratedFile.print()
6
+ */
7
+ export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportStatement | JSDocBlock | LiteralString | LiteralProtoInt64 | DescImport | ShapeImport | Printable[];
8
+ export type ExportStatement = {
9
+ kind: "es_export_stmt";
10
+ name: string;
11
+ declaration?: string;
12
+ };
13
+ export type LiteralProtoInt64 = {
14
+ readonly kind: "es_proto_int64";
15
+ type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
16
+ longType: LongType;
17
+ value: bigint | string;
18
+ };
19
+ export type LiteralString = {
20
+ readonly kind: "es_string";
21
+ value: string;
22
+ };
23
+ export type DescImport = {
24
+ readonly kind: "es_desc_ref";
25
+ desc: DescMessage | DescEnum | DescExtension | DescService | DescFile;
26
+ typeOnly?: boolean;
27
+ };
28
+ export type ShapeImport = {
29
+ readonly kind: "es_shape_ref";
30
+ desc: DescMessage | DescEnum;
31
+ };
32
+ export type JSDocBlock = {
33
+ readonly kind: "es_jsdoc";
34
+ text: string;
35
+ indentation?: string;
36
+ };
@@ -1,25 +1,14 @@
1
1
  import type { ImportSymbol } from "./import-symbol.js";
2
- export interface RuntimeImports {
3
- proto2: ImportSymbol;
4
- proto3: ImportSymbol;
5
- Message: ImportSymbol;
6
- PartialMessage: ImportSymbol;
7
- PlainMessage: ImportSymbol;
8
- FieldList: ImportSymbol;
9
- MessageType: ImportSymbol;
10
- Extension: ImportSymbol;
11
- BinaryReadOptions: ImportSymbol;
12
- BinaryWriteOptions: ImportSymbol;
13
- JsonReadOptions: ImportSymbol;
14
- JsonWriteOptions: ImportSymbol;
15
- JsonValue: ImportSymbol;
16
- JsonObject: ImportSymbol;
17
- protoDouble: ImportSymbol;
18
- protoInt64: ImportSymbol;
19
- ScalarType: ImportSymbol;
20
- LongType: ImportSymbol;
21
- MethodKind: ImportSymbol;
22
- MethodIdempotency: ImportSymbol;
23
- IMessageTypeRegistry: ImportSymbol;
24
- }
2
+ import { symbols } from "@bufbuild/protobuf/codegenv1";
3
+ export type RuntimeImports = mapRecord<typeof symbols>;
25
4
  export declare function createRuntimeImports(bootstrapWkt: boolean): RuntimeImports;
5
+ type mapRecord<T extends Record<string, unknown>> = {
6
+ [P in keyof T]: T[P] extends symbolInfo ? ImportSymbol : T[P] extends Record<string, unknown> ? mapRecord<T[P]> : never;
7
+ };
8
+ declare function mapRecord<T extends Record<string, unknown>>(record: T, bootstrapWkt: boolean): mapRecord<T>;
9
+ type symbolInfo = {
10
+ readonly typeOnly: boolean;
11
+ readonly from: string;
12
+ readonly bootstrapWktFrom: string;
13
+ };
14
+ export {};
@@ -15,36 +15,35 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.createRuntimeImports = void 0;
17
17
  const import_symbol_js_1 = require("./import-symbol.js");
18
- const protobuf_1 = require("@bufbuild/protobuf");
18
+ const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
19
19
  function createRuntimeImports(bootstrapWkt) {
20
- // prettier-ignore
21
- return {
22
- proto2: infoToSymbol("proto2", bootstrapWkt),
23
- proto3: infoToSymbol("proto3", bootstrapWkt),
24
- Message: infoToSymbol("Message", bootstrapWkt),
25
- PartialMessage: infoToSymbol("PartialMessage", bootstrapWkt),
26
- PlainMessage: infoToSymbol("PlainMessage", bootstrapWkt),
27
- FieldList: infoToSymbol("FieldList", bootstrapWkt),
28
- MessageType: infoToSymbol("MessageType", bootstrapWkt),
29
- Extension: infoToSymbol("Extension", bootstrapWkt),
30
- BinaryReadOptions: infoToSymbol("BinaryReadOptions", bootstrapWkt),
31
- BinaryWriteOptions: infoToSymbol("BinaryWriteOptions", bootstrapWkt),
32
- JsonReadOptions: infoToSymbol("JsonReadOptions", bootstrapWkt),
33
- JsonWriteOptions: infoToSymbol("JsonWriteOptions", bootstrapWkt),
34
- JsonValue: infoToSymbol("JsonValue", bootstrapWkt),
35
- JsonObject: infoToSymbol("JsonObject", bootstrapWkt),
36
- protoDouble: infoToSymbol("protoDouble", bootstrapWkt),
37
- protoInt64: infoToSymbol("protoInt64", bootstrapWkt),
38
- ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
39
- LongType: infoToSymbol("LongType", bootstrapWkt),
40
- MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
41
- MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
42
- IMessageTypeRegistry: infoToSymbol("IMessageTypeRegistry", bootstrapWkt),
43
- };
20
+ return mapRecord(codegenv1_1.symbols, bootstrapWkt);
44
21
  }
45
22
  exports.createRuntimeImports = createRuntimeImports;
46
- function infoToSymbol(name, bootstrapWkt) {
47
- const info = protobuf_1.codegenInfo.symbols[name];
48
- const symbol = (0, import_symbol_js_1.createImportSymbol)(name, bootstrapWkt ? info.privateImportPath : info.publicImportPath);
49
- return info.typeOnly ? symbol.toTypeOnly() : symbol;
23
+ function mapRecord(record, bootstrapWkt) {
24
+ const result = Object.create(null);
25
+ for (const [key, value] of Object.entries(record)) {
26
+ if (isSymbolInfo(value)) {
27
+ result[key] = (0, import_symbol_js_1.createImportSymbol)(key, bootstrapWkt ? value.bootstrapWktFrom : value.from, value.typeOnly);
28
+ }
29
+ else {
30
+ result[key] = mapRecord(record[key], bootstrapWkt);
31
+ }
32
+ }
33
+ return result;
34
+ }
35
+ function isSymbolInfo(arg) {
36
+ if (typeof arg != "object" || arg === null) {
37
+ return false;
38
+ }
39
+ const wantNames = [
40
+ "typeOnly",
41
+ "from",
42
+ "bootstrapWktFrom",
43
+ ];
44
+ const gotNames = Object.getOwnPropertyNames(arg);
45
+ if (gotNames.length !== wantNames.length) {
46
+ return false;
47
+ }
48
+ return wantNames.every((w) => gotNames.includes(w));
50
49
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
3
+ *
4
+ * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
5
+ */
6
+ export declare function safeIdentifier(name: string): string;
@@ -0,0 +1,90 @@
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.safeIdentifier = void 0;
17
+ /**
18
+ * Names that cannot be used for identifiers, such as class names,
19
+ * but _can_ be used for object properties.
20
+ */
21
+ const reservedIdentifiers = new Set([
22
+ // ECMAScript 2015 keywords
23
+ "break",
24
+ "case",
25
+ "catch",
26
+ "class",
27
+ "const",
28
+ "continue",
29
+ "debugger",
30
+ "default",
31
+ "delete",
32
+ "do",
33
+ "else",
34
+ "export",
35
+ "extends",
36
+ "false",
37
+ "finally",
38
+ "for",
39
+ "function",
40
+ "if",
41
+ "import",
42
+ "in",
43
+ "instanceof",
44
+ "new",
45
+ "null",
46
+ "return",
47
+ "super",
48
+ "switch",
49
+ "this",
50
+ "throw",
51
+ "true",
52
+ "try",
53
+ "typeof",
54
+ "var",
55
+ "void",
56
+ "while",
57
+ "with",
58
+ "yield",
59
+ // ECMAScript 2015 future reserved keywords
60
+ "enum",
61
+ "implements",
62
+ "interface",
63
+ "let",
64
+ "package",
65
+ "private",
66
+ "protected",
67
+ "public",
68
+ "static",
69
+ // Class name cannot be 'Object' when targeting ES5 with module CommonJS
70
+ "Object",
71
+ // TypeScript keywords that cannot be used for types (as opposed to variables)
72
+ "bigint",
73
+ "number",
74
+ "boolean",
75
+ "string",
76
+ "object",
77
+ // Identifiers reserved for the runtime, so we can generate legible code
78
+ "globalThis",
79
+ "Uint8Array",
80
+ "Partial",
81
+ ]);
82
+ /**
83
+ * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
84
+ *
85
+ * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
86
+ */
87
+ function safeIdentifier(name) {
88
+ return reservedIdentifiers.has(name) ? name + "$" : name;
89
+ }
90
+ exports.safeIdentifier = safeIdentifier;
@@ -1,9 +1,9 @@
1
- import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
2
- import { FeatureSetDefaults } from "@bufbuild/protobuf";
1
+ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
2
+ import type { CodeGeneratorRequest } from "@bufbuild/protobuf/wkt";
3
+ import { Edition } from "@bufbuild/protobuf/wkt";
3
4
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
4
- import { RuntimeImports } from "./runtime-imports.js";
5
5
  import type { Target } from "./target.js";
6
- import { ParsedParameter } from "./parameter.js";
6
+ import type { ParsedParameter } from "./parameter.js";
7
7
  /**
8
8
  * Schema describes the files and types that the plugin is requested to
9
9
  * generate.
@@ -21,14 +21,15 @@ 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
- * Provides some symbols from the runtime library @bufbuild/protobuf.
26
- */
27
- readonly runtime: RuntimeImports;
28
24
  /**
29
25
  * Generate a new file with the given name.
30
26
  */
31
27
  generateFile(name: string): GeneratedFile;
28
+ /**
29
+ * List all types in a file (including messages, enumerations, and extensions
30
+ * nested in messages).
31
+ */
32
+ typesInFile(file: DescFile): Iterable<DescMessage | DescEnum | DescExtension | DescService>;
32
33
  /**
33
34
  * The original google.protobuf.compiler.CodeGeneratorRequest.
34
35
  */
@@ -38,5 +39,5 @@ interface SchemaController extends Schema {
38
39
  getFileInfo: () => FileInfo[];
39
40
  prepareGenerate(target: Target): void;
40
41
  }
41
- export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter, pluginName: string, pluginVersion: string, featureSetDefaults: FeatureSetDefaults | undefined): SchemaController;
42
+ export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter, pluginName: string, pluginVersion: string, minimumEdition: Edition, maximumEdition: Edition): SchemaController;
42
43
  export {};