@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
@@ -1,7 +1,7 @@
1
- import { Schema } from "./ecmascript/schema.js";
1
+ import type { SupportedEdition } from "@bufbuild/protobuf";
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
- * Whether the plugin supports editions.
20
+ * The earliest edition supported by this plugin. Defaults to the minimum
21
+ * edition supported by @bufbuild/protobuf.
21
22
  */
22
- supportsEditions?: boolean;
23
+ minimumEdition?: SupportedEdition;
23
24
  /**
24
- * By default, plugins support all editions supported by createDescriptorSet()
25
- * from @bufbuild/protobuf, if supportsEditions is enabled.
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
- featureSetDefaults?: FeatureSetDefaults;
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.
@@ -14,10 +14,12 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.createEcmaScriptPlugin = 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");
17
20
  const schema_js_1 = require("./ecmascript/schema.js");
18
21
  const transpile_js_1 = require("./ecmascript/transpile.js");
19
22
  const parameter_js_1 = require("./ecmascript/parameter.js");
20
- const protobuf_1 = require("@bufbuild/protobuf");
21
23
  /**
22
24
  * Create a new code generator plugin for ECMAScript.
23
25
  * The plugin can generate JavaScript, TypeScript, or TypeScript declaration
@@ -30,9 +32,11 @@ function createEcmaScriptPlugin(init) {
30
32
  name: init.name,
31
33
  version: init.version,
32
34
  run(req) {
33
- var _a, _b;
35
+ var _a, _b, _c;
36
+ const minimumEdition = (_a = init.minimumEdition) !== null && _a !== void 0 ? _a : reflect_1.minimumEdition;
37
+ const maximumEdition = (_b = init.maximumEdition) !== null && _b !== void 0 ? _b : reflect_1.maximumEdition;
34
38
  const parameter = (0, parameter_js_1.parseParameter)(req.parameter, init.parseOption);
35
- const schema = (0, schema_js_1.createSchema)(req, parameter, init.name, init.version, init.featureSetDefaults);
39
+ const schema = (0, schema_js_1.createSchema)(req, parameter, init.name, init.version, minimumEdition, maximumEdition);
36
40
  const targetTs = schema.targets.includes("ts");
37
41
  const targetJs = schema.targets.includes("js");
38
42
  const targetDts = schema.targets.includes("dts");
@@ -91,24 +95,22 @@ function createEcmaScriptPlugin(init) {
91
95
  // but no generate function was provided. This also means that we will
92
96
  // have generated .ts files above.
93
97
  if (transpileJs || transpileDts) {
94
- const transpileFn = (_a = init.transpile) !== null && _a !== void 0 ? _a : transpile_js_1.transpile;
98
+ const transpileFn = (_c = init.transpile) !== null && _c !== void 0 ? _c : transpile_js_1.transpile;
95
99
  // Transpile the TypeScript files and add to the master list of files
96
100
  const transpiledFiles = transpileFn(tsFiles, transpileJs, transpileDts, parameter.jsImportStyle);
97
101
  files.push(...transpiledFiles);
98
102
  }
99
- return toResponse(files, (_b = init.supportsEditions) !== null && _b !== void 0 ? _b : false);
103
+ return toResponse(files, minimumEdition, maximumEdition);
100
104
  },
101
105
  };
102
106
  }
103
107
  exports.createEcmaScriptPlugin = createEcmaScriptPlugin;
104
- function toResponse(files, supportsEditions) {
105
- let supportedFeatures = protobuf_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL;
106
- if (supportsEditions) {
107
- supportedFeatures =
108
- supportedFeatures | protobuf_1.CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS;
109
- }
110
- return new protobuf_1.CodeGeneratorResponse({
111
- supportedFeatures: protobuf_1.protoInt64.parse(supportedFeatures),
108
+ function toResponse(files, minimumEdition, maximumEdition) {
109
+ return (0, protobuf_1.create)(wkt_1.CodeGeneratorResponseDesc, {
110
+ supportedFeatures: protobuf_1.protoInt64.parse(wkt_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL |
111
+ wkt_1.CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS),
112
+ minimumEdition,
113
+ maximumEdition,
112
114
  file: files.map((f) => {
113
115
  if (f.preamble !== undefined) {
114
116
  f.content = f.preamble + "\n" + f.content;
@@ -14,7 +14,8 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.makeFilePreamble = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
17
+ const wkt_1 = require("@bufbuild/protobuf/wkt");
18
+ const source_code_info_js_1 = require("../source-code-info.js");
18
19
  function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
19
20
  const builder = [];
20
21
  const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
@@ -34,25 +35,25 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
34
35
  builder.push("\n");
35
36
  }
36
37
  };
37
- writeLeadingComments(file.getSyntaxComments());
38
+ writeLeadingComments((0, source_code_info_js_1.getSyntaxComments)(file));
38
39
  builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
39
40
  if (parameter !== "") {
40
41
  builder.push(` with parameter "${parameter}"`);
41
42
  }
42
43
  builder.push("\n");
43
44
  builder.push(`// @generated from file ${file.name}.proto (`);
44
- if (file.proto.package !== undefined) {
45
+ if (file.proto.package.length > 0) {
45
46
  builder.push(`package ${file.proto.package}, `);
46
47
  }
47
48
  switch (file.edition) {
48
- case protobuf_1.Edition.EDITION_PROTO2:
49
+ case wkt_1.Edition.EDITION_PROTO2:
49
50
  builder.push(`syntax proto2)\n`);
50
51
  break;
51
- case protobuf_1.Edition.EDITION_PROTO3:
52
+ case wkt_1.Edition.EDITION_PROTO3:
52
53
  builder.push(`syntax proto3)\n`);
53
54
  break;
54
55
  default: {
55
- const editionString = protobuf_1.Edition[file.edition];
56
+ const editionString = wkt_1.Edition[file.edition];
56
57
  if (typeof editionString == "string") {
57
58
  const e = editionString.replace("EDITION_", "").toLowerCase();
58
59
  builder.push(`edition ${e})\n`);
@@ -68,7 +69,7 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
68
69
  builder.push("// @ts-nocheck\n");
69
70
  }
70
71
  builder.push("\n");
71
- writeLeadingComments(file.getPackageComments());
72
+ writeLeadingComments((0, source_code_info_js_1.getPackageComments)(file));
72
73
  return trimSuffix(builder.join(""), "\n");
73
74
  }
74
75
  exports.makeFilePreamble = makeFilePreamble;
@@ -1,12 +1,7 @@
1
- import { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage } from "@bufbuild/protobuf";
1
+ import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
2
2
  import type { ImportSymbol } from "./import-symbol.js";
3
+ import type { Printable } from "./printable.js";
3
4
  import type { RuntimeImports } from "./runtime-imports.js";
4
- import type { JSDocBlock } from "./jsdoc.js";
5
- import { ExportDeclaration, LiteralProtoInt64, LiteralString, RefDescEnum, RefDescMessage } from "./opaque-printables.js";
6
- /**
7
- * All types that can be passed to GeneratedFile.print()
8
- */
9
- export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | LiteralString | LiteralProtoInt64 | RefDescMessage | RefDescEnum | DescMessage | DescEnum | DescExtension | Printable[];
10
5
  /**
11
6
  * FileInfo represents an intermediate type using for transpiling TypeScript internally.
12
7
  */
