@bufbuild/protoplugin 1.5.1 → 1.7.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 (64) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/create-es-plugin.d.ts +23 -3
  3. package/dist/cjs/create-es-plugin.js +26 -135
  4. package/dist/cjs/ecmascript/custom-options.d.ts +8 -0
  5. package/dist/cjs/ecmascript/custom-options.js +7 -1
  6. package/dist/cjs/ecmascript/export-declaration.d.ts +7 -0
  7. package/dist/cjs/ecmascript/export-declaration.js +24 -0
  8. package/dist/cjs/ecmascript/gencommon.d.ts +4 -5
  9. package/dist/cjs/ecmascript/gencommon.js +2 -76
  10. package/dist/cjs/ecmascript/generated-file.d.ts +48 -13
  11. package/dist/cjs/ecmascript/generated-file.js +82 -19
  12. package/dist/cjs/ecmascript/import-path.js +1 -1
  13. package/dist/cjs/ecmascript/import-symbol.js +1 -1
  14. package/dist/cjs/ecmascript/index.d.ts +22 -7
  15. package/dist/cjs/ecmascript/index.js +31 -9
  16. package/dist/cjs/ecmascript/jsdoc.d.ts +8 -0
  17. package/dist/cjs/ecmascript/jsdoc.js +93 -0
  18. package/dist/cjs/ecmascript/parameter.d.ts +13 -0
  19. package/dist/cjs/ecmascript/parameter.js +161 -0
  20. package/dist/cjs/ecmascript/reify-wkt.js +1 -1
  21. package/dist/cjs/ecmascript/runtime-imports.d.ts +1 -0
  22. package/dist/cjs/ecmascript/runtime-imports.js +2 -1
  23. package/dist/cjs/ecmascript/schema.d.ts +5 -6
  24. package/dist/cjs/ecmascript/schema.js +24 -37
  25. package/dist/cjs/ecmascript/target.js +1 -1
  26. package/dist/cjs/ecmascript/transpile.d.ts +1 -1
  27. package/dist/cjs/ecmascript/transpile.js +5 -2
  28. package/dist/cjs/error.js +1 -1
  29. package/dist/cjs/index.d.ts +5 -1
  30. package/dist/cjs/index.js +1 -1
  31. package/dist/cjs/plugin.js +1 -1
  32. package/dist/cjs/run-node.js +1 -1
  33. package/dist/esm/create-es-plugin.d.ts +23 -3
  34. package/dist/esm/create-es-plugin.js +27 -136
  35. package/dist/esm/ecmascript/custom-options.d.ts +8 -0
  36. package/dist/esm/ecmascript/custom-options.js +7 -1
  37. package/dist/esm/ecmascript/export-declaration.d.ts +7 -0
  38. package/dist/esm/ecmascript/export-declaration.js +20 -0
  39. package/dist/esm/ecmascript/gencommon.d.ts +4 -5
  40. package/dist/esm/ecmascript/gencommon.js +1 -73
  41. package/dist/esm/ecmascript/generated-file.d.ts +48 -13
  42. package/dist/esm/ecmascript/generated-file.js +83 -20
  43. package/dist/esm/ecmascript/import-path.js +1 -1
  44. package/dist/esm/ecmascript/import-symbol.js +1 -1
  45. package/dist/esm/ecmascript/index.d.ts +22 -7
  46. package/dist/esm/ecmascript/index.js +24 -3
  47. package/dist/esm/ecmascript/jsdoc.d.ts +8 -0
  48. package/dist/esm/ecmascript/jsdoc.js +89 -0
  49. package/dist/esm/ecmascript/parameter.d.ts +13 -0
  50. package/dist/esm/ecmascript/parameter.js +157 -0
  51. package/dist/esm/ecmascript/reify-wkt.js +1 -1
  52. package/dist/esm/ecmascript/runtime-imports.d.ts +1 -0
  53. package/dist/esm/ecmascript/runtime-imports.js +2 -1
  54. package/dist/esm/ecmascript/schema.d.ts +5 -6
  55. package/dist/esm/ecmascript/schema.js +24 -36
  56. package/dist/esm/ecmascript/target.js +1 -1
  57. package/dist/esm/ecmascript/transpile.d.ts +1 -1
  58. package/dist/esm/ecmascript/transpile.js +5 -2
  59. package/dist/esm/error.js +1 -1
  60. package/dist/esm/index.d.ts +5 -1
  61. package/dist/esm/index.js +1 -1
  62. package/dist/esm/plugin.js +1 -1
  63. package/dist/esm/run-node.js +1 -1
  64. package/package.json +2 -2
