@bufbuild/protoplugin 2.2.5 → 2.3.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.
@@ -2,7 +2,6 @@ import { type SupportedEdition } from "@bufbuild/protobuf";
2
2
  import type { Schema } from "./schema.js";
3
3
  import type { FileInfo } from "./generated-file.js";
4
4
  import type { Plugin } from "./plugin.js";
5
- import type { RawPluginOptions } from "./parameter.js";
6
5
  interface PluginInit<Options extends object> {
7
6
  /**
8
7
  * Name of this code generator plugin.
@@ -15,8 +14,26 @@ interface PluginInit<Options extends object> {
15
14
  /**
16
15
  * An optional parsing function which can be used to parse your own plugin
17
16
  * options.
17
+ *
18
+ * For example, if a plugin is run with the options foo=123,bar,baz=a,baz=b
19
+ * the raw options are:
20
+ *
21
+ * ```ts
22
+ * [
23
+ * { key: "foo", value: "123" },
24
+ * { key: "bar", value: "" },
25
+ * { key: "baz", value: "a" },
26
+ * { key: "baz", value: "b" },
27
+ * ]
28
+ * ```
29
+ *
30
+ * If your plugin does not recognize an option, it must throw an Error in
31
+ * parseOptions.
18
32
  */
19
- parseOptions?: (rawOptions: RawPluginOptions) => Options;
33
+ parseOptions?: (rawOptions: {
34
+ key: string;
35
+ value: string;
36
+ }[]) => Options;
20
37
  /**
21
38
  * The earliest edition supported by this plugin. Defaults to the minimum
22
39
  * edition supported by @bufbuild/protobuf.
@@ -47,10 +47,10 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
47
47
  }
48
48
  switch (file.edition) {
49
49
  case wkt_1.Edition.EDITION_PROTO2:
50
- builder.push(`syntax proto2)\n`);
50
+ builder.push("syntax proto2)\n");
51
51
  break;
52
52
  case wkt_1.Edition.EDITION_PROTO3:
53
- builder.push(`syntax proto3)\n`);
53
+ builder.push("syntax proto3)\n");
54
54
  break;
55
55
  default: {
56
56
  const editionString = wkt_1.Edition[file.edition];
@@ -59,7 +59,7 @@ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck)
59
59
  builder.push(`edition ${e})\n`);
60
60
  }
61
61
  else {
62
- builder.push(`unknown edition\n`);
62
+ builder.push("unknown edition\n");
63
63
  }
64
64
  break;
65
65
  }
@@ -113,7 +113,7 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
113
113
  if (legacyCommonJs) {
114
114
  c.push(`"use strict";\n`);
115
115
  c.push(`Object.defineProperty(exports, "__esModule", { value: true });\n`);
116
- c.push(`\n`);
116
+ c.push("\n");
117
117
  }
118
118
  const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
119
119
  if (legacyCommonJs) {
@@ -166,10 +166,10 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
166
166
  }
167
167
  if (legacyCommonJs) {
168
168
  if (legacyCommonJsExportNames.length > 0) {
169
- c.push(`\n`);
169
+ c.push("\n");
170
170
  }
171
171
  for (const name of legacyCommonJsExportNames) {
172
- c.push(`exports.`, name, " = ", name, ";\n");
172
+ c.push("exports.", name, " = ", name, ";\n");
173
173
  }
174
174
  }
175
175
  return c.join("");
package/dist/cjs/names.js CHANGED
@@ -77,35 +77,35 @@ function generatedJsonTypeName(desc) {
77
77
  * Compute the ideal name for a generated descriptor.
78
78
  */
79
79
  function idealDescName(desc, i) {
80
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
80
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
81
81
  if (desc.kind == "file") {
82
82
  const name = "file_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_");
83
- return (0, safe_identifier_js_1.safeIdentifier)(name + escape);
83
+ return (0, safe_identifier_js_1.safeIdentifier)(name + salt);
84
84
  }
85
85
  switch (desc.kind) {
86
86
  case "enum":
87
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Schema" + escape);
87
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Schema" + salt);
88
88
  case "message":
89
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Schema" + escape);
89
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Schema" + salt);
90
90
  case "extension":
91
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + escape);
91
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + salt);
92
92
  case "service":
93
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + escape);
93
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + salt);
94
94
  }
95
95
  }
96
96
  /**
97
97
  * Compute the ideal name for a generated shape.
98
98
  */
99
99
  function idealShapeName(desc, i) {
100
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
101
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + escape);
100
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
101
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + salt);
102
102
  }
103
103
  /**
104
104
  * Compute the ideal name for a generated JSON type.
105
105
  */