@@ -49,10 +44,6 @@ export interface GeneratedFile {
49
44
  * See print(Printable[]) for behavior when printing Printable items.
50
45
  */
51
46
  print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
52
- /**
53
- * @deprecated Please use createImportSymbol() from @bufbuild/protoplugin/ecmascript instead
54
- */
55
- export(name: string): ImportSymbol;
56
47
  /**
57
48
  * Create a string literal.
58
49
  */
@@ -61,13 +52,13 @@ export interface GeneratedFile {
61
52
  * Create a JSDoc comment block with the given text. Line breaks and white-space
62
53
  * stay intact.
63
54
  */
64
- jsDoc(text: string, indentation?: string): JSDocBlock;
55
+ jsDoc(text: string, indentation?: string): Printable;
65
56
  /**
66
57
  * Create a JSDoc comment block for the given message, enumeration, or other
67
58
  * descriptor. The comment block will contain the original comments from the
68
59
  * protobuf source, and annotations such as `@generated from message MyMessage`.
69
60
  */
70
- jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
61
+ jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): Printable;
71
62
  /**
72
63
  * Create a printable export statement. For example:
73
64
  *
@@ -84,11 +75,15 @@ export interface GeneratedFile {
84
75
  * statement. If the plugin option `js_import_style=legacy_commonjs` is set,
85
76
  * exports will automatically be generated for CommonJS.
86
77
  */
87
- exportDecl(declaration: string, name: string | DescMessage | DescEnum | DescExtension): Printable;
78
+ exportDecl(declaration: string, name: string): Printable;
88
79
  /**
89
80
  * Import a message or enumeration generated by protoc-gen-es.
90
81
  */
91
- import(type: DescMessage | DescEnum | DescExtension): ImportSymbol;
82
+ importShape(desc: DescMessage | DescEnum): ImportSymbol;
83
+ /**
84
+ * Import a descriptor generated by protoc-gen-es.
85
+ */
86
+ importDesc(desc: DescMessage | DescEnum | DescExtension | DescService | DescFile, typeOnly?: boolean): ImportSymbol;
92
87
  /**
93
88
  * Import any symbol from a file or package.
94
89
  *
@@ -105,12 +100,16 @@ export interface GeneratedFile {
105
100
  * formulate your own imports and exports based on this property.
106
101
  */
107
102
  readonly jsImportStyle: "module" | "legacy_commonjs";
103
+ /**
104
+ * Provides some symbols from the runtime library @bufbuild/protobuf.
105
+ */
106
+ readonly runtime: RuntimeImports;
108
107
  }