@@ -1,10 +1,12 @@
1
- import type { DescEnum, DescFile, DescMessage } from "@bufbuild/protobuf";
1
+ import type { 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
+ import type { JSDocBlock } from "./jsdoc.js";
4
6
  /**
5
7
  * All types that can be passed to GeneratedFile.print()
6
8
  */
7
- export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | DescMessage | DescEnum | Printable[];
9
+ export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | DescMessage | DescEnum | DescExtension | Printable[];
8
10
  /**
9
11
  * FileInfo represents an intermediate type using for transpiling TypeScript internally.
10
12
  */
@@ -48,13 +50,45 @@ export interface GeneratedFile {
48
50
  */
49
51
  print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
50
52
  /**
51
- * Reserves an identifier in this file.
53
+ * @deprecated Please use createImportSymbol() from @bufbuild/protoplugin/ecmascript instead
52
54
  */
53
55
  export(name: string): ImportSymbol;
56
+ /**
57
+ * Create a string literal.
58
+ */
59
+ string(string: string): Printable;
60
+ /**
61
+ * Create a JSDoc comment block with the given text. Line breaks and white-space
62
+ * stay intact.
63
+ */
64
+ jsDoc(text: string, indentation?: string): JSDocBlock;
65
+ /**
66
+ * Create a JSDoc comment block for the given message, enumeration, or other
67
+ * descriptor. The comment block will contain the original comments from the
68
+ * protobuf source, and annotations such as `@generated from message MyMessage`.
69
+ */
70
+ jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
71
+ /**
72
+ * Create a printable export statement. For example:
73
+ *
74
+ * ```ts
75
+ * f.print(f.exportDecl("abstract class", "MyClass"), " {}")
76
+ * ```
77
+ *
78
+ * Will generate as:
79
+ * ```ts
80
+ * export abstract class MyClass {}
81
+ * ```
82
+ *
83
+ * Using this method is preferred over a calling print() with a literal export
84
+ * statement. If the plugin option `js_import_style=legacy_commonjs` is set,
85
+ * exports will automatically be generated for CommonJS.
86
+ */
87
+ exportDecl(declaration: string, name: string | DescMessage | DescEnum | DescExtension): Printable;
54
88
  /**
55
89
  * Import a message or enumeration generated by protoc-gen-es.
56
90
  */
57
- import(type: DescMessage | DescEnum): ImportSymbol;
91
+ import(type: DescMessage | DescEnum | DescExtension): ImportSymbol;
58
92
  /**
59
93
  * Import any symbol from a file or package.
60
94
  *
@@ -66,16 +100,17 @@ export interface GeneratedFile {
66
100
  * relative to the current file.
67
101
  */
68
102
  import(name: string, from: string): ImportSymbol;
103
+ /**
104
+ * In case you need full control over exports and imports, use print() and
105
+ * formulate your own imports and exports based on this property.
106
+ */
107
+ readonly jsImportStyle: "module" | "legacy_commonjs";
69
108
  }
70
- export interface GenerateFileToFileInfo {
71
- getFileInfo(): FileInfo | undefined;
109
+ export interface GeneratedFileController extends GeneratedFile {
110
+ getFileInfo(): FileInfo;
72
111
  }
73
- type CreateTypeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;
112
+ type CreateTypeImportFn = (desc: DescMessage | DescEnum | DescExtension) => ImportSymbol;
74
113
  type RewriteImportPathFn = (path: string) => string;
75
- export declare function createGeneratedFile(name: string, importPath: string, rewriteImportPath: RewriteImportPathFn, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, preambleSettings: {
76
- pluginName: string;
77
- pluginVersion: string;
78
- pluginParameter: string;
79
- tsNocheck: boolean;
80
- }, keepEmpty: boolean): GeneratedFile & GenerateFileToFileInfo;
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;
81
116
  export {};
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -12,14 +12,16 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { createImportSymbol } from "./import-symbol.js";
15
- import { literalString, makeFilePreamble } from "./gencommon.js";
15
+ import { literalString } from "./gencommon.js";
16
16
  import { makeImportPathRelative } from "./import-path.js";
17
- export function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
17
+ import { createExportDeclaration } from "./export-declaration.js";
18
+ import { createJsDocBlock } from "./jsdoc.js";
19
+ export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath, createTypeImport, runtimeImports, createPreamble) {
18
20
  let preamble;
19
21
  const el = [];
20
22
  return {
21
23
  preamble(file) {
22
- preamble = makeFilePreamble(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.pluginParameter, preambleSettings.tsNocheck);
24
+ preamble = createPreamble(file);
23
25
  },
24
26
  print(printableOrFragments, ...rest) {
25
27
  let printables;
@@ -41,6 +43,15 @@ export function createGeneratedFile(name, importPath, rewriteImportPath, createT
41
43
  export(name) {
42
44
  return createImportSymbol(name, importPath);
43
45
  },
46
+ exportDecl(declaration, name) {
47
+ return createExportDeclaration(declaration, name);
48
+ },
49
+ string(string) {
50
+ return literalString(string);
51
+ },
52
+ jsDoc(textOrDesc, indentation) {
53
+ return createJsDocBlock(textOrDesc, indentation);
54
+ },
44
55
  import(typeOrName, from) {
45
56
  if (typeof typeOrName == "string") {
46
57
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -48,42 +59,81 @@ export function createGeneratedFile(name, importPath, rewriteImportPath, createT
48
59
  }
49
60
  return createTypeImport(typeOrName);
50
61
  },
62
+ jsImportStyle,
51
63
  getFileInfo() {
52
- const content = elToContent(el, importPath, rewriteImportPath);
53
- if (!keepEmpty && content.length === 0) {
54
- return;
55
- }
56
64
  return {
57
65
  name,
58
- content,
66
+ content: elToContent(el, importPath, rewriteImportPath, jsImportStyle == "legacy_commonjs"),
59
67
  preamble,
60
68
  };
61
69
  },
62
70
  };
63
71
  }
64
- function elToContent(el, importerPath, rewriteImportPath) {
72
+ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
73
+ if (el.length == 0) {
74
+ return "";
75
+ }
65
76
  const c = [];
77
+ if (legacyCommonJs) {
78
+ c.push(`"use strict";\n`);
79
+ c.push(`Object.defineProperty(exports, "__esModule", { value: true });\n`);
80
+ c.push(`\n`);
81
+ }
66
82
  const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
67
- const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
68
- const what = `{ ${p.join(", ")} }`;
69
- if (typeOnly) {
70
- c.push(`import type ${what} from ${literalString(from)};\n`);
83
+ if (legacyCommonJs) {
84
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name}: ${alias}`);
85
+ const what = `{ ${p.join(", ")} }`;
86
+ c.push(`const ${what} = require(${literalString(from)});\n`);
71
87
  }
72
88
  else {
73
- c.push(`import ${what} from ${literalString(from)};\n`);
89
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
90
+ const what = `{ ${p.join(", ")} }`;
91
+ if (typeOnly) {
92
+ c.push(`import type ${what} from ${literalString(from)};\n`);
93
+ }
94
+ else {
95
+ c.push(`import ${what} from ${literalString(from)};\n`);
96
+ }
74
97
  }
75
98
  });
76
99
  if (c.length > 0) {
77
100
  c.push("\n");
78
101
  }
102
+ const legacyCommonJsExportNames = [];
79
103
  for (const e of el) {
80
104
  if (typeof e == "string") {
81
105
  c.push(e);
82
- continue;
83
106
  }
84
- const ident = symbolToIdentifier.get(e.id);
85
- if (ident != undefined) {
86
- c.push(ident);
107
+ else {
108
+ switch (e.kind) {
109
+ case "es_symbol": {
110
+ const ident = symbolToIdentifier.get(e.id);
111
+ if (ident != undefined) {
112
+ c.push(ident);
113
+ }
114
+ break;
115
+ }
116
+ case "es_export_stmt":
117
+ if (legacyCommonJs) {
118
+ legacyCommonJsExportNames.push(e.name);
119
+ }
120
+ else {
121
+ c.push("export ");
122
+ }
123
+ if (e.declaration !== undefined && e.declaration.length > 0) {
124
+ c.push(e.declaration, " ");
125
+ }
126
+ c.push(e.name);
127
+ break;
128
+ }
129
+ }
130
+ }
131
+ if (legacyCommonJs) {
132
+ if (legacyCommonJsExportNames.length > 0) {
133
+ c.push(`\n`);
134
+ }
135
+ for (const name of legacyCommonJsExportNames) {
136
+ c.push(`exports.`, name, " = ", name, ";\n");
87
137
  }
88
138
  }
89
139
  return c.join("");
@@ -116,7 +166,20 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
116
166
  case "es_symbol":
117
167
  el.push(p);
118
168
  break;
169
+ case "es_jsdoc":
170
+ el.push(p.toString());
171
+ break;
172
+ case "es_export_decl":
173
+ el.push({
174
+ kind: "es_export_stmt",
175
+ declaration: p.declaration,
176
+ name: typeof p.name == "string"
177
+ ? p.name
178
+ : createTypeImport(p.name).name,
179
+ });
180
+ break;
119
181
  case "message":
182
+ case "extension":
120
183
  case "enum":
121
184
  el.push(createTypeImport(p));
122
185
  break;
@@ -149,7 +212,7 @@ function processImports(el, importerPath, rewriteImportPath, makeImportStatement
149
212
  const foreignSymbols = [];
150
213
  // Walk through all symbols used and populate the collections above.
151
214
  for (const s of el) {
152
- if (typeof s == "string") {
215
+ if (typeof s != "object" || s.kind !== "es_symbol") {
153
216
  continue;
154
217
  }
155
218
  symbolToIdentifier.set(s.id, s.name);
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -1,9 +1,24 @@
1
+ import { AnyDesc, DescExtension, DescFile } from "@bufbuild/protobuf";
2
+ import { Printable } from "./generated-file.js";
1
3
  export { reifyWkt } from "./reify-wkt.js";
2
- export { Target } from "./target.js";
3
- export { Schema } from "./schema.js";
4
- export { RuntimeImports } from "./runtime-imports.js";
5
- export { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
6
- export { ImportSymbol } from "./import-symbol.js";
7
- export declare const localName: (desc: import("@bufbuild/protobuf").DescEnum | import("@bufbuild/protobuf").DescMessage | import("@bufbuild/protobuf").DescService | import("@bufbuild/protobuf").DescEnumValue | import("@bufbuild/protobuf").DescField | import("@bufbuild/protobuf").DescOneof | import("@bufbuild/protobuf").DescMethod) => string;
8
- export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
4
+ export type { Target } from "./target.js";
5
+ export type { Schema } from "./schema.js";
6
+ export type { RuntimeImports } from "./runtime-imports.js";
7
+ export type { GeneratedFile, FileInfo, Printable } from "./generated-file.js";
8
+ export type { ImportSymbol } from "./import-symbol.js";
9
+ 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, } from "./gencommon.js";
9
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;
17
+ /**
18
+ * @deprecated Please use GeneratedFile.jsDoc() instead
19
+ */
20
+ export declare function makeJsDoc(desc: Exclude<AnyDesc, DescFile | DescExtension>, indentation?: string): Printable;
21
+ /**
22
+ * @deprecated Please use GeneratedFile.jsDoc() instead
23
+ */
24
+ export declare function createJsDocBlock(text: string, indentation?: string): Printable;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -11,8 +11,29 @@
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 } from "@bufbuild/protobuf";
14
+ import { codegenInfo, } from "@bufbuild/protobuf";
15
+ import { createJsDocBlock as createJsDocBlockInternal } from "./jsdoc.js";
16
+ import { literalString as literalStringInternal } from "./gencommon.js";
15
17
  export { reifyWkt } from "./reify-wkt.js";
18
+ export { createImportSymbol } from "./import-symbol.js";
16
19
  export const { localName } = codegenInfo;
17
- export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
20
+ export { getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, } from "./gencommon.js";
18
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
+ }
28
+ /**
29
+ * @deprecated Please use GeneratedFile.jsDoc() instead
30
+ */
31
+ export function makeJsDoc(desc, indentation = "") {
32
+ return createJsDocBlockInternal(desc, indentation).toString();
33
+ }
34
+ /**
35
+ * @deprecated Please use GeneratedFile.jsDoc() instead
36
+ */
37
+ export function createJsDocBlock(text, indentation = "") {
38
+ return createJsDocBlockInternal(text, indentation).toString();
39
+ }
@@ -0,0 +1,8 @@
1
+ import type { AnyDesc, DescFile } from "@bufbuild/protobuf";
2
+ export type JSDocBlock = {
3
+ readonly kind: "es_jsdoc";
4
+ text: string;
5
+ indentation?: string;
6
+ toString(): string;
7
+ };
8
+ export declare function createJsDocBlock(textOrDesc: string | Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
@@ -0,0 +1,89 @@
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
+ export function createJsDocBlock(textOrDesc, indentation) {
15
+ const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
16
+ return {
17
+ kind: "es_jsdoc",
18
+ text,
19
+ indentation,
20
+ toString() {
21
+ if (text.trim().length == 0) {
22
+ return "";
23
+ }
24
+ let lines = text.split("\n");
25
+ if (lines.length === 0) {
26
+ return "";
27
+ }
28
+ lines = lines.map((l) => l.split("*/").join("*\\/"));
29
+ lines = lines.map((l) => (l.length > 0 ? " " + l : l));
30
+ const i = indentation !== null && indentation !== void 0 ? indentation : "";
31
+ return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
32
+ },
33
+ };
34
+ }
35
+ function createTextForDesc(desc) {
36
+ var _a, _b;
37
+ const comments = desc.getComments();
38
+ let text = "";
39
+ if (comments.leading !== undefined) {
40
+ text += comments.leading;
41
+ if (text.endsWith("\n")) {
42
+ text = text.substring(0, text.length - 1);
43
+ }
44
+ }
45
+ if (comments.trailing !== undefined) {
46
+ if (text.length > 0) {
47
+ text += "\n\n";
48
+ }
49
+ text += comments.trailing;
50
+ if (text.endsWith("\n")) {
51
+ text = text.substring(0, text.length - 1);
52
+ }
53
+ }
54
+ if (text.length > 0) {
55
+ text += "\n\n";
56
+ }
57
+ text = text
58
+ .split("\n")
59
+ .map((line) => (line.startsWith(" ") ? line.substring(1) : line))
60
+ .join("\n");
61
+ switch (desc.kind) {
62
+ case "enum_value":
63
+ text += `@generated from enum value: ${desc.declarationString()};`;
64
+ break;
65
+ case "field":
66
+ text += `@generated from field: ${desc.declarationString()};`;
67
+ break;
68
+ case "extension":
69
+ text += `@generated from extension: ${desc.declarationString()};`;
70
+ break;
71
+ default:
72
+ text += `@generated from ${desc.toString()}`;
73
+ break;
74
+ }
75
+ let deprecated = desc.deprecated;
76
+ switch (desc.kind) {
77
+ case "enum":
78
+ case "message":
79
+ case "service":
80
+ deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
81
+ break;
82
+ default:
83
+ break;
84
+ }
85
+ if (deprecated) {
86
+ text += "\n@deprecated";
87
+ }
88
+ return text;
89
+ }
@@ -0,0 +1,13 @@
1
+ import { Target } from "./target.js";
2
+ import { RewriteImports } from "./import-path.js";
3
+ export interface ParsedParameter {
4
+ targets: Target[];
5
+ tsNocheck: boolean;
6
+ bootstrapWkt: boolean;
7
+ keepEmptyFiles: boolean;
8
+ rewriteImports: RewriteImports;
9
+ importExtension: string;
10
+ jsImportStyle: "module" | "legacy_commonjs";
11
+ sanitizedParameter: string;
12
+ }
13
+ export declare function parseParameter(parameter: string | undefined, parseExtraOption: ((key: string, value: string) => void) | undefined): ParsedParameter;
@@ -0,0 +1,157 @@
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 { PluginOptionError } from "../error.js";
15
+ export function parseParameter(parameter, parseExtraOption) {
16
+ let targets = ["js", "dts"];
17
+ let tsNocheck = true;
18
+ let bootstrapWkt = false;
19
+ let keepEmptyFiles = false;
20
+ const rewriteImports = [];
21
+ let importExtension = ".js";
22
+ let jsImportStyle = "module";
23
+ const rawParameters = [];
24
+ for (const { key, value, raw } of splitParameter(parameter)) {
25
+ // Whether this key/value plugin parameter pair should be
26
+ // printed to the generated file preamble
27
+ let sanitize = false;
28
+ switch (key) {
29
+ case "target":
30
+ targets = [];
31
+ for (const rawTarget of value.split("+")) {
32
+ switch (rawTarget) {
33
+ case "js":
34
+ case "ts":
35
+ case "dts":
36
+ if (targets.indexOf(rawTarget) < 0) {
37
+ targets.push(rawTarget);
38
+ }
39
+ break;
40
+ default:
41
+ throw new PluginOptionError(raw);
42
+ }
43
+ }
44
+ value.split("+");
45
+ break;
46
+ case "ts_nocheck":
47
+ switch (value) {
48
+ case "true":
49
+ case "1":
50
+ tsNocheck = true;
51
+ break;
52
+ case "false":
53
+ case "0":
54
+ tsNocheck = false;
55
+ break;
56
+ default:
57
+ throw new PluginOptionError(raw);
58
+ }
59
+ break;
60
+ case "bootstrap_wkt":
61
+ switch (value) {
62
+ case "true":
63
+ case "1":
64
+ bootstrapWkt = true;
65
+ break;
66
+ case "false":
67
+ case "0":
68
+ bootstrapWkt = false;
69
+ break;
70
+ default:
71
+ throw new PluginOptionError(raw);
72
+ }
73
+ break;
74
+ case "rewrite_imports": {
75
+ const parts = value.split(":");
76
+ if (parts.length !== 2) {
77
+ throw new PluginOptionError(raw, "must be in the form of <pattern>:<target>");
78
+ }
79
+ const [pattern, target] = parts;
80
+ rewriteImports.push({ pattern, target });
81
+ // rewrite_imports can be noisy and is more of an implementation detail
82
+ // so we strip it out of the preamble
83
+ sanitize = true;
84
+ break;
85
+ }
86
+ case "import_extension": {
87
+ importExtension = value === "none" ? "" : value;
88
+ break;
89
+ }
90
+ case "js_import_style":
91
+ switch (value) {
92
+ case "module":
93
+ jsImportStyle = value;
94
+ break;
95
+ case "legacy_commonjs":
96
+ jsImportStyle = value;
97
+ break;
98
+ default:
99
+ throw new PluginOptionError(raw);
100
+ }
101
+ break;
102
+ case "keep_empty_files": {
103
+ switch (value) {
104
+ case "true":
105
+ case "1":
106
+ keepEmptyFiles = true;
107
+ break;
108
+ case "false":
109
+ case "0":
110
+ keepEmptyFiles = false;
111
+ break;
112
+ default:
113
+ throw new PluginOptionError(raw);
114
+ }
115
+ break;
116
+ }
117
+ default:
118
+ if (parseExtraOption === undefined) {
119
+ throw new PluginOptionError(raw);
120
+ }
121
+ try {
122
+ parseExtraOption(key, value);
123
+ }
124
+ catch (e) {
125
+ throw new PluginOptionError(raw, e);
126
+ }
127
+ break;
128
+ }
129
+ if (!sanitize) {
130
+ rawParameters.push(raw);
131
+ }
132
+ }
133
+ const sanitizedParameter = rawParameters.join(",");
134
+ return {
135
+ targets,
136
+ tsNocheck,
137
+ bootstrapWkt,
138
+ rewriteImports,
139
+ importExtension,
140
+ jsImportStyle,
141
+ keepEmptyFiles,
142
+ sanitizedParameter,
143
+ };
144
+ }
145
+ function splitParameter(parameter) {
146
+ if (parameter == undefined) {
147
+ return [];
148
+ }
149
+ return parameter.split(",").map((raw) => {
150
+ const i = raw.indexOf("=");
151
+ return {
152
+ key: i === -1 ? raw : raw.substring(0, i),
153
+ value: i === -1 ? "" : raw.substring(i + 1),
154
+ raw,
155
+ };
156
+ });
157
+ }
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -7,6 +7,7 @@ export interface RuntimeImports {
7
7
  PlainMessage: ImportSymbol;
8
8
  FieldList: ImportSymbol;
9
9
  MessageType: ImportSymbol;
10
+ Extension: ImportSymbol;
10
11
  BinaryReadOptions: ImportSymbol;
11
12
  BinaryWriteOptions: ImportSymbol;
12
13
  JsonReadOptions: ImportSymbol;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ export function createRuntimeImports(bootstrapWkt) {
23
23
  PlainMessage: infoToSymbol("PlainMessage", bootstrapWkt),
24
24
  FieldList: infoToSymbol("FieldList", bootstrapWkt),
25
25
  MessageType: infoToSymbol("MessageType", bootstrapWkt),
26
+ Extension: infoToSymbol("Extension", bootstrapWkt),
26
27
  BinaryReadOptions: infoToSymbol("BinaryReadOptions", bootstrapWkt),
27
28
  BinaryWriteOptions: infoToSymbol("BinaryWriteOptions", bootstrapWkt),
28
29
  JsonReadOptions: infoToSymbol("JsonReadOptions", bootstrapWkt),