@bufbuild/protoplugin 1.7.2 → 1.8.0

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 (45) hide show
  1. package/dist/cjs/ecmascript/file-preamble.d.ts +2 -0
  2. package/dist/cjs/ecmascript/file-preamble.js +74 -0
  3. package/dist/cjs/ecmascript/generated-file.d.ts +3 -3
  4. package/dist/cjs/ecmascript/generated-file.js +90 -31
  5. package/dist/cjs/ecmascript/index.d.ts +2 -6
  6. package/dist/cjs/ecmascript/index.js +10 -17
  7. package/dist/cjs/ecmascript/jsdoc.d.ts +6 -0
  8. package/dist/cjs/ecmascript/jsdoc.js +1 -0
  9. package/dist/cjs/ecmascript/{gencommon.d.ts → legacy-gencommon.d.ts} +12 -6
  10. package/dist/cjs/ecmascript/{gencommon.js → legacy-gencommon.js} +50 -98
  11. package/dist/cjs/ecmascript/opaque-printables.d.ts +26 -0
  12. package/dist/{esm/ecmascript/export-declaration.js → cjs/ecmascript/opaque-printables.js} +2 -7
  13. package/dist/cjs/ecmascript/schema.d.ts +1 -1
  14. package/dist/cjs/ecmascript/schema.js +2 -2
  15. package/dist/cjs/error.d.ts +2 -0
  16. package/dist/cjs/error.js +9 -1
  17. package/dist/cjs/run-node.js +1 -1
  18. package/dist/esm/ecmascript/file-preamble.d.ts +2 -0
  19. package/dist/esm/ecmascript/file-preamble.js +70 -0
  20. package/dist/esm/ecmascript/generated-file.d.ts +3 -3
  21. package/dist/esm/ecmascript/generated-file.js +90 -31
  22. package/dist/esm/ecmascript/index.d.ts +2 -6
  23. package/dist/esm/ecmascript/index.js +2 -9
  24. package/dist/esm/ecmascript/jsdoc.d.ts +6 -0
  25. package/dist/esm/ecmascript/jsdoc.js +1 -0
  26. package/dist/esm/ecmascript/{gencommon.d.ts → legacy-gencommon.d.ts} +12 -6
  27. package/dist/esm/ecmascript/{gencommon.js → legacy-gencommon.js} +49 -95
  28. package/dist/esm/ecmascript/opaque-printables.d.ts +26 -0
  29. package/dist/{cjs/ecmascript/export-declaration.js → esm/ecmascript/opaque-printables.js} +1 -11
  30. package/dist/esm/ecmascript/schema.d.ts +1 -1
  31. package/dist/esm/ecmascript/schema.js +1 -1
  32. package/dist/esm/error.d.ts +2 -0
  33. package/dist/esm/error.js +7 -0
  34. package/dist/esm/run-node.js +2 -2
  35. package/package.json +3 -14
  36. package/dist/cjs/ecmascript/export-declaration.d.ts +0 -7
  37. package/dist/esm/ecmascript/export-declaration.d.ts +0 -7
  38. package/dist/proxy/ecmascript/index.d.ts +0 -1
  39. package/dist/proxy/ecmascript/index.js +0 -1
  40. package/dist/proxy/index.d.ts +0 -1
  41. package/dist/proxy/index.js +0 -1
  42. /package/dist/cjs/ecmascript/{custom-options.d.ts → legacy-custom-options.d.ts} +0 -0
  43. /package/dist/cjs/ecmascript/{custom-options.js → legacy-custom-options.js} +0 -0
  44. /package/dist/esm/ecmascript/{custom-options.d.ts → legacy-custom-options.d.ts} +0 -0
  45. /package/dist/esm/ecmascript/{custom-options.js → legacy-custom-options.js} +0 -0
@@ -19,7 +19,7 @@ const generated_file_js_1 = require("./generated-file.js");
19
19
  const runtime_imports_js_1 = require("./runtime-imports.js");
20
20
  const import_symbol_js_1 = require("./import-symbol.js");
21
21
  const import_path_js_1 = require("./import-path.js");