109
108
  export interface GeneratedFileController extends GeneratedFile {
110
109
  getFileInfo(): FileInfo;
111
110
  }
112
- type CreateTypeImportFn = (desc: DescMessage | DescEnum | DescExtension) => ImportSymbol;
113
- type RewriteImportPathFn = (path: string) => string;
114
- type CreatePreambleFn = (descFile: DescFile) => string;
115
- export declare function createGeneratedFile(name: string, importPath: string, jsImportStyle: "module" | "legacy_commonjs", rewriteImportPath: RewriteImportPathFn, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, createPreamble: CreatePreambleFn): GeneratedFileController;
116
- export {};
111
+ export type ResolveDescImportFn = (desc: DescMessage | DescEnum | DescExtension | DescService | DescFile, typeOnly?: boolean) => ImportSymbol;
112
+ export type ResolveShapeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
113
+ export type RewriteImportFn = (path: string) => string;
114
+ export type CreatePreambleFn = (descFile: DescFile) => string;
115
+ export declare function createGeneratedFile(name: string, importPath: string, jsImportStyle: "module" | "legacy_commonjs", rewriteImport: RewriteImportFn, resolveDescImport: ResolveDescImportFn, resolveShapeImport: ResolveShapeImportFn, createPreamble: CreatePreambleFn, runtime: RuntimeImports): GeneratedFileController;
@@ -15,10 +15,11 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.createGeneratedFile = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
+ const reflect_1 = require("@bufbuild/protobuf/reflect");
18
19
  const import_symbol_js_1 = require("./import-symbol.js");
19
20
  const import_path_js_1 = require("./import-path.js");
20
21
  const jsdoc_js_1 = require("./jsdoc.js");
