@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,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -17,12 +17,14 @@ exports.createGeneratedFile = void 0;
17
17
  const import_symbol_js_1 = require("./import-symbol.js");
18
18
  const gencommon_js_1 = require("./gencommon.js");
19
19
  const import_path_js_1 = require("./import-path.js");
20
- function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImport, runtimeImports, preambleSettings, keepEmpty) {
20
+ const export_declaration_js_1 = require("./export-declaration.js");
21
+ const jsdoc_js_1 = require("./jsdoc.js");
22
+ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath, createTypeImport, runtimeImports, createPreamble) {
21
23
  let preamble;
22
24
  const el = [];
23
25
  return {
24
26
  preamble(file) {
25
- preamble = (0, gencommon_js_1.makeFilePreamble)(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.pluginParameter, preambleSettings.tsNocheck);
27
+ preamble = createPreamble(file);
26
28
  },
27
29
  print(printableOrFragments, ...rest) {
28
30
  let printables;
@@ -44,6 +46,15 @@ function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImpo
44
46
  export(name) {
45
47
  return (0, import_symbol_js_1.createImportSymbol)(name, importPath);
46
48
  },
49
+ exportDecl(declaration, name) {
50
+ return (0, export_declaration_js_1.createExportDeclaration)(declaration, name);
51
+ },
52
+ string(string) {
53
+ return (0, gencommon_js_1.literalString)(string);
54
+ },
55
+ jsDoc(textOrDesc, indentation) {
56
+ return (0, jsdoc_js_1.createJsDocBlock)(textOrDesc, indentation);
57
+ },
47
58
  import(typeOrName, from) {
48
59
  if (typeof typeOrName == "string") {
49
60
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -51,43 +62,82 @@ function createGeneratedFile(name, importPath, rewriteImportPath, createTypeImpo
51
62
  }
52
63
  return createTypeImport(typeOrName);
53
64
  },
65
+ jsImportStyle,
54
66
  getFileInfo() {
55
- const content = elToContent(el, importPath, rewriteImportPath);
56
- if (!keepEmpty && content.length === 0) {
57
- return;
58
- }
59
67
  return {
60
68
  name,
61
- content,
69
+ content: elToContent(el, importPath, rewriteImportPath, jsImportStyle == "legacy_commonjs"),
62
70
  preamble,
63
71
  };
64
72
  },
65
73
  };
66
74
  }
67
75
  exports.createGeneratedFile = createGeneratedFile;
68
- function elToContent(el, importerPath, rewriteImportPath) {
76
+ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
77
+ if (el.length == 0) {
78
+ return "";
79
+ }
69
80
  const c = [];
81
+ if (legacyCommonJs) {
82
+ c.push(`"use strict";\n`);
83
+ c.push(`Object.defineProperty(exports, "__esModule", { value: true });\n`);
84
+ c.push(`\n`);
85
+ }
70
86
  const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
71
- const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
72
- const what = `{ ${p.join(", ")} }`;
73
- if (typeOnly) {
74
- c.push(`import type ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
87
+ if (legacyCommonJs) {
88
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name}: ${alias}`);
89
+ const what = `{ ${p.join(", ")} }`;
90
+ c.push(`const ${what} = require(${(0, gencommon_js_1.literalString)(from)});\n`);
75
91
  }
76
92
  else {
77
- c.push(`import ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
93
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
94
+ const what = `{ ${p.join(", ")} }`;
95
+ if (typeOnly) {
96
+ c.push(`import type ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
97
+ }
98
+ else {
99
+ c.push(`import ${what} from ${(0, gencommon_js_1.literalString)(from)};\n`);
100
+ }
78
101
  }
79
102
  });
80
103
  if (c.length > 0) {
81
104
  c.push("\n");
82
105
  }
106
+ const legacyCommonJsExportNames = [];
83
107
  for (const e of el) {
84
108
  if (typeof e == "string") {
85
109
  c.push(e);
86
- continue;
87
110
  }
88
- const ident = symbolToIdentifier.get(e.id);
89
- if (ident != undefined) {
90
- c.push(ident);
111
+ else {
112
+ switch (e.kind) {
113
+ case "es_symbol": {
114
+ const ident = symbolToIdentifier.get(e.id);
115
+ if (ident != undefined) {
116
+ c.push(ident);
117
+ }
118
+ break;
119
+ }
120
+ case "es_export_stmt":
121
+ if (legacyCommonJs) {
122
+ legacyCommonJsExportNames.push(e.name);
123
+ }
124
+ else {
125
+ c.push("export ");
126
+ }
127
+ if (e.declaration !== undefined && e.declaration.length > 0) {
128
+ c.push(e.declaration, " ");
129
+ }
130
+ c.push(e.name);
131
+ break;
132
+ }
133
+ }
134
+ }
135
+ if (legacyCommonJs) {
136
+ if (legacyCommonJsExportNames.length > 0) {
137
+ c.push(`\n`);
138
+ }
139
+ for (const name of legacyCommonJsExportNames) {
140
+ c.push(`exports.`, name, " = ", name, ";\n");
91
141
  }
92
142
  }
93
143
  return c.join("");
@@ -120,7 +170,20 @@ function printableToEl(printables, el, createTypeImport, runtimeImports) {
120
170
  case "es_symbol":
121
171
  el.push(p);
122
172
  break;
173
+ case "es_jsdoc":
174
+ el.push(p.toString());
175
+ break;
176
+ case "es_export_decl":
177
+ el.push({
178
+ kind: "es_export_stmt",
179
+ declaration: p.declaration,
180
+ name: typeof p.name == "string"
181
+ ? p.name
182
+ : createTypeImport(p.name).name,
183
+ });
184
+ break;
123
185
  case "message":
186
+ case "extension":
124
187
  case "enum":
125
188
  el.push(createTypeImport(p));
126
189
  break;
@@ -153,7 +216,7 @@ function processImports(el, importerPath, rewriteImportPath, makeImportStatement
153
216
  const foreignSymbols = [];
154
217
  // Walk through all symbols used and populate the collections above.
155
218
  for (const s of el) {
156
- if (typeof s == "string") {
219
+ if (typeof s != "object" || s.kind !== "es_symbol") {
157
220
  continue;
158
221
  }
159
222
  symbolToIdentifier.set(s.id, s.name);
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // 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,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -13,19 +13,41 @@
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.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.literalString = exports.makeJsDoc = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.createJsDocBlock = exports.localName = exports.reifyWkt = void 0;
16
+ exports.createJsDocBlock = exports.makeJsDoc = exports.literalString = exports.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = exports.getFieldTyping = exports.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.localName = exports.createImportSymbol = exports.reifyWkt = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
+ const jsdoc_js_1 = require("./jsdoc.js");
19
+ const gencommon_js_1 = require("./gencommon.js");
18
20
  var reify_wkt_js_1 = require("./reify-wkt.js");
19
21
  Object.defineProperty(exports, "reifyWkt", { enumerable: true, get: function () { return reify_wkt_js_1.reifyWkt; } });
22
+ var import_symbol_js_1 = require("./import-symbol.js");
23
+ Object.defineProperty(exports, "createImportSymbol", { enumerable: true, get: function () { return import_symbol_js_1.createImportSymbol; } });
20
24
  exports.localName = protobuf_1.codegenInfo.localName;
21
- var gencommon_js_1 = require("./gencommon.js");
22
- Object.defineProperty(exports, "createJsDocBlock", { enumerable: true, get: function () { return gencommon_js_1.createJsDocBlock; } });
23
- Object.defineProperty(exports, "getFieldExplicitDefaultValue", { enumerable: true, get: function () { return gencommon_js_1.getFieldExplicitDefaultValue; } });
24
- Object.defineProperty(exports, "getFieldIntrinsicDefaultValue", { enumerable: true, get: function () { return gencommon_js_1.getFieldIntrinsicDefaultValue; } });
25
- Object.defineProperty(exports, "getFieldTyping", { enumerable: true, get: function () { return gencommon_js_1.getFieldTyping; } });
26
- Object.defineProperty(exports, "makeJsDoc", { enumerable: true, get: function () { return gencommon_js_1.makeJsDoc; } });
27
- Object.defineProperty(exports, "literalString", { enumerable: true, get: function () { return gencommon_js_1.literalString; } });
25
+ var gencommon_js_2 = require("./gencommon.js");
26
+ Object.defineProperty(exports, "getFieldExplicitDefaultValue", { enumerable: true, get: function () { return gencommon_js_2.getFieldExplicitDefaultValue; } });
27
+ Object.defineProperty(exports, "getFieldIntrinsicDefaultValue", { enumerable: true, get: function () { return gencommon_js_2.getFieldIntrinsicDefaultValue; } });
28
+ Object.defineProperty(exports, "getFieldTyping", { enumerable: true, get: function () { return gencommon_js_2.getFieldTyping; } });
28
29
  var custom_options_js_1 = require("./custom-options.js");
29
30
  Object.defineProperty(exports, "findCustomScalarOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomScalarOption; } });
30
31
  Object.defineProperty(exports, "findCustomMessageOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomMessageOption; } });
31
32
  Object.defineProperty(exports, "findCustomEnumOption", { enumerable: true, get: function () { return custom_options_js_1.findCustomEnumOption; } });
33
+ /**
34
+ * @deprecated Please use GeneratedFile.string() instead
35
+ */
36
+ function literalString(value) {
37
+ return (0, gencommon_js_1.literalString)(value);
38
+ }
39
+ exports.literalString = literalString;
40
+ /**
41
+ * @deprecated Please use GeneratedFile.jsDoc() instead
42
+ */
43
+ function makeJsDoc(desc, indentation = "") {
44
+ return (0, jsdoc_js_1.createJsDocBlock)(desc, indentation).toString();
45
+ }
46
+ exports.makeJsDoc = makeJsDoc;
47
+ /**
48
+ * @deprecated Please use GeneratedFile.jsDoc() instead
49
+ */
50
+ function createJsDocBlock(text, indentation = "") {
51
+ return (0, jsdoc_js_1.createJsDocBlock)(text, indentation).toString();
52
+ }
53
+ exports.createJsDocBlock = createJsDocBlock;
@@ -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,93 @@
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.createJsDocBlock = void 0;
17
+ function createJsDocBlock(textOrDesc, indentation) {
18
+ const text = typeof textOrDesc == "string" ? textOrDesc : createTextForDesc(textOrDesc);
19
+ return {
20
+ kind: "es_jsdoc",
21
+ text,
22
+ indentation,
23
+ toString() {
24
+ if (text.trim().length == 0) {
25
+ return "";
26
+ }
27
+ let lines = text.split("\n");
28
+ if (lines.length === 0) {
29
+ return "";
30
+ }
31
+ lines = lines.map((l) => l.split("*/").join("*\\/"));
32
+ lines = lines.map((l) => (l.length > 0 ? " " + l : l));
33
+ const i = indentation !== null && indentation !== void 0 ? indentation : "";
34
+ return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
35
+ },
36
+ };
37
+ }
38
+ exports.createJsDocBlock = createJsDocBlock;
39
+ function createTextForDesc(desc) {
40
+ var _a, _b;
41
+ const comments = desc.getComments();
42
+ let text = "";
43
+ if (comments.leading !== undefined) {
44
+ text += comments.leading;
45
+ if (text.endsWith("\n")) {
46
+ text = text.substring(0, text.length - 1);
47
+ }
48
+ }
49
+ if (comments.trailing !== undefined) {
50
+ if (text.length > 0) {
51
+ text += "\n\n";
52
+ }
53
+ text += comments.trailing;
54
+ if (text.endsWith("\n")) {
55
+ text = text.substring(0, text.length - 1);
56
+ }
57
+ }
58
+ if (text.length > 0) {
59
+ text += "\n\n";
60
+ }
61
+ text = text
62
+ .split("\n")
63
+ .map((line) => (line.startsWith(" ") ? line.substring(1) : line))
64
+ .join("\n");
65
+ switch (desc.kind) {
66
+ case "enum_value":
67
+ text += `@generated from enum value: ${desc.declarationString()};`;
68
+ break;
69
+ case "field":
70
+ text += `@generated from field: ${desc.declarationString()};`;
71
+ break;
72
+ case "extension":
73
+ text += `@generated from extension: ${desc.declarationString()};`;
74
+ break;
75
+ default:
76
+ text += `@generated from ${desc.toString()}`;
77
+ break;
78
+ }
79
+ let deprecated = desc.deprecated;
80
+ switch (desc.kind) {
81
+ case "enum":
82
+ case "message":
83
+ case "service":
84
+ deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
85
+ break;
86
+ default:
87
+ break;
88
+ }
89
+ if (deprecated) {
90
+ text += "\n@deprecated";
91
+ }
92
+ return text;
93
+ }
@@ -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,161 @@
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.parseParameter = void 0;
17
+ const error_js_1 = require("../error.js");
18
+ function parseParameter(parameter, parseExtraOption) {
19
+ let targets = ["js", "dts"];
20
+ let tsNocheck = true;
21
+ let bootstrapWkt = false;
22
+ let keepEmptyFiles = false;
23
+ const rewriteImports = [];
24
+ let importExtension = ".js";
25
+ let jsImportStyle = "module";
26
+ const rawParameters = [];
27
+ for (const { key, value, raw } of splitParameter(parameter)) {
28
+ // Whether this key/value plugin parameter pair should be
29
+ // printed to the generated file preamble
30
+ let sanitize = false;
31
+ switch (key) {
32
+ case "target":
33
+ targets = [];
34
+ for (const rawTarget of value.split("+")) {
35
+ switch (rawTarget) {
36
+ case "js":
37
+ case "ts":
38
+ case "dts":
39
+ if (targets.indexOf(rawTarget) < 0) {
40
+ targets.push(rawTarget);
41
+ }
42
+ break;
43
+ default:
44
+ throw new error_js_1.PluginOptionError(raw);
45
+ }
46
+ }
47
+ value.split("+");
48
+ break;
49
+ case "ts_nocheck":
50
+ switch (value) {
51
+ case "true":
52
+ case "1":
53
+ tsNocheck = true;
54
+ break;
55
+ case "false":
56
+ case "0":
57
+ tsNocheck = false;
58
+ break;
59
+ default:
60
+ throw new error_js_1.PluginOptionError(raw);
61
+ }
62
+ break;
63
+ case "bootstrap_wkt":
64
+ switch (value) {
65
+ case "true":
66
+ case "1":
67
+ bootstrapWkt = true;
68
+ break;
69
+ case "false":
70
+ case "0":
71
+ bootstrapWkt = false;
72
+ break;
73
+ default:
74
+ throw new error_js_1.PluginOptionError(raw);
75
+ }
76
+ break;
77
+ case "rewrite_imports": {
78
+ const parts = value.split(":");
79
+ if (parts.length !== 2) {
80
+ throw new error_js_1.PluginOptionError(raw, "must be in the form of <pattern>:<target>");
81
+ }
82
+ const [pattern, target] = parts;
83
+ rewriteImports.push({ pattern, target });
84
+ // rewrite_imports can be noisy and is more of an implementation detail
85
+ // so we strip it out of the preamble
86
+ sanitize = true;
87
+ break;
88
+ }
89
+ case "import_extension": {
90
+ importExtension = value === "none" ? "" : value;
91
+ break;
92
+ }
93
+ case "js_import_style":
94
+ switch (value) {
95
+ case "module":
96
+ jsImportStyle = value;
97
+ break;
98
+ case "legacy_commonjs":
99
+ jsImportStyle = value;
100
+ break;
101
+ default:
102
+ throw new error_js_1.PluginOptionError(raw);
103
+ }
104
+ break;
105
+ case "keep_empty_files": {
106
+ switch (value) {
107
+ case "true":
108
+ case "1":
109
+ keepEmptyFiles = true;
110
+ break;
111
+ case "false":
112
+ case "0":
113
+ keepEmptyFiles = false;
114
+ break;
115
+ default:
116
+ throw new error_js_1.PluginOptionError(raw);
117
+ }
118
+ break;
119
+ }
120
+ default:
121
+ if (parseExtraOption === undefined) {
122
+ throw new error_js_1.PluginOptionError(raw);
123
+ }
124
+ try {
125
+ parseExtraOption(key, value);
126
+ }
127
+ catch (e) {
128
+ throw new error_js_1.PluginOptionError(raw, e);
129
+ }
130
+ break;
131
+ }
132
+ if (!sanitize) {
133
+ rawParameters.push(raw);
134
+ }
135
+ }
136
+ const sanitizedParameter = rawParameters.join(",");
137
+ return {
138
+ targets,
139
+ tsNocheck,
140
+ bootstrapWkt,
141
+ rewriteImports,
142
+ importExtension,
143
+ jsImportStyle,
144
+ keepEmptyFiles,
145
+ sanitizedParameter,
146
+ };
147
+ }
148
+ exports.parseParameter = parseParameter;
149
+ function splitParameter(parameter) {
150
+ if (parameter == undefined) {
151
+ return [];
152
+ }
153
+ return parameter.split(",").map((raw) => {
154
+ const i = raw.indexOf("=");
155
+ return {
156
+ key: i === -1 ? raw : raw.substring(0, i),
157
+ value: i === -1 ? "" : raw.substring(i + 1),
158
+ raw,
159
+ };
160
+ });
161
+ }
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // 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,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ function createRuntimeImports(bootstrapWkt) {
26
26
  PlainMessage: infoToSymbol("PlainMessage", bootstrapWkt),
27
27
  FieldList: infoToSymbol("FieldList", bootstrapWkt),
28
28
  MessageType: infoToSymbol("MessageType", bootstrapWkt),
29
+ Extension: infoToSymbol("Extension", bootstrapWkt),
29
30
  BinaryReadOptions: infoToSymbol("BinaryReadOptions", bootstrapWkt),
30
31
  BinaryWriteOptions: infoToSymbol("BinaryWriteOptions", bootstrapWkt),
31
32
  JsonReadOptions: infoToSymbol("JsonReadOptions", bootstrapWkt),
@@ -1,9 +1,9 @@
1
1
  import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
2
- import { CodeGeneratorResponse } from "@bufbuild/protobuf";
2
+ import { FeatureSetDefaults } from "@bufbuild/protobuf";
3
3
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
4
4
  import { RuntimeImports } from "./runtime-imports.js";
5
5
  import type { Target } from "./target.js";
6
- import { RewriteImports } from "./import-path.js";
6
+ import { ParsedParameter } from "./parameter";
7
7
  /**
8
8
  * Schema describes the files and types that the plugin is requested to
9
9
  * generate.
@@ -34,10 +34,9 @@ export interface Schema {
34
34
  */
35
35
  readonly proto: CodeGeneratorRequest;
36
36
  }
37
- interface SchemaController {
38
- schema: Schema;
37
+ interface SchemaController extends Schema {
39
38
  getFileInfo: () => FileInfo[];
39
+ prepareGenerate(target: Target): void;
40
40
  }
41
- export declare function createSchema(request: CodeGeneratorRequest, targets: Target[], tsNocheck: boolean, bootstrapWkt: boolean, rewriteImports: RewriteImports, importExtension: string, keepEmptyFiles: boolean, pluginName: string, pluginVersion: string, pluginParameter: string): SchemaController;
42
- export declare function toResponse(files: FileInfo[]): CodeGeneratorResponse;
41
+ export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter, pluginName: string, pluginVersion: string, featureSetDefaults: FeatureSetDefaults | undefined): SchemaController;
43
42
  export {};