@bufbuild/protoplugin 2.0.0-beta.2 → 2.0.0-beta.3

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.
package/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # @bufbuild/protoplugin
2
2
 
3
- This package helps to create your own code generator plugin using the
3
+ This package helps you create your own code generator plugin using the
4
4
  Protobuf-ES plugin framework.
5
5
 
6
- **Protobuf-ES** is a complete implementation of [Protocol Buffers](https://developers.google.com/protocol-buffers) in TypeScript, suitable for web browsers and Node.js.
6
+ **Protobuf-ES** is a complete implementation of [Protocol Buffers](https://protobuf.dev) in TypeScript, suitable for
7
+ web browsers and Node.js.
7
8
 
8
- In addition to a full Protobuf runtime library, it also provides a code generator
9
- [`protoc-gen-es`](https://www.npmjs.com/package/@bufbuild/protoc-gen-es), which utilizes a plugin framework to generate base types from
10
- your Protobuf schema. It is fully compatible with both Buf and protoc compilers.
9
+ In addition to a full Protobuf runtime library, it provides the [`protoc-gen-es`](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
10
+ code generator, which uses a plugin framework to generate base types from
11
+ your Protobuf schema. It is fully compatible with both `buf` and `protoc` compilers.
11
12
 
12
13
  And now, you can write your own **Protobuf-ES** compatible plugins using this same
13
14
  plugin framework with the `@bufbuild/protoplugin` package.
@@ -31,4 +32,4 @@ ECMAScript-compliant code.
31
32
 
32
33
  ## Usage
33
34
 
34
- Get started now with our [plugin documentation](https://github.com/bufbuild/protobuf-es/blob/main/docs/writing_plugins.md).
35
+ Get started now with our [plugin documentation](https://github.com/bufbuild/protobuf-es/blob/main/MANUAL.md#writing-plugins).
@@ -13,7 +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.createEcmaScriptPlugin = void 0;
16
+ exports.createEcmaScriptPlugin = createEcmaScriptPlugin;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const protobuf_2 = require("@bufbuild/protobuf");
19
19
  const wkt_1 = require("@bufbuild/protobuf/wkt");
@@ -104,7 +104,6 @@ function createEcmaScriptPlugin(init) {
104
104
  },
105
105
  };
106
106
  }
107
- exports.createEcmaScriptPlugin = createEcmaScriptPlugin;
108
107
  function toResponse(files, minimumEdition, maximumEdition) {
109
108
  return (0, protobuf_1.create)(wkt_1.CodeGeneratorResponseSchema, {
110
109
  supportedFeatures: protobuf_1.protoInt64.parse(wkt_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL |
package/dist/cjs/error.js CHANGED
@@ -13,7 +13,9 @@
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.isPluginOptionError = exports.reasonToString = exports.PluginOptionError = void 0;
16
+ exports.PluginOptionError = void 0;
17
+ exports.reasonToString = reasonToString;
18
+ exports.isPluginOptionError = isPluginOptionError;
17
19
  class PluginOptionError extends Error {
18
20
  constructor(option, reason) {
19
21
  const detail = reason !== undefined ? reasonToString(reason) : "";
@@ -33,11 +35,9 @@ function reasonToString(reason) {
33
35
  }
34
36
  return String(reason);
35
37
  }
36
- exports.reasonToString = reasonToString;
37
38
  function isPluginOptionError(arg) {
38
39
  if (!(arg instanceof Error)) {
39
40
  return false;
40
41
  }
41
42
  return arg.name === "PluginOptionError";
42
43
  }
43
- exports.isPluginOptionError = isPluginOptionError;
@@ -13,7 +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.makeFilePreamble = void 0;
16
+ exports.makeFilePreamble = makeFilePreamble;
17
17
  const wkt_1 = require("@bufbuild/protobuf/wkt");
18
18
  const source_code_info_js_1 = require("./source-code-info.js");
19
19
  function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
@@ -75,4 +75,3 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
75
75
  writeLeadingComments((0, source_code_info_js_1.getPackageComments)(file));
76
76
  return trimSuffix(builder.join(""), "\n");
77
77
  }
78
- exports.makeFilePreamble = makeFilePreamble;
@@ -48,6 +48,10 @@ export interface GeneratedFile {
48
48
  * Create a string literal.
49
49
  */
50
50
  string(string: string): Printable;
51
+ /**
52
+ * Create an array literal.
53
+ */
54
+ array(elements: Printable[]): Printable;
51
55
  /**
52
56
  * Create a JSDoc comment block with the given text. Line breaks and white-space
53
57
  * stay intact.
@@ -13,7 +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.createGeneratedFile = void 0;
16
+ exports.createGeneratedFile = createGeneratedFile;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const import_symbol_js_1 = require("./import-symbol.js");
19
19
  const import_path_js_1 = require("./import-path.js");
@@ -61,6 +61,18 @@ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImport, res
61
61
  value: string,
62
62
  };
63
63
  },
64
+ array(elements) {
65
+ const p = [];
66
+ p.push("[");
67
+ for (const [index, element] of elements.entries()) {
68
+ p.push(element);
69
+ if (index < elements.length - 1) {
70
+ p.push(", ");
71
+ }
72
+ }
73
+ p.push("]");
74
+ return p;
75
+ },
64
76
  jsDoc(textOrSchema, indentation) {
65
77
  return {
66
78
  kind: "es_jsdoc",
@@ -93,7 +105,6 @@ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImport, res
93
105
  },
94
106
  };
95
107
  }
96
- exports.createGeneratedFile = createGeneratedFile;
97
108
  function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
98
109
  if (el.length == 0) {
99
110
  return "";
@@ -1,3 +1,4 @@
1
+ import type { ImportExtension } from "./parameter.js";
1
2
  /**
2
3
  * A configuration for rewriting import paths, a feature mainly used for
3
4
  * remote code generation in the BSR npm registry, which makes it possible
@@ -42,7 +43,7 @@ export type RewriteImports = {
42
43
  * Apply import rewrites to the given import path, and change all .js extensions
43
44
  * to the given import extension.
44
45
  */
45
- export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
46
+ export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: ImportExtension): string;
46
47
  export declare const relativePathRE: RegExp;
47
48
  /**
48
49
  * Derives an ECMAScript module import path from a file path. For example,
@@ -13,7 +13,10 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.makeImportPathRelative = exports.deriveImportPath = exports.relativePathRE = exports.rewriteImportPath = void 0;
16
+ exports.relativePathRE = void 0;
17
+ exports.rewriteImportPath = rewriteImportPath;
18
+ exports.deriveImportPath = deriveImportPath;
19
+ exports.makeImportPathRelative = makeImportPathRelative;
17
20
  const cache = new WeakMap();
18
21
  /**
19
22
  * Apply import rewrites to the given import path, and change all .js extensions
@@ -42,13 +45,17 @@ function rewriteImportPath(importPath, rewriteImports, importExtension) {
42
45
  break;
43
46
  }
44
47
  }
45
- if (importExtension != ".js" && importPath.endsWith(".js")) {
46
- importPath =
47
- importPath.substring(0, importPath.length - 3) + importExtension;
48
+ if (importPath.endsWith(".js")) {
49
+ switch (importExtension) {
50
+ case "none":
51
+ return importPath.substring(0, importPath.length - 3);
52
+ case "ts":
53
+ case "js":
54
+ return importPath.substring(0, importPath.length - 2) + importExtension;
55
+ }
48
56
  }
49
57
  return importPath;
50
58
  }
51
- exports.rewriteImportPath = rewriteImportPath;
52
59
  function starToRegExp(star) {
53
60
  const r = ["^"];
54
61
  for (let i = 0; i < star.length; i++) {
@@ -96,7 +103,6 @@ function deriveImportPath(filename) {
96
103
  }
97
104
  return importPath;
98
105
  }
99
- exports.deriveImportPath = deriveImportPath;
100
106
  /**
101
107
  * Makes an import path relative to the file importing it. For example,
102
108
  * consider the following files:
@@ -132,4 +138,3 @@ function makeImportPathRelative(importer, importPath) {
132
138
  .join("/");
133
139
  return exports.relativePathRE.test(c) ? c : "./" + c;
134
140
  }
135
- exports.makeImportPathRelative = makeImportPathRelative;
@@ -13,7 +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.createImportSymbol = void 0;
16
+ exports.createImportSymbol = createImportSymbol;
17
17
  /**
18
18
  * Create a new import symbol.
19
19
  */
@@ -29,4 +29,3 @@ function createImportSymbol(name, from, typeOnly) {
29
29
  },
30
30
  };
31
31
  }
32
- exports.createImportSymbol = createImportSymbol;
@@ -2,9 +2,8 @@ export type { Plugin } from "./plugin.js";
2
2
  export { runNodeJs } from "./run-node.js";
3
3
  export { createEcmaScriptPlugin } from "./create-es-plugin.js";
4
4
  export { getComments, getDeclarationString, getPackageComments, getSyntaxComments, } from "./source-code-info.js";
5
- export type { Target } from "./target.js";
6
5
  export type { Schema } from "./schema.js";
7
- export type { EcmaScriptPluginOptions } from "./parameter.js";
6
+ export type { EcmaScriptPluginOptions, Target, ImportExtension, } from "./parameter.js";
8
7
  export type { GeneratedFile, FileInfo } from "./generated-file.js";
9
8
  export type { ImportSymbol } from "./import-symbol.js";
10
9
  export { createImportSymbol } from "./import-symbol.js";
package/dist/cjs/jsdoc.js CHANGED
@@ -13,7 +13,8 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.formatJsDocBlock = exports.createJsDocTextFromDesc = void 0;
16
+ exports.createJsDocTextFromDesc = createJsDocTextFromDesc;
17
+ exports.formatJsDocBlock = formatJsDocBlock;
17
18
  const source_code_info_js_1 = require("./source-code-info.js");
18
19
  const reflect_1 = require("@bufbuild/protobuf/reflect");
19
20
  function createJsDocTextFromDesc(desc) {
@@ -81,7 +82,6 @@ function createJsDocTextFromDesc(desc) {
81
82
  }
82
83
  return text;
83
84
  }
84
- exports.createJsDocTextFromDesc = createJsDocTextFromDesc;
85
85
  function formatJsDocBlock(text, indentation) {
86
86
  if (text.trim().length == 0) {
87
87
  return "";
@@ -95,4 +95,3 @@ function formatJsDocBlock(text, indentation) {
95
95
  const i = indentation !== null && indentation !== void 0 ? indentation : "";
96
96
  return [`${i}/**\n`, ...lines.map((l) => `${i} *${l}\n`), `${i} */`].join("");
97
97
  }
98
- exports.formatJsDocBlock = formatJsDocBlock;
package/dist/cjs/names.js CHANGED
@@ -13,7 +13,10 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.generatedJsonTypeName = exports.generatedShapeName = exports.generatedDescName = exports.generateFilePath = void 0;
16
+ exports.generateFilePath = generateFilePath;
17
+ exports.generatedDescName = generatedDescName;
18
+ exports.generatedShapeName = generatedShapeName;
19
+ exports.generatedJsonTypeName = generatedJsonTypeName;
17
20
  const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
18
21
  const reflect_1 = require("@bufbuild/protobuf/reflect");
19
22
  const safe_identifier_js_1 = require("./safe-identifier.js");
@@ -36,7 +39,6 @@ function generateFilePath(file, bootstrapWkt, filesToGenerate) {
36
39
  }
37
40
  return "./" + file.name + "_pb.js";
38
41
  }
39
- exports.generateFilePath = generateFilePath;
40
42
  /**
41
43
  * Return a safe identifier for a generated descriptor.
42
44
  */
@@ -49,7 +51,6 @@ function generatedDescName(desc) {
49
51
  }
50
52
  return name;
51
53
  }
52
- exports.generatedDescName = generatedDescName;
53
54
  /**
54
55
  * Return a safe identifier for a generated shape.
55
56
  */
@@ -61,7 +62,6 @@ function generatedShapeName(desc) {
61
62
  }
62
63
  return name;
63
64
  }
64
- exports.generatedShapeName = generatedShapeName;
65
65
  /**
66
66
  * Return a safe identifier for a generated JSON type.
67
67
  */
@@ -73,7 +73,6 @@ function generatedJsonTypeName(desc) {
73
73
  }
74
74
  return name;
75
75
  }
76
- exports.generatedJsonTypeName = generatedJsonTypeName;
77
76
  /**
78
77
  * Compute the ideal name for a generated descriptor.
79
78
  */
@@ -1,5 +1,12 @@
1
- import type { Target } from "./target.js";
2
1
  import type { RewriteImports } from "./import-path.js";
2
+ /**
3
+ * Represents possible values of the plugin option `target`.
4
+ */
5
+ export type Target = "js" | "ts" | "dts";
6
+ /**
7
+ * Possible values of the plugin option `import_extension`.
8
+ */
9
+ export type ImportExtension = "none" | "js" | "ts";
3
10
  /**
4
11
  * Standard plugin options that every ECMAScript plugin supports.
5
12
  */
@@ -12,11 +19,11 @@ export interface EcmaScriptPluginOptions {
12
19
  */
13
20
  targets: Target[];
14
21
  /**
15
- * Add an extension to every import, for example ".js" or ".ts".
22
+ * Add an extension to every import: "js" or "ts".
16
23
  *
17
- * The default is "".
24
+ * The default is "none".
18
25
  */
19
- importExtension: string;
26
+ importExtension: ImportExtension;
20
27
  /**
21
28
  * Generate `import` statements or `require()` calls.
22
29
  *
@@ -13,7 +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.parseParameter = void 0;
16
+ exports.parseParameter = parseParameter;
17
17
  const error_js_1 = require("./error.js");
18
18
  function parseParameter(parameter, parseExtraOptions) {
19
19
  let targets = ["js", "dts"];
@@ -21,7 +21,7 @@ function parseParameter(parameter, parseExtraOptions) {
21
21
  let bootstrapWkt = false;
22
22
  let keepEmptyFiles = false;
23
23
  const rewriteImports = [];
24
- let importExtension = "";
24
+ let importExtension = "none";
25
25
  let jsImportStyle = "module";
26
26
  const extraParameters = [];
27
27
  const extraParametersRaw = [];
@@ -89,7 +89,22 @@ function parseParameter(parameter, parseExtraOptions) {
89
89
  break;
90
90
  }
91
91
  case "import_extension": {
92
- importExtension = value === "none" ? "" : value;
92
+ switch (value) {
93
+ case "none":
94
+ case "":
95
+ importExtension = "none";
96
+ break;
97
+ case "js":
98
+ case ".js":
99
+ importExtension = "js";
100
+ break;
101
+ case "ts":
102
+ case ".ts":
103
+ importExtension = "ts";
104
+ break;
105
+ default:
106
+ throw new error_js_1.PluginOptionError(raw);
107
+ }
93
108
  break;
94
109
  }
95
110
  case "js_import_style":
@@ -157,7 +172,6 @@ function parseParameter(parameter, parseExtraOptions) {
157
172
  throw new error_js_1.PluginOptionError(extraParametersRaw.join(","), err);
158
173
  }
159
174
  }
160
- exports.parseParameter = parseParameter;
161
175
  function splitParameter(parameter) {
162
176
  if (parameter.length == 0) {
163
177
  return [];
@@ -13,7 +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.runNodeJs = void 0;
16
+ exports.runNodeJs = runNodeJs;
17
17
  const error_js_1 = require("./error.js");
18
18
  const protobuf_1 = require("@bufbuild/protobuf");
19
19
  const wkt_1 = require("@bufbuild/protobuf/wkt");
@@ -55,7 +55,6 @@ function runNodeJs(plugin) {
55
55
  return;
56
56
  });
57
57
  }
58
- exports.runNodeJs = runNodeJs;
59
58
  /**
60
59
  * Read a stream to the end.
61
60
  */
@@ -13,13 +13,12 @@
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.createRuntimeImports = void 0;
16
+ exports.createRuntimeImports = createRuntimeImports;
17
17
  const import_symbol_js_1 = require("./import-symbol.js");
18
18
  const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
19
19
  function createRuntimeImports(bootstrapWkt) {
20
20
  return mapRecord(codegenv1_1.symbols, bootstrapWkt);
21
21
  }
22
- exports.createRuntimeImports = createRuntimeImports;
23
22
  function mapRecord(record, bootstrapWkt) {
24
23
  const result = Object.create(null);
25
24
  for (const [key, value] of Object.entries(record)) {
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
2
+ * Escapes reserved words in ECMAScript and TypeScript identifiers, by appending
3
+ * a dollar sign.
4
+ *
5
+ * This function is intended for use with identifiers from Protobuf. The passed
6
+ * string must be a valid identifier (e.g. not start with a digit).
3
7
  *
4
8
  * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
5
9
  */
@@ -13,7 +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.safeIdentifier = void 0;
16
+ exports.safeIdentifier = safeIdentifier;
17
17
  /**
18
18
  * Names that cannot be used for identifiers, such as class names,
19
19
  * but _can_ be used for object properties.
@@ -80,11 +80,14 @@ const reservedIdentifiers = new Set([
80
80
  "Partial",
81
81
  ]);
82
82
  /**
83
- * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
83
+ * Escapes reserved words in ECMAScript and TypeScript identifiers, by appending
84
+ * a dollar sign.
85
+ *
86
+ * This function is intended for use with identifiers from Protobuf. The passed
87
+ * string must be a valid identifier (e.g. not start with a digit).
84
88
  *
85
89
  * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
86
90
  */
87
91
  function safeIdentifier(name) {
88
92
  return reservedIdentifiers.has(name) ? name + "$" : name;
89
93
  }
90
- exports.safeIdentifier = safeIdentifier;
@@ -2,8 +2,7 @@ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from
2
2
  import type { CodeGeneratorRequest } from "@bufbuild/protobuf/wkt";
3
3
  import { Edition } from "@bufbuild/protobuf/wkt";
4
4
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
5
- import type { Target } from "./target.js";
6
- import type { EcmaScriptPluginOptions, ParsedParameter } from "./parameter.js";
5
+ import type { EcmaScriptPluginOptions, ParsedParameter, Target } from "./parameter.js";
7
6
  /**
8
7
  * Schema describes the files and types that the plugin is requested to
9
8
  * generate.
@@ -13,7 +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.createSchema = void 0;
16
+ exports.createSchema = createSchema;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const wkt_1 = require("@bufbuild/protobuf/wkt");
19
19
  const reflect_1 = require("@bufbuild/protobuf/reflect");
@@ -59,7 +59,6 @@ function createSchema(request, parameter, pluginName, pluginVersion, minimumEdit
59
59
  },
60
60
  };
61
61
  }
62
- exports.createSchema = createSchema;
63
62
  function getFilesToGenerate(request, minimumEdition, maximumEdition) {
64
63
  if (minimumEdition > maximumEdition) {
65
64
  throw new Error(`configured minimumEdition ${editionToString(minimumEdition)} > maximumEdition ${editionToString(maximumEdition)} - please contact plugin author`);
@@ -13,7 +13,11 @@
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.getDeclarationString = exports.getFeatureOptionStrings = exports.getComments = exports.getSyntaxComments = exports.getPackageComments = void 0;
16
+ exports.getPackageComments = getPackageComments;
17
+ exports.getSyntaxComments = getSyntaxComments;
18
+ exports.getComments = getComments;
19
+ exports.getFeatureOptionStrings = getFeatureOptionStrings;
20
+ exports.getDeclarationString = getDeclarationString;
17
21
  const protobuf_1 = require("@bufbuild/protobuf");
18
22
  const reflect_1 = require("@bufbuild/protobuf/reflect");
19
23
  const wkt_1 = require("@bufbuild/protobuf/wkt");
@@ -25,7 +29,6 @@ function getPackageComments(desc) {
25
29
  wkt_1.FileDescriptorProtoSchema.field.package.number,
26
30
  ]);
27
31
  }
28
- exports.getPackageComments = getPackageComments;
29
32
  /**
30
33
  * Get comments on the syntax element in the protobuf source.
31
34
  */
@@ -34,7 +37,6 @@ function getSyntaxComments(desc) {
34
37
  wkt_1.FileDescriptorProtoSchema.field.syntax.number,
35
38
  ]);
36
39
  }
37
- exports.getSyntaxComments = getSyntaxComments;
38
40
  /**
39
41
  * Get comments on the element in the protobuf source.
40
42
  */
@@ -123,7 +125,6 @@ function getComments(desc) {
123
125
  }
124
126
  return findComments(file.proto.sourceCodeInfo, path);
125
127
  }
126
- exports.getComments = getComments;
127
128
  /**
128
129
  * Get feature options set on the element in the protobuf source. This returns
129
130
  * compact (e.g. fields) or regular options (e.g. files) as an array of strings.
@@ -147,7 +148,6 @@ function getFeatureOptionStrings(desc) {
147
148
  }
148
149
  return strings;
149
150
  }
150
- exports.getFeatureOptionStrings = getFeatureOptionStrings;
151
151
  /**
152
152
  * Return a string that matches the definition of a field in the protobuf
153
153
  * source. Does not take custom options into account.
@@ -225,7 +225,6 @@ function getDeclarationString(desc) {
225
225
  }
226
226
  return parts.join(" ");
227
227
  }
228
- exports.getDeclarationString = getDeclarationString;
229
228
  /**
230
229
  * Whether this field was declared with `required` in the protobuf source.
231
230
  */
@@ -16,7 +16,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
16
16
  return (mod && mod.__esModule) ? mod : { "default": mod };
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.transpile = void 0;
19
+ exports.transpile = transpile;
20
20
  const typescript_1 = __importDefault(require("typescript"));
21
21
  const vfs_1 = require("@typescript/vfs");
22
22
  /* eslint-disable import/no-named-as-default-member */
@@ -122,4 +122,3 @@ function transpile(files, transpileJs, transpileDts, jsImportStyle) {
122
122
  }
123
123
  return results;
124
124
  }
125
- exports.transpile = transpile;
@@ -48,6 +48,10 @@ export interface GeneratedFile {
48
48
  * Create a string literal.
49
49
  */
50
50
  string(string: string): Printable;
51
+ /**
52
+ * Create an array literal.
53
+ */
54
+ array(elements: Printable[]): Printable;
51
55
  /**
52
56
  * Create a JSDoc comment block with the given text. Line breaks and white-space
53
57
  * stay intact.
@@ -58,6 +58,18 @@ export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImpo
58
58
  value: string,
59
59
  };
60
60
  },
61
+ array(elements) {
62
+ const p = [];
63
+ p.push("[");
64
+ for (const [index, element] of elements.entries()) {
65
+ p.push(element);
66
+ if (index < elements.length - 1) {
67
+ p.push(", ");
68
+ }
69
+ }
70
+ p.push("]");
71
+ return p;
72
+ },
61
73
  jsDoc(textOrSchema, indentation) {
62
74
  return {
63
75
  kind: "es_jsdoc",
@@ -1,3 +1,4 @@
1
+ import type { ImportExtension } from "./parameter.js";
1
2
  /**
2
3
  * A configuration for rewriting import paths, a feature mainly used for
3
4
  * remote code generation in the BSR npm registry, which makes it possible
@@ -42,7 +43,7 @@ export type RewriteImports = {
42
43
  * Apply import rewrites to the given import path, and change all .js extensions
43
44
  * to the given import extension.
44
45
  */
45
- export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
46
+ export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: ImportExtension): string;
46
47
  export declare const relativePathRE: RegExp;
47
48
  /**
48
49
  * Derives an ECMAScript module import path from a file path. For example,
@@ -39,9 +39,14 @@ export function rewriteImportPath(importPath, rewriteImports, importExtension) {
39
39
  break;
40
40
  }
41
41
  }
42
- if (importExtension != ".js" && importPath.endsWith(".js")) {
43
- importPath =
44
- importPath.substring(0, importPath.length - 3) + importExtension;
42
+ if (importPath.endsWith(".js")) {
43
+ switch (importExtension) {
44
+ case "none":
45
+ return importPath.substring(0, importPath.length - 3);
46
+ case "ts":
47
+ case "js":
48
+ return importPath.substring(0, importPath.length - 2) + importExtension;
49
+ }
45
50
  }
46
51
  return importPath;
47
52
  }
@@ -2,9 +2,8 @@ export type { Plugin } from "./plugin.js";
2
2
  export { runNodeJs } from "./run-node.js";
3
3
  export { createEcmaScriptPlugin } from "./create-es-plugin.js";
4
4
  export { getComments, getDeclarationString, getPackageComments, getSyntaxComments, } from "./source-code-info.js";
5
- export type { Target } from "./target.js";
6
5
  export type { Schema } from "./schema.js";
7
- export type { EcmaScriptPluginOptions } from "./parameter.js";
6
+ export type { EcmaScriptPluginOptions, Target, ImportExtension, } from "./parameter.js";
8
7
  export type { GeneratedFile, FileInfo } from "./generated-file.js";
9
8
  export type { ImportSymbol } from "./import-symbol.js";
10
9
  export { createImportSymbol } from "./import-symbol.js";
@@ -1,5 +1,12 @@
1
- import type { Target } from "./target.js";
2
1
  import type { RewriteImports } from "./import-path.js";
2
+ /**
3
+ * Represents possible values of the plugin option `target`.
4
+ */
5
+ export type Target = "js" | "ts" | "dts";
6
+ /**
7
+ * Possible values of the plugin option `import_extension`.
8
+ */
9
+ export type ImportExtension = "none" | "js" | "ts";
3
10
  /**
4
11
  * Standard plugin options that every ECMAScript plugin supports.
5
12
  */
@@ -12,11 +19,11 @@ export interface EcmaScriptPluginOptions {
12
19
  */
13
20
  targets: Target[];
14
21
  /**
15
- * Add an extension to every import, for example ".js" or ".ts".
22
+ * Add an extension to every import: "js" or "ts".
16
23
  *
17
- * The default is "".
24
+ * The default is "none".
18
25
  */
19
- importExtension: string;
26
+ importExtension: ImportExtension;
20
27
  /**
21
28
  * Generate `import` statements or `require()` calls.
22
29
  *
@@ -18,7 +18,7 @@ export function parseParameter(parameter, parseExtraOptions) {
18
18
  let bootstrapWkt = false;
19
19
  let keepEmptyFiles = false;
20
20
  const rewriteImports = [];
21
- let importExtension = "";
21
+ let importExtension = "none";
22
22
  let jsImportStyle = "module";
23
23
  const extraParameters = [];
24
24
  const extraParametersRaw = [];
@@ -86,7 +86,22 @@ export function parseParameter(parameter, parseExtraOptions) {
86
86
  break;
87
87
  }
88
88
  case "import_extension": {
89
- importExtension = value === "none" ? "" : value;
89
+ switch (value) {
90
+ case "none":
91
+ case "":
92
+ importExtension = "none";
93
+ break;
94
+ case "js":
95
+ case ".js":
96
+ importExtension = "js";
97
+ break;
98
+ case "ts":
99
+ case ".ts":
100
+ importExtension = "ts";
101
+ break;
102
+ default:
103
+ throw new PluginOptionError(raw);
104
+ }
90
105
  break;
91
106
  }
92
107
  case "js_import_style":
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
2
+ * Escapes reserved words in ECMAScript and TypeScript identifiers, by appending
3
+ * a dollar sign.
4
+ *
5
+ * This function is intended for use with identifiers from Protobuf. The passed
6
+ * string must be a valid identifier (e.g. not start with a digit).
3
7
  *
4
8
  * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
5
9
  */
@@ -77,7 +77,11 @@ const reservedIdentifiers = new Set([
77
77
  "Partial",
78
78
  ]);
79
79
  /**
80
- * Escapes names that are reserved identifiers in ECMAScript, TypeScript.
80
+ * Escapes reserved words in ECMAScript and TypeScript identifiers, by appending
81
+ * a dollar sign.
82
+ *
83
+ * This function is intended for use with identifiers from Protobuf. The passed
84
+ * string must be a valid identifier (e.g. not start with a digit).
81
85
  *
82
86
  * Also see safeObjectProperty() from @bufbuild/protoplugin/reflect.
83
87
  */
@@ -2,8 +2,7 @@ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from
2
2
  import type { CodeGeneratorRequest } from "@bufbuild/protobuf/wkt";
3
3
  import { Edition } from "@bufbuild/protobuf/wkt";
4
4
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
5
- import type { Target } from "./target.js";
6
- import type { EcmaScriptPluginOptions, ParsedParameter } from "./parameter.js";
5
+ import type { EcmaScriptPluginOptions, ParsedParameter, Target } from "./parameter.js";
7
6
  /**
8
7
  * Schema describes the files and types that the plugin is requested to
9
8
  * generate.
package/package.json CHANGED
@@ -1,8 +1,15 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "2.0.0-beta.2",
4
- "license": "(Apache-2.0 AND BSD-3-Clause)",
3
+ "version": "2.0.0-beta.3",
4
+ "license": "Apache-2.0",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
+ "keywords": [
7
+ "protobuf",
8
+ "schema",
9
+ "typescript",
10
+ "ecmascript",
11
+ "protoc-plugin"
12
+ ],
6
13
  "repository": {
7
14
  "type": "git",
8
15
  "url": "https://github.com/bufbuild/protobuf-es.git",
@@ -28,7 +35,7 @@
28
35
  }
29
36
  },
30
37
  "dependencies": {
31
- "@bufbuild/protobuf": "2.0.0-beta.2",
38
+ "@bufbuild/protobuf": "2.0.0-beta.3",
32
39
  "@typescript/vfs": "^1.5.2",
33
40
  "typescript": "5.4.5"
34
41
  },
@@ -1,4 +0,0 @@
1
- /**
2
- * Represents possible values of the plugin option `target`.
3
- */
4
- export type Target = "js" | "ts" | "dts";
@@ -1,15 +0,0 @@
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 });
@@ -1,4 +0,0 @@
1
- /**
2
- * Represents possible values of the plugin option `target`.
3
- */
4
- export type Target = "js" | "ts" | "dts";
@@ -1,14 +0,0 @@
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 {};