106
106
  function idealJsonTypeName(desc, i) {
107
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
108
- return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Json" + escape);
107
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
108
+ return (0, safe_identifier_js_1.safeIdentifier)(identifier(desc) + "Json" + salt);
109
109
  }
110
110
  /**
111
111
  * Return an identifier for the given descriptor based on its type name.
@@ -56,26 +56,7 @@ export interface ParsedParameter<T> {
56
56
  parsed: T & EcmaScriptPluginOptions;
57
57
  sanitized: string;
58
58
  }
59
- /**
60
- * Raw options to parse.
61
- *
62
- * For example, if a plugin is run with the options foo=123,bar,baz=a,baz=b
63
- * the raw options are:
64
- *
65
- * ```ts
66
- * [
67
- * { key: "foo", value: "123" },
68
- * { key: "bar", value: "" },
69
- * { key: "baz", value: "a" },
70
- * { key: "baz", value: "b" },
71
- * ]
72
- * ```
73
- *
74
- * If your plugin does not recognize an option, it must throw an Error in
75
- * parseOptions.
76
- */
77
- export type RawPluginOptions = {
59
+ export declare function parseParameter<T extends object>(parameter: string, parseExtraOptions: ((rawOptions: {
78
60
  key: string;
79
61
  value: string;
80
- }[];
81
- export declare function parseParameter<T extends object>(parameter: string, parseExtraOptions: ((rawOptions: RawPluginOptions) => T) | undefined): ParsedParameter<T>;
62
+ }[]) => T) | undefined): ParsedParameter<T>;
@@ -158,7 +158,7 @@ function parseParameter(parameter, parseExtraOptions) {
158
158
  jsImportStyle,
159
159
  keepEmptyFiles,
160
160
  };