21
- function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath, createTypeImport, runtimeImports, createPreamble) {
22
+ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImport, resolveDescImport, resolveShapeImport, createPreamble, runtime) {
22
23
  let preamble;
23
24
  const el = [];
24
25
  return {
@@ -39,38 +40,51 @@ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath,
39
40
  ? [printableOrFragments, ...rest]
40
41
  : rest;
41
42
  }
42
- printableToEl(printables, el, createTypeImport, runtimeImports);
43
+ printableToEl({
44
+ el,
45
+ runtime,
46
+ resolveDescImport,
47
+ resolveShapeImport,
48
+ }, printables);
43
49
  el.push("\n");
44
50
  },
45
- export(name) {
46
- return (0, import_symbol_js_1.createImportSymbol)(name, importPath);
47
- },
48
51
  exportDecl(declaration, name) {
49
52
  return {
50
- kind: "es_export_decl",
51
- declaration,
53
+ kind: "es_export_stmt",
52
54
  name,
55
+ declaration,
53
56
  };
54
57
  },
55
58
  string(string) {
56
- // We do not use LiteralString, which was added later, to maintain backwards compatibility
57
- return escapeString(string);
59
+ return {
60
+ kind: "es_string",
61
+ value: string,
62
+ };
58
63
  },
59
64
  jsDoc(textOrDesc, indentation) {
60
- return (0, jsdoc_js_1.createJsDocBlock)(textOrDesc, indentation);
65
+ return {
66
+ kind: "es_jsdoc",
67
+ text: typeof textOrDesc == "string"
68
+ ? textOrDesc
69
+ : (0, jsdoc_js_1.createJsDocTextFromDesc)(textOrDesc),
70
+ indentation,
71
+ };
61
72
  },
62
- import(typeOrName, from) {
63
- if (typeof typeOrName == "string") {
64
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
65
- return (0, import_symbol_js_1.createImportSymbol)(typeOrName, from);
66
- }
67
- return createTypeImport(typeOrName);
73
+ importDesc(desc, typeOnly = false) {
74
+ return resolveDescImport(desc, typeOnly);
75
+ },
76
+ importShape(desc) {
77
+ return resolveShapeImport(desc);
78
+ },
79
+ import(name, from) {
80
+ return (0, import_symbol_js_1.createImportSymbol)(name, from);
68
81
  },
69
82
  jsImportStyle,
83
+ runtime,
70
84
  getFileInfo() {
71
85
  return {
72
86
  name,
73
- content: elToContent(el, importPath, rewriteImportPath, jsImportStyle == "legacy_commonjs"),
87
+ content: elToContent(el, importPath, rewriteImport, jsImportStyle == "legacy_commonjs"),
74
88
  preamble,
75
89
  };
76
90
  },
@@ -146,10 +160,11 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
146
160
  }
147
161
  return c.join("");
148
162
  }