22
- const gencommon_1 = require("./gencommon");
22
+ const file_preamble_js_1 = require("./file-preamble.js");
23
23
  function createSchema(request, parameter, pluginName, pluginVersion, featureSetDefaults) {
24
24
  const descriptorSet = (0, protobuf_1.createDescriptorSet)(request.protoFile, {
25
25
  featureSetDefaults,
@@ -31,7 +31,7 @@ function createSchema(request, parameter, pluginName, pluginVersion, featureSetD
31
31
  const from = (0, import_path_js_1.makeImportPath)(desc.file, parameter.bootstrapWkt, filesToGenerate);
32
32
  return (0, import_symbol_js_1.createImportSymbol)(name, from);
33
33
  };
34
- const createPreamble = (descFile) => (0, gencommon_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
34
+ const createPreamble = (descFile) => (0, file_preamble_js_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
35
35
  let target;
36
36
  const generatedFiles = [];
37
37
  return {
@@ -1,4 +1,6 @@
1
1
  export declare class PluginOptionError extends Error {
2
+ name: string;
2
3
  constructor(option: string, reason?: unknown);
3
4
  }
4
5
  export declare function reasonToString(reason: unknown): string;
6
+ export declare function isPluginOptionError(arg: unknown): arg is PluginOptionError;
package/dist/cjs/error.js CHANGED
@@ -13,13 +13,14 @@
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.reasonToString = exports.PluginOptionError = void 0;
16
+ exports.isPluginOptionError = exports.reasonToString = exports.PluginOptionError = void 0;
17
17
  class PluginOptionError extends Error {
18
18
  constructor(option, reason) {
19
19
  const detail = reason !== undefined ? reasonToString(reason) : "";
20
20
  super(detail.length > 0
21
21
  ? `invalid option "${option}": ${detail}`
22
22
  : `invalid option "${option}"`);
23
+ this.name = "PluginOptionError";
23
24
  }
24
25
  }
25
26
  exports.PluginOptionError = PluginOptionError;
@@ -33,3 +34,10 @@ function reasonToString(reason) {
33
34
  return String(reason);
34
35
  }
35
36
  exports.reasonToString = reasonToString;
37
+ function isPluginOptionError(arg) {
38
+ if (!(arg instanceof Error)) {
39
+ return false;
40
+ }
41
+ return arg.name === "PluginOptionError";
42
+ }
43
+ exports.isPluginOptionError = isPluginOptionError;
@@ -46,7 +46,7 @@ function runNodeJs(plugin) {
46
46
  })
47
47
  .then(() => process.exit(0))
48
48
  .catch((reason) => {
49
- const message = reason instanceof error_js_1.PluginOptionError
49
+ const message = (0, error_js_1.isPluginOptionError)(reason)
50
50
  ? reason.message
51
51
  : (0, error_js_1.reasonToString)(reason);
52
52
  process.stderr.write(`${plugin.name}: ${message}\n`);
@@ -0,0 +1,2 @@
1
+ import type { DescFile } from "@bufbuild/protobuf";
2
+ export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
@@ -0,0 +1,70 @@
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { Edition } from "@bufbuild/protobuf";
15
+ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
16
+ const builder = [];
17
+ const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
18
+ const writeLeadingComments = (comments) => {
19
+ for (let comment of comments.leadingDetached) {
20
+ comment = trimSuffix(comment, "\n");
21
+ for (const line of comment.split("\n")) {
22
+ builder.push(`//${line}\n`);
23
+ }
24
+ builder.push("\n");
25
+ }
26
+ if (comments.leading !== undefined) {
27
+ const comment = trimSuffix(comments.leading, "\n");
28
+ for (const line of comment.split("\n")) {
29
+ builder.push(`//${line}\n`);
30
+ }
31
+ builder.push("\n");
32
+ }
33
+ };
34
+ writeLeadingComments(file.getSyntaxComments());
35
+ builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
36
+ if (parameter !== "") {
37
+ builder.push(` with parameter "${parameter}"`);
38
+ }
39
+ builder.push("\n");
40
+ builder.push(`// @generated from file ${file.name}.proto (`);
41
+ if (file.proto.package !== undefined) {
42
+ builder.push(`package ${file.proto.package}, `);
43
+ }
44
+ switch (file.edition) {
45
+ case Edition.EDITION_PROTO2:
46
+ builder.push(`syntax proto2)\n`);
47
+ break;
48
+ case Edition.EDITION_PROTO3:
49
+ builder.push(`syntax proto3)\n`);
50
+ break;
51
+ default: {
52
+ const editionString = Edition[file.edition];
53
+ if (typeof editionString == "string") {
54
+ const e = editionString.replace("EDITION_", "").toLowerCase();
55
+ builder.push(`edition ${e})\n`);
56
+ }
57
+ else {
58
+ builder.push(`unknown edition\n`);
59
+ }
60
+ break;
61
+ }
62
+ }
63
+ builder.push("/* eslint-disable */\n");
64
+ if (tsNoCheck) {
65
+ builder.push("// @ts-nocheck\n");
66
+ }
67
+ builder.push("\n");
68
+ writeLeadingComments(file.getPackageComments());
69
+ return trimSuffix(builder.join(""), "\n");
70
+ }
@@ -1,12 +1,12 @@
1
- import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage } from "@bufbuild/protobuf";
1
+ import { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage } from "@bufbuild/protobuf";
2
2
  import type { ImportSymbol } from "./import-symbol.js";
3
3
  import type { RuntimeImports } from "./runtime-imports.js";
4
- import type { ExportDeclaration } from "./export-declaration.js";
5
4
  import type { JSDocBlock } from "./jsdoc.js";
5
+ import { ExportDeclaration, LiteralProtoInt64, LiteralString, RefDescEnum, RefDescMessage } from "./opaque-printables.js";
6
6
  /**
7
7
  * All types that can be passed to GeneratedFile.print()
8
8
  */
9
- export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | DescMessage | DescEnum | DescExtension | Printable[];
9
+ export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | LiteralString | LiteralProtoInt64 | RefDescMessage | RefDescEnum | DescMessage | DescEnum | DescExtension | Printable[];
10
10
  /**
11
11
  * FileInfo represents an intermediate type using for transpiling TypeScript internally.
12
12
  */
@@ -11,10 +11,9 @@
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 { LongType, protoInt64, ScalarType, } from "@bufbuild/protobuf";
14
15
  import { createImportSymbol } from "./import-symbol.js";
15
- import { literalString } from "./gencommon.js";
16
16
  import { makeImportPathRelative } from "./import-path.js";
17
- import { createExportDeclaration } from "./export-declaration.js";
18
17
  import { createJsDocBlock } from "./jsdoc.js";
19
18
  export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath, createTypeImport, runtimeImports, createPreamble) {
20
19
  let preamble;
@@ -44,10 +43,15 @@ export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImpo
44
43
  return createImportSymbol(name, importPath);
45
44
  },
46
45
  exportDecl(declaration, name) {
47
- return createExportDeclaration(declaration, name);
46
+ return {
47
+ kind: "es_export_decl",
48
+ declaration,
49
+ name,
50
+ };
48
51
  },
49
52
  string(string) {
50
- return literalString(string);
53
+ // We do not use LiteralString, which was added later, to maintain backwards compatibility
54
+ return escapeString(string);
51
55
  },
52
56
  jsDoc(textOrDesc, indentation) {
53
57
  return createJsDocBlock(textOrDesc, indentation);
@@ -83,16 +87,16 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
83
87
  if (legacyCommonJs) {
84
88
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name}: ${alias}`);
85
89
  const what = `{ ${p.join(", ")} }`;
86
- c.push(`const ${what} = require(${literalString(from)});\n`);
90
+ c.push(`const ${what} = require(${escapeString(from)});\n`);
87
91
  }
88
92
  else {
89
93
  const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
90
94
  const what = `{ ${p.join(", ")} }`;
91
95
  if (typeOnly) {
92
- c.push(`import type ${what} from ${literalString(from)};\n`);
96
+ c.push(`import type ${what} from ${escapeString(from)};\n`);
93
97
  }
94
98
  else {
95
- c.push(`import ${what} from ${literalString(from)};\n`);
99
+ c.push(`import ${what} from ${escapeString(from)};\n`);
96
100
  }
97
101
  }
98
102
  });
@@ -149,17 +153,17 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
149
153
  el.push(p);
150
154
  break;
151
155
  case "number":
152
- el.push(...literalNumber(p, runtimeImports));
156
+ elNumber(el, p, runtimeImports);
153
157
  break;
154
158
  case "boolean":
155
159
  el.push(p.toString());
156
160
  break;
157
161
  case "bigint":
158
- el.push(...literalBigint(p, runtimeImports));
162
+ elBigint(el, p, runtimeImports);
159
163
  break;
160
164
  case "object":
161
165
  if (p instanceof Uint8Array) {
162
- el.push(literalUint8Array(p));
166
+ elUint8Array(el, p);
163
167
  break;
164
168
  }
165
169
  switch (p.kind) {
@@ -169,6 +173,12 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
169
173
  case "es_jsdoc":
170
174
  el.push(p.toString());
171
175
  break;
176
+ case "es_string":
177
+ el.push(escapeString(p.value));
178
+ break;
179
+ case "es_proto_int64":
180
+ elProtoInt64(el, p, runtimeImports);
181
+ break;
172
182
  case "es_export_decl":
173
183
  el.push({
174
184
  kind: "es_export_stmt",
@@ -178,6 +188,12 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
178
188
  : createTypeImport(p.name).name,
179
189
  });
180
190
  break;
191
+ case "es_ref_message":
192
+ case "es_ref_enum":
193
+ el.push(p.typeOnly
194
+ ? createTypeImport(p.type).toTypeOnly()
195
+ : createTypeImport(p.type));
196
+ break;
181
197
  case "message":
182
198
  case "extension":
183
199
  case "enum":
@@ -307,37 +323,80 @@ function processImports(el, importerPath, rewriteImportPath, makeImportStatement
307
323
  }
308
324
  return symbolToIdentifier;
309
325
  }
310
- function literalNumber(value, runtimeImports) {
326
+ function elBigint(el, value, runtimeImports) {
327
+ if (value == protoInt64.zero) {
328
+ // Loose comparison will match between 0n and 0.
329
+ el.push(runtimeImports.protoInt64, ".zero");
330
+ }
331
+ else {
332
+ el.push(runtimeImports.protoInt64, value > 0 ? ".uParse(" : ".parse(", escapeString(value.toString()), ")");
333
+ }
334
+ }
335
+ function elNumber(el, value, runtimeImports) {
311
336
  if (Number.isNaN(value)) {
312
- return [runtimeImports.protoDouble, ".NaN"];
337
+ el.push(runtimeImports.protoDouble, ".NaN");
313
338
  }
314
- if (value === Number.POSITIVE_INFINITY) {
315
- return [runtimeImports.protoDouble, ".POSITIVE_INFINITY"];
339
+ else if (value === Number.POSITIVE_INFINITY) {
340
+ el.push(runtimeImports.protoDouble, ".POSITIVE_INFINITY");
316
341
  }
317
- if (value === Number.NEGATIVE_INFINITY) {
318
- return [runtimeImports.protoDouble, ".NEGATIVE_INFINITY"];
342
+ else if (value === Number.NEGATIVE_INFINITY) {
343
+ el.push(runtimeImports.protoDouble, ".NEGATIVE_INFINITY");
319
344
  }
320
- return value.toString(10);
321
- }
322
- function literalBigint(value, runtimeImports) {
323
- // Loose comparison will match between 0n and 0.
324
- if (value == 0) {
325
- return [runtimeImports.protoInt64, ".zero"];
345
+ else {
346
+ el.push(value.toString(10));
326
347
  }
327
- return [
328
- runtimeImports.protoInt64,
329
- value > 0 ? ".uParse(" : ".parse(",
330
- literalString(value.toString()),
331
- ")",
332
- ];
333
348
  }
334
- function literalUint8Array(value) {
349
+ function elUint8Array(el, value) {
335
350
  if (value.length === 0) {
336
- return "new Uint8Array(0)";
351
+ el.push("new Uint8Array(0)");
352
+ return;
337
353
  }
354
+ el.push("new Uint8Array([");
338
355
  const strings = [];
339
356
  for (const n of value) {
340
357
  strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
341
358
  }
342
- return `new Uint8Array([${strings.join(", ")}])`;
359
+ el.push(strings.join(", "));
360
+ el.push("])");
361
+ }
362
+ function elProtoInt64(el, literal, runtimeImports) {
363
+ switch (literal.longType) {
364
+ case LongType.STRING:
365
+ el.push(escapeString(literal.value.toString()));
366
+ break;
367
+ case LongType.BIGINT:
368
+ if (literal.value == protoInt64.zero) {
369
+ // Loose comparison will match between 0n and 0.
370
+ el.push(runtimeImports.protoInt64, ".zero");
371
+ break;
372
+ }
373
+ switch (literal.type) {
374
+ case ScalarType.UINT64:
375
+ case ScalarType.FIXED64:
376
+ el.push(runtimeImports.protoInt64);
377
+ el.push(".uParse(");
378
+ el.push(escapeString(literal.value.toString()));
379
+ el.push(")");
380
+ break;
381
+ default:
382
+ el.push(runtimeImports.protoInt64);
383
+ el.push(".parse(");
384
+ el.push(escapeString(literal.value.toString()));
385
+ el.push(")");
386
+ break;
387
+ }
388
+ }
389
+ }
390
+ function escapeString(value) {
391
+ return ('"' +
392
+ value
393
+ .split("\\")
394
+ .join("\\\\")
395
+ .split('"')
396
+ .join('\\"')
397
+ .split("\r")
398
+ .join("\\r")
399
+ .split("\n")
400
+ .join("\\n") +
401
+ '"');
343
402
  }
@@ -8,12 +8,8 @@ export type { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
8
8
  export type { ImportSymbol } from "./import-symbol.js";
9
9
  export { createImportSymbol } from "./import-symbol.js";
10
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, } from "./gencommon.js";
12
- export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./custom-options.js";
13
- /**
14
- * @deprecated Please use GeneratedFile.string() instead
15
- */
16
- export declare function literalString(value: string): string;
11
+ export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, literalString, } from "./legacy-gencommon.js";
12
+ export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./legacy-custom-options.js";
17
13
  /**
18
14
  * @deprecated Please use GeneratedFile.jsDoc() instead
19
15
  */
@@ -13,18 +13,11 @@
13
13
  // limitations under the License.
14
14
  import { codegenInfo, } from "@bufbuild/protobuf";
15
15
  import { createJsDocBlock as createJsDocBlockInternal } from "./jsdoc.js";
16
- import { literalString as literalStringInternal } from "./gencommon.js";
17
16
  export { reifyWkt } from "./reify-wkt.js";
18
17
  export { createImportSymbol } from "./import-symbol.js";
19
18
  export const { localName } = codegenInfo;
20
- export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, } from "./gencommon.js";
21
- export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./custom-options.js";
22
- /**
23
- * @deprecated Please use GeneratedFile.string() instead
24
- */
25
- export function literalString(value) {
26
- return literalStringInternal(value);
27
- }
19
+ export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, literalString, } from "./legacy-gencommon.js";
20
+ export { findCustomScalarOption, findCustomMessageOption, findCustomEnumOption, } from "./legacy-custom-options.js";
28
21
  /**
29
22
  * @deprecated Please use GeneratedFile.jsDoc() instead
30
23
  */
@@ -1,8 +1,14 @@
1
1
  import type { AnyDesc, DescFile } from "@bufbuild/protobuf";
2
2
  export type JSDocBlock = {
3
3
  readonly kind: "es_jsdoc";
4
+ /**
5
+ * @deprecated In a future release, we will make this property optional.
6
+ */
4
7
  text: string;
5
8
  indentation?: string;
9
+ /**
10
+ * @deprecated In a future release, we will remove this method.
11
+ */
6
12
  toString(): string;
7
13
  };
8
14
  export declare function createJsDocBlock(textOrDesc: string | Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
@@ -11,6 +11,7 @@
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
+ // TODO simplify type JSDocBlock to bring it in line with others in opaque-printables.ts
14
15
  export function createJsDocBlock(textOrDesc, indentation) {
15
16
  const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
16
17
  return {
@@ -1,19 +1,25 @@
1
1
  import type { DescExtension } from "@bufbuild/protobuf";
2
- import { DescField, DescFile, LongType, ScalarType } from "@bufbuild/protobuf";
2
+ import { DescField } from "@bufbuild/protobuf";
3
3
  import type { GeneratedFile, Printable } from "./generated-file.js";
4
4
  import type { ImportSymbol } from "./import-symbol.js";
5
- export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
6
5
  /**
7
- * Returns an expression for the TypeScript typing of a field,
8
- * and whether the property should be optional.
6
+ * @deprecated Please use GeneratedFile.string() instead
7
+ */
8
+ export declare function literalString(value: string): string;
9
+ /**
10
+ * @deprecated
9
11
  */
10
12
  export declare function getFieldTyping(field: DescField | DescExtension, file: GeneratedFile): {
11
13
  typing: Printable;
12
14
  optional: boolean;
13
15
  };
14
- export declare function scalarTypeScriptType(type: ScalarType, longType: LongType): Printable;
15
- export declare function literalString(value: string): string;
16
+ /**
17
+ * @deprecated
18
+ */
16
19
  export declare function getFieldExplicitDefaultValue(field: DescField | DescExtension, protoInt64Symbol: ImportSymbol): Printable | undefined;
20
+ /**
21
+ * @deprecated
22
+ */
17
23
  export declare function getFieldIntrinsicDefaultValue(field: DescField): {
18
24
  defaultValue: Printable | undefined;
19
25
  typingInferrable: boolean;
@@ -11,67 +11,26 @@
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 { codegenInfo, Edition, LongType, ScalarType, } from "@bufbuild/protobuf";
15
- const { localName, getUnwrappedFieldType, scalarDefaultValue } = codegenInfo;
16
- export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
17
- const builder = [];
18
- const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
19
- const writeLeadingComments = (comments) => {
20
- for (let comment of comments.leadingDetached) {
21
- comment = trimSuffix(comment, "\n");
22
- for (const line of comment.split("\n")) {
23
- builder.push(`//${line}\n`);
24
- }
25
- builder.push("\n");
26
- }
27
- if (comments.leading !== undefined) {
28
- const comment = trimSuffix(comments.leading, "\n");
29
- for (const line of comment.split("\n")) {
30
- builder.push(`//${line}\n`);
31
- }
32
- builder.push("\n");
33
- }
34
- };
35
- writeLeadingComments(file.getSyntaxComments());
36
- builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
37
- if (parameter !== "") {
38
- builder.push(` with parameter "${parameter}"`);
39
- }
40
- builder.push("\n");
41
- builder.push(`// @generated from file ${file.name}.proto (`);
42
- if (file.proto.package !== undefined) {
43
- builder.push(`package ${file.proto.package}, `);
44
- }
45
- switch (file.edition) {
46
- case Edition.EDITION_PROTO2:
47
- builder.push(`syntax proto2)\n`);
48
- break;
49
- case Edition.EDITION_PROTO3:
50
- builder.push(`syntax proto3)\n`);
51
- break;
52
- default: {
53
- const editionString = Edition[file.edition];
54
- if (typeof editionString == "string") {
55
- const e = editionString.replace("EDITION_", "").toLowerCase();
56
- builder.push(`edition ${e})\n`);
57
- }
58
- else {
59
- builder.push(`unknown edition\n`);
60
- }
61
- break;
62
- }
63
- }
64
- builder.push("/* eslint-disable */\n");
65
- if (tsNoCheck) {
66
- builder.push("// @ts-nocheck\n");
67
- }
68
- builder.push("\n");
69
- writeLeadingComments(file.getPackageComments());
70
- return trimSuffix(builder.join(""), "\n");
14
+ import { codegenInfo, LongType, ScalarType, } from "@bufbuild/protobuf";
15
+ const { localName, getUnwrappedFieldType, scalarZeroValue } = codegenInfo;
16
+ /**
17
+ * @deprecated Please use GeneratedFile.string() instead
18
+ */
19
+ export function literalString(value) {
20
+ return ('"' +
21
+ value
22
+ .split("\\")
23
+ .join("\\\\")
24
+ .split('"')
25
+ .join('\\"')
26
+ .split("\r")
27
+ .join("\\r")
28
+ .split("\n")
29
+ .join("\\n") +
30
+ '"');
71
31
  }
72
32
  /**
73
- * Returns an expression for the TypeScript typing of a field,
74
- * and whether the property should be optional.
33
+ * @deprecated
75
34
  */
76
35
  export function getFieldTyping(field, file) {
77
36
  const typing = [];
@@ -133,40 +92,9 @@ export function getFieldTyping(field, file) {
133
92
  }
134
93
  return { typing, optional };
135
94
  }
136
- export function scalarTypeScriptType(type, longType) {
137
- switch (type) {
138
- case ScalarType.STRING:
139
- return "string";
140
- case ScalarType.BOOL:
141
- return "boolean";
142
- case ScalarType.UINT64:
143
- case ScalarType.SFIXED64:
144
- case ScalarType.FIXED64:
145
- case ScalarType.SINT64:
146
- case ScalarType.INT64:
147
- if (longType === LongType.STRING) {
148
- return "string";
149
- }
150
- return "bigint";
151
- case ScalarType.BYTES:
152
- return "Uint8Array";
153
- default:
154
- return "number";
155
- }
156
- }
157
- export function literalString(value) {
158
- return ('"' +
159
- value
160
- .split("\\")
161
- .join("\\\\")
162
- .split('"')
163
- .join('\\"')
164
- .split("\r")
165
- .join("\\r")
166
- .split("\n")
167
- .join("\\n") +
168
- '"');
169
- }
95
+ /**
96
+ * @deprecated
97
+ */
170
98
  export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
171
99
  switch (field.fieldKind) {
172
100
  case "enum": {
@@ -222,6 +150,9 @@ export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
222
150
  }
223
151
  return undefined;
224
152
  }
153
+ /**
154
+ * @deprecated
155
+ */
225
156
  export function getFieldIntrinsicDefaultValue(field) {
226
157
  if (field.repeated) {
227
158
  return {
@@ -257,8 +188,7 @@ export function getFieldIntrinsicDefaultValue(field) {
257
188
  defaultValue = literalString("");
258
189
  }
259
190
  else {
260
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
261
- defaultValue = scalarDefaultValue(field.scalar, field.longType);
191
+ defaultValue = scalarZeroValue(field.scalar, field.longType);
262
192
  if (typeof defaultValue === "string") {
263
193
  defaultValue = literalString(defaultValue);
264
194
  }
@@ -274,3 +204,27 @@ export function getFieldIntrinsicDefaultValue(field) {
274
204
  typingInferrable,
275
205
  };
276
206
  }
207
+ /**
208
+ * @deprecated
209
+ */
210
+ function scalarTypeScriptType(type, longType) {
211
+ switch (type) {
212
+ case ScalarType.STRING:
213
+ return "string";
214
+ case ScalarType.BOOL:
215
+ return "boolean";
216
+ case ScalarType.UINT64:
217
+ case ScalarType.SFIXED64:
218
+ case ScalarType.FIXED64:
219
+ case ScalarType.SINT64:
220
+ case ScalarType.INT64:
221
+ if (longType === LongType.STRING) {
222
+ return "string";
223
+ }
224
+ return "bigint";
225
+ case ScalarType.BYTES:
226
+ return "Uint8Array";
227
+ default:
228
+ return "number";
229
+ }
230
+ }
@@ -0,0 +1,26 @@
1
+ import { DescEnum, DescExtension, DescMessage, LongType, ScalarType } from "@bufbuild/protobuf";
2
+ export type LiteralProtoInt64 = {
3
+ readonly kind: "es_proto_int64";
4
+ type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
5
+ longType: LongType;
6
+ value: bigint | string;
7
+ };
8
+ export type LiteralString = {
9
+ readonly kind: "es_string";
10
+ value: string;
11
+ };
12
+ export type RefDescMessage = {
13
+ readonly kind: "es_ref_message";
14
+ type: DescMessage;
15
+ typeOnly: boolean;
16
+ };
17
+ export type RefDescEnum = {
18
+ readonly kind: "es_ref_enum";
19
+ type: DescEnum;
20
+ typeOnly: boolean;
21
+ };
22
+ export type ExportDeclaration = {
23
+ readonly kind: "es_export_decl";
24
+ declaration: string;
25
+ name: string | DescMessage | DescEnum | DescExtension;
26
+ };