161
- if (parseExtraOptions === undefined || extraParameters.length === 0) {
161
+ if (parseExtraOptions === undefined) {
162
162
  return {
163
163
  parsed: ecmaScriptPluginOptions,
164
164
  sanitized: sanitizedParameters,
@@ -217,7 +217,7 @@ function getDeclarationString(desc) {
217
217
  }
218
218
  if (protoOptions !== undefined &&
219
219
  (0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsSchema.field.deprecated)) {
220
- options.push(`deprecated = true`);
220
+ options.push("deprecated = true");
221
221
  }
222
222
  options.push(...getFeatureOptionStrings(desc));
223
223
  if (options.length > 0) {
@@ -19,7 +19,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.transpile = transpile;
20
20
  const typescript_1 = __importDefault(require("typescript"));
21
21
  const vfs_1 = require("@typescript/vfs");
22
- /* eslint-disable import/no-named-as-default-member */
23
22
  // The default options used to auto-transpile if needed.
24
23
  const defaultOptions = {
25
24
  // Type checking
@@ -71,9 +70,9 @@ function createTranspiler(options, files) {
71
70
  const fsMap = (0, vfs_1.createDefaultMapFromNodeModules)({
72
71
  target: options.target,
73
72
  });
74
- files.forEach((file) => {
73
+ for (const file of files) {
75
74
  fsMap.set(file.name, file.content);
76
- });
75
+ }
77
76
  const system = (0, vfs_1.createSystem)(fsMap);
78
77
  const host = (0, vfs_1.createVirtualCompilerHost)(system, options, typescript_1.default);
79
78
  return typescript_1.default.createProgram({
@@ -91,7 +90,7 @@ function transpile(files, transpileJs, transpileDts, jsImportStyle) {
91
90
  const program = createTranspiler(options, files);
92
91
  const results = [];
93
92
  let err;
94
- const result = program.emit(undefined, (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
93
+ const result = program.emit(undefined, (fileName, data, _writeByteOrderMark, _onError, sourceFiles) => {
95
94
  // We have to go through some hoops here because the header we add to each
96
95
  // file is not part of the AST. So we find the TypeScript file we
97
96
  // generated for each emitted file and add the header to each output ourselves.
@@ -2,7 +2,6 @@ import { type SupportedEdition } from "@bufbuild/protobuf";
2
2
  import type { Schema } from "./schema.js";
3
3
  import type { FileInfo } from "./generated-file.js";
4
4
  import type { Plugin } from "./plugin.js";
5
- import type { RawPluginOptions } from "./parameter.js";
6
5
  interface PluginInit<Options extends object> {
7
6
  /**
8
7
  * Name of this code generator plugin.
@@ -15,8 +14,26 @@ interface PluginInit<Options extends object> {
15
14
  /**
16
15
  * An optional parsing function which can be used to parse your own plugin
17
16
  * options.
17
+ *
18
+ * For example, if a plugin is run with the options foo=123,bar,baz=a,baz=b
19
+ * the raw options are:
20
+ *
21
+ * ```ts
22
+ * [
23
+ * { key: "foo", value: "123" },
24
+ * { key: "bar", value: "" },
25
+ * { key: "baz", value: "a" },
26
+ * { key: "baz", value: "b" },
27
+ * ]
28
+ * ```
29
+ *
30
+ * If your plugin does not recognize an option, it must throw an Error in
31
+ * parseOptions.
18
32
  */
19
- parseOptions?: (rawOptions: RawPluginOptions) => Options;
33
+ parseOptions?: (rawOptions: {
34
+ key: string;
35
+ value: string;
36
+ }[]) => Options;
20
37
  /**
21
38
  * The earliest edition supported by this plugin. Defaults to the minimum
22
39
  * edition supported by @bufbuild/protobuf.
@@ -44,10 +44,10 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
44
44
  }
45
45
  switch (file.edition) {
46
46
  case Edition.EDITION_PROTO2:
47
- builder.push(`syntax proto2)\n`);
47
+ builder.push("syntax proto2)\n");
48
48
  break;
49
49
  case Edition.EDITION_PROTO3:
50
- builder.push(`syntax proto3)\n`);
50
+ builder.push("syntax proto3)\n");
51
51
  break;
52
52
  default: {
53
53
  const editionString = Edition[file.edition];
@@ -56,7 +56,7 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
56
56
  builder.push(`edition ${e})\n`);
57
57
  }
58
58
  else {
59
- builder.push(`unknown edition\n`);
59
+ builder.push("unknown edition\n");
60
60
  }
61
61
  break;
62
62
  }
@@ -110,7 +110,7 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
110
110
  if (legacyCommonJs) {
111
111
  c.push(`"use strict";\n`);
112
112
  c.push(`Object.defineProperty(exports, "__esModule", { value: true });\n`);
113
- c.push(`\n`);
113
+ c.push("\n");
114
114
  }
115
115
  const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
116
116
  if (legacyCommonJs) {
@@ -163,10 +163,10 @@ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
163
163
  }
164
164
  if (legacyCommonJs) {
165
165
  if (legacyCommonJsExportNames.length > 0) {
166
- c.push(`\n`);
166
+ c.push("\n");
167
167
  }
168
168
  for (const name of legacyCommonJsExportNames) {
169
- c.push(`exports.`, name, " = ", name, ";\n");
169
+ c.push("exports.", name, " = ", name, ";\n");
170
170
  }
171
171
  }
172
172
  return c.join("");
package/dist/esm/names.js CHANGED
@@ -71,35 +71,35 @@ export function generatedJsonTypeName(desc) {
71
71
  * Compute the ideal name for a generated descriptor.
72
72
  */
73
73
  function idealDescName(desc, i) {
74
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
74
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
75
75
  if (desc.kind == "file") {
76
76
  const name = "file_" + desc.name.replace(/[^a-zA-Z0-9_]+/g, "_");
77
- return safeIdentifier(name + escape);
77
+ return safeIdentifier(name + salt);
78
78
  }
79
79
  switch (desc.kind) {
80
80
  case "enum":
81
- return safeIdentifier(identifier(desc) + "Schema" + escape);
81
+ return safeIdentifier(identifier(desc) + "Schema" + salt);
82
82
  case "message":
83
- return safeIdentifier(identifier(desc) + "Schema" + escape);
83
+ return safeIdentifier(identifier(desc) + "Schema" + salt);
84
84
  case "extension":
85
- return safeIdentifier(identifier(desc) + escape);
85
+ return safeIdentifier(identifier(desc) + salt);
86
86
  case "service":
87
- return safeIdentifier(identifier(desc) + escape);
87
+ return safeIdentifier(identifier(desc) + salt);
88
88
  }
89
89
  }
90
90
  /**
91
91
  * Compute the ideal name for a generated shape.
92
92
  */
93
93
  function idealShapeName(desc, i) {
94
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
95
- return safeIdentifier(identifier(desc) + escape);
94
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
95
+ return safeIdentifier(identifier(desc) + salt);
96
96
  }
97
97
  /**
98
98
  * Compute the ideal name for a generated JSON type.
99
99
  */
100
100
  function idealJsonTypeName(desc, i) {
101
- const escape = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
102
- return safeIdentifier(identifier(desc) + "Json" + escape);
101
+ const salt = i === 0 ? "" : i === 1 ? "$" : `$${i - 1}`;
102
+ return safeIdentifier(identifier(desc) + "Json" + salt);
103
103
  }
104
104
  /**
105
105
  * Return an identifier for the given descriptor based on its type name.
@@ -56,26 +56,7 @@ export interface ParsedParameter<T> {
56
56
  parsed: T & EcmaScriptPluginOptions;
57
57
  sanitized: string;
58
58
  }
59
- /**
60
- * Raw options to parse.
61
- *
62
- * For example, if a plugin is run with the options foo=123,bar,baz=a,baz=b
63
- * the raw options are:
64
- *
65
- * ```ts
66
- * [
67
- * { key: "foo", value: "123" },
68
- * { key: "bar", value: "" },
69
- * { key: "baz", value: "a" },
70
- * { key: "baz", value: "b" },
71
- * ]
72
- * ```
73
- *
74
- * If your plugin does not recognize an option, it must throw an Error in
75
- * parseOptions.
76
- */
77
- export type RawPluginOptions = {
59
+ export declare function parseParameter<T extends object>(parameter: string, parseExtraOptions: ((rawOptions: {
78
60
  key: string;
79
61
  value: string;
80
- }[];
81
- export declare function parseParameter<T extends object>(parameter: string, parseExtraOptions: ((rawOptions: RawPluginOptions) => T) | undefined): ParsedParameter<T>;
62
+ }[]) => T) | undefined): ParsedParameter<T>;
@@ -155,7 +155,7 @@ export function parseParameter(parameter, parseExtraOptions) {
155
155
  jsImportStyle,
156
156
  keepEmptyFiles,
157
157
  };
158
- if (parseExtraOptions === undefined || extraParameters.length === 0) {
158
+ if (parseExtraOptions === undefined) {
159
159
  return {
160
160
  parsed: ecmaScriptPluginOptions,
161
161
  sanitized: sanitizedParameters,
@@ -210,7 +210,7 @@ export function getDeclarationString(desc) {
210
210
  }
211
211
  if (protoOptions !== undefined &&
212
212
  isFieldSet(protoOptions, FieldOptionsSchema.field.deprecated)) {
213
- options.push(`deprecated = true`);
213
+ options.push("deprecated = true");
214
214
  }
215
215
  options.push(...getFeatureOptionStrings(desc));
216
216
  if (options.length > 0) {
@@ -13,7 +13,6 @@
13
13
  // limitations under the License.
14
14
  import ts from "typescript";
15
15
  import { createDefaultMapFromNodeModules, createSystem, createVirtualCompilerHost, } from "@typescript/vfs";
16
- /* eslint-disable import/no-named-as-default-member */
17
16
  // The default options used to auto-transpile if needed.
18
17
  const defaultOptions = {
19
18
  // Type checking
@@ -65,9 +64,9 @@ function createTranspiler(options, files) {
65
64
  const fsMap = createDefaultMapFromNodeModules({
66
65
  target: options.target,
67
66
  });
68
- files.forEach((file) => {
67
+ for (const file of files) {
69
68
  fsMap.set(file.name, file.content);
70
- });
69
+ }
71
70
  const system = createSystem(fsMap);
72
71
  const host = createVirtualCompilerHost(system, options, ts);
73
72
  return ts.createProgram({
@@ -85,7 +84,7 @@ export function transpile(files, transpileJs, transpileDts, jsImportStyle) {
85
84
  const program = createTranspiler(options, files);
86
85
  const results = [];
87
86
  let err;
88
- const result = program.emit(undefined, (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
87
+ const result = program.emit(undefined, (fileName, data, _writeByteOrderMark, _onError, sourceFiles) => {
89
88
  // We have to go through some hoops here because the header we add to each
90
89
  // file is not part of the AST. So we find the TypeScript file we
91
90
  // generated for each emitted file and add the header to each output ourselves.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "2.2.5",
3
+ "version": "2.3.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
6
  "keywords": [
@@ -20,9 +20,9 @@
20
20
  "build": "npm run build:cjs && npm run build:esm",
21
21
  "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
22
22
  "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm",
23
- "format": "prettier --write --ignore-unknown '.' '!dist' '!.turbo'",
23
+ "format": "biome format --write",
24
24
  "license-header": "license-header",
25
- "lint": "eslint --max-warnings 0 .",
25
+ "lint": "biome lint --error-on-warnings",
26
26
  "attw": "attw --pack"
27
27
  },
28
28
  "type": "module",
@@ -35,13 +35,11 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@bufbuild/protobuf": "2.2.5",
38
+ "@bufbuild/protobuf": "2.3.0",
39
39
  "@typescript/vfs": "^1.5.2",
40
40
  "typescript": "5.4.5"
41
41
  },
42
- "files": [
43
- "dist/**"
44
- ],
42
+ "files": ["dist/**"],
45
43
  "devDependencies": {
46
44
  "@types/lz-string": "^1.5.0"
47
45
  }