149
- function printableToEl(printables, el, createTypeImport, runtimeImports) {
163
+ function printableToEl(opt, printables) {
164
+ const { el } = opt;
150
165
  for (const p of printables) {
151
166
  if (Array.isArray(p)) {
152
- printableToEl(p, el, createTypeImport, runtimeImports);
167
+ printableToEl(opt, p);
153
168
  }
154
169
  else {
155
170
  switch (typeof p) {
@@ -157,52 +172,88 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
157
172
  el.push(p);
158
173
  break;
159
174
  case "number":
160
- elNumber(el, p, runtimeImports);
175
+ if (Number.isNaN(p)) {
176
+ el.push("globalThis.NaN");
177
+ }
178
+ else if (p === Number.POSITIVE_INFINITY) {
179
+ el.push("globalThis.Infinity");
180
+ }
181
+ else if (p === Number.NEGATIVE_INFINITY) {
182
+ el.push("-globalThis.Infinity");
183
+ }
184
+ else {
185
+ el.push(p.toString(10));
186
+ }
161
187
  break;
162
188
  case "boolean":
163
189
  el.push(p.toString());
164
190
  break;
165
191
  case "bigint":
166
- elBigint(el, p, runtimeImports);
192
+ if (p == protobuf_1.protoInt64.zero) {
193
+ // Loose comparison will match between 0n and 0.
194
+ el.push(opt.runtime.protoInt64, ".zero");
195
+ }
196
+ else {
197
+ el.push(opt.runtime.protoInt64, p > 0 ? ".uParse(" : ".parse(", escapeString(p.toString()), ")");
198
+ }
167
199
  break;
168
200
  case "object":
169
201
  if (p instanceof Uint8Array) {
170
- elUint8Array(el, p);
202
+ if (p.length === 0) {
203
+ el.push("new Uint8Array(0)");
204
+ }
205
+ else {
206
+ el.push("new Uint8Array([");
207
+ const strings = [];
208
+ for (const n of p) {
209
+ strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
210
+ }
211
+ el.push(strings.join(", "));
212
+ el.push("])");
213
+ }
171
214
  break;
172
215
  }
173
216
  switch (p.kind) {
174
217
  case "es_symbol":
218
+ case "es_export_stmt":
175
219
  el.push(p);
176
220
  break;
221
+ case "es_desc_ref":
222
+ el.push(opt.resolveDescImport(p.desc, p.typeOnly));
223
+ break;
224
+ case "es_shape_ref":
225
+ el.push(opt.resolveShapeImport(p.desc));
226
+ break;
177
227
  case "es_jsdoc":
178
- el.push(p.toString());
228
+ el.push((0, jsdoc_js_1.formatJsDocBlock)(p.text, p.indentation));
179
229
  break;
180
230
  case "es_string":
181
231
  el.push(escapeString(p.value));
182
232
  break;
183
233
  case "es_proto_int64":
184
- elProtoInt64(el, p, runtimeImports);
185
- break;
186
- case "es_export_decl":
187
- el.push({
188
- kind: "es_export_stmt",
189
- declaration: p.declaration,
190
- name: typeof p.name == "string"
191
- ? p.name
192
- : createTypeImport(p.name).name,
193
- });
194
- break;
195
- case "es_ref_message":
196
- case "es_ref_enum":
197
- el.push(p.typeOnly
198
- ? createTypeImport(p.type).toTypeOnly()
199
- : createTypeImport(p.type));
200
- break;
201
- case "message":
202
- case "extension":
203
- case "enum":
204
- el.push(createTypeImport(p));
234
+ switch (p.longType) {
235
+ case reflect_1.LongType.STRING:
236
+ el.push(escapeString(p.value.toString()));
237
+ break;
238
+ case reflect_1.LongType.BIGINT:
239
+ if (p.value == protobuf_1.protoInt64.zero) {
240
+ // Loose comparison will match between 0n and 0.
241
+ el.push(opt.runtime.protoInt64, ".zero");
242
+ break;
243
+ }
244
+ switch (p.type) {
245
+ case reflect_1.ScalarType.UINT64:
246
+ case reflect_1.ScalarType.FIXED64:
247
+ el.push(opt.runtime.protoInt64, ".uParse(", escapeString(p.value.toString()), ")");
248
+ break;
249
+ default:
250
+ el.push(opt.runtime.protoInt64, ".parse(", escapeString(p.value.toString()), ")");
251
+ break;
252
+ }
253
+ }
205
254
  break;
255
+ default:
256
+ throw `cannot print ${typeof p}`;
206
257
  }
207
258
  break;
208
259
  default:
@@ -327,70 +378,6 @@ function processImports(el, importerPath, rewriteImportPath, makeImportStatement
327
378
  }
328
379
  return symbolToIdentifier;
329
380
  }
330
- function elBigint(el, value, runtimeImports) {
331
- if (value == protobuf_1.protoInt64.zero) {
332
- // Loose comparison will match between 0n and 0.
333
- el.push(runtimeImports.protoInt64, ".zero");
334
- }
335
- else {
336
- el.push(runtimeImports.protoInt64, value > 0 ? ".uParse(" : ".parse(", escapeString(value.toString()), ")");
337
- }
338
- }
339
- function elNumber(el, value, runtimeImports) {
340
- if (Number.isNaN(value)) {
341
- el.push(runtimeImports.protoDouble, ".NaN");
342
- }
343
- else if (value === Number.POSITIVE_INFINITY) {
344
- el.push(runtimeImports.protoDouble, ".POSITIVE_INFINITY");
345
- }
346
- else if (value === Number.NEGATIVE_INFINITY) {
347
- el.push(runtimeImports.protoDouble, ".NEGATIVE_INFINITY");
348
- }
349
- else {
350
- el.push(value.toString(10));
351
- }
352
- }
353
- function elUint8Array(el, value) {
354
- if (value.length === 0) {
355
- el.push("new Uint8Array(0)");
356
- return;
357
- }
358
- el.push("new Uint8Array([");
359
- const strings = [];
360
- for (const n of value) {
361
- strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
362
- }
363
- el.push(strings.join(", "));
364
- el.push("])");
365
- }
366
- function elProtoInt64(el, literal, runtimeImports) {
367
- switch (literal.longType) {
368
- case protobuf_1.LongType.STRING:
369
- el.push(escapeString(literal.value.toString()));
370
- break;
371
- case protobuf_1.LongType.BIGINT:
372
- if (literal.value == protobuf_1.protoInt64.zero) {
373
- // Loose comparison will match between 0n and 0.
374
- el.push(runtimeImports.protoInt64, ".zero");
375
- break;
376
- }
377
- switch (literal.type) {
378
- case protobuf_1.ScalarType.UINT64:
379
- case protobuf_1.ScalarType.FIXED64:
380
- el.push(runtimeImports.protoInt64);
381
- el.push(".uParse(");
382
- el.push(escapeString(literal.value.toString()));
383
- el.push(")");
384
- break;
385
- default:
386
- el.push(runtimeImports.protoInt64);
387
- el.push(".parse(");
388
- el.push(escapeString(literal.value.toString()));
389
- el.push(")");
390
- break;
391
- }
392
- }
393
- }
394
381
  function escapeString(value) {
395
382
  return ('"' +
396
383
  value
@@ -1,4 +1,3 @@
1
- import { DescFile } from "@bufbuild/protobuf";
2
1
  /**
3
2
  * A configuration for rewriting import paths, a feature mainly used for
4
3
  * remote code generation in the BSR npm registry, which makes it possible
@@ -44,11 +43,6 @@ export type RewriteImports = {
44
43
  * to the given import extension.
45
44
  */
46
45
  export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
47
- /**
48
- * Returns the import path for files generated by the base type generator
49
- * protoc-gen-es.
50
- */
51
- export declare function makeImportPath(file: DescFile, bootstrapWkt: boolean, filesToGenerate: DescFile[]): string;
52
46
  export declare const relativePathRE: RegExp;
53
47
  /**
54
48
  * Derives an ECMAScript module import path from a file path. For example,
@@ -13,8 +13,7 @@
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.makeImportPathRelative = exports.deriveImportPath = exports.relativePathRE = exports.makeImportPath = exports.rewriteImportPath = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
16
+ exports.makeImportPathRelative = exports.deriveImportPath = exports.relativePathRE = exports.rewriteImportPath = void 0;
18
17
  const cache = new WeakMap();
19
18
  /**
20
19
  * Apply import rewrites to the given import path, and change all .js extensions
@@ -85,26 +84,6 @@ function starToRegExp(star) {
85
84
  r.push("$");
86
85
  return new RegExp(r.join(""));
87
86
  }
88
- /**
89
- * Returns the import path for files generated by the base type generator
90
- * protoc-gen-es.
91
- */
92
- function makeImportPath(file, bootstrapWkt, filesToGenerate) {
93
- // Well-known types are published with the runtime package. We usually want
94
- // the generated code to import them from the runtime package, with the
95
- // following exceptions:
96
- // 1. We are bootstrapping the runtime package via the plugin option
97
- // "bootstrap_wkt". In that case, we cannot refer to the runtime package
98
- // itself.
99
- // 2. We were explicitly asked to generate the well-known type.
100
- if (!bootstrapWkt &&
101
- !filesToGenerate.includes(file) &&
102
- protobuf_1.codegenInfo.wktSourceFiles.includes(file.name + ".proto")) {
103
- return protobuf_1.codegenInfo.packageName;
104
- }
105
- return "./" + file.name + "_pb.js";
106
- }
107
- exports.makeImportPath = makeImportPath;
108
87
  exports.relativePathRE = /^\.{1,2}\//;
109
88
  /**
110
89
  * Derives an ECMAScript module import path from a file path. For example,
@@ -29,11 +29,9 @@ export type ImportSymbol = {
29
29
  /**
30
30
  * The unique ID based on name and from, disregarding typeOnly.
31
31
  */
32
- readonly id: EsSymbolId;
32
+ readonly id: `import("${string}").${string}`;
33
33
  };
34
34
  /**
35
35
  * Create a new import symbol.
36
36
  */
37
37
  export declare function createImportSymbol(name: string, from: string, typeOnly?: boolean): ImportSymbol;
38
- type EsSymbolId = string;
39
- export {};
@@ -18,17 +18,15 @@ exports.createImportSymbol = void 0;
18
18
  * Create a new import symbol.
19
19
  */
20
20
  function createImportSymbol(name, from, typeOnly) {
21
- const id = `import("${from}").${name}`;
22
- const s = {
21
+ return {
23
22
  kind: "es_symbol",
24
23
  name,
25
24
  from,
26
- typeOnly: false,
27
- id,
25
+ typeOnly: typeOnly !== null && typeOnly !== void 0 ? typeOnly : false,
26
+ id: `import("${from}").${name}`,
28
27
  toTypeOnly() {
29
28
  return Object.assign(Object.assign({}, this), { typeOnly: true });
30
29
  },
31
30
  };
32
- return typeOnly === true ? s.toTypeOnly() : s;
33
31
  }
34
32
  exports.createImportSymbol = createImportSymbol;
@@ -1,20 +1,7 @@
1
- import { AnyDesc, DescExtension, DescFile } from "@bufbuild/protobuf";
2
- import { Printable } from "./generated-file.js";
3
- export { reifyWkt } from "./reify-wkt.js";
4
1
  export type { Target } from "./target.js";
5
2
  export type { Schema } from "./schema.js";
6
- export type { RuntimeImports } from "./runtime-imports.js";
7
- export type { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
3
+ export type { GeneratedFile, FileInfo } from "./generated-file.js";
8
4
  export type { ImportSymbol } from "./import-symbol.js";
9
5
  export { createImportSymbol } from "./import-symbol.js";
10
- export declare const localName: (desc: import("@bufbuild/protobuf").DescEnum | import("@bufbuild/protobuf").DescMessage | import("@bufbuild/protobuf").DescService | DescExtension | import("@bufbuild/protobuf").DescEnumValue | import("@bufbuild/protobuf").DescField | import("@bufbuild/protobuf").DescOneof | import("@bufbuild/protobuf").DescMethod) => string;
11
- export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, literalString, } from "./legacy-gencommon.js";
12
- export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./legacy-custom-options.js";
13
- /**
14
- * @deprecated Please use GeneratedFile.jsDoc() instead
15
- */
16
- export declare function makeJsDoc(desc: Exclude<AnyDesc, DescFile | DescExtension>, indentation?: string): Printable;
17
- /**
18
- * @deprecated Please use GeneratedFile.jsDoc() instead
19
- */
20
- export declare function createJsDocBlock(text: string, indentation?: string): Printable;
6
+ export type { Printable } from "./printable.js";
7
+ export { safeIdentifier } from "./safe-identifier.js";