@bufbuild/protoplugin 1.7.2 → 1.9.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 (55) hide show
  1. package/dist/cjs/create-es-plugin.d.ts +1 -1
  2. package/dist/cjs/ecmascript/file-preamble.d.ts +2 -0
  3. package/dist/cjs/ecmascript/file-preamble.js +74 -0
  4. package/dist/cjs/ecmascript/generated-file.d.ts +2 -2
  5. package/dist/cjs/ecmascript/generated-file.js +90 -31
  6. package/dist/cjs/ecmascript/import-path.d.ts +1 -1
  7. package/dist/cjs/ecmascript/index.d.ts +4 -8
  8. package/dist/cjs/ecmascript/index.js +10 -17
  9. package/dist/cjs/ecmascript/jsdoc.d.ts +6 -0
  10. package/dist/cjs/ecmascript/jsdoc.js +1 -0
  11. package/dist/cjs/ecmascript/{custom-options.d.ts → legacy-custom-options.d.ts} +2 -2
  12. package/dist/cjs/ecmascript/{gencommon.d.ts → legacy-gencommon.d.ts} +12 -7
  13. package/dist/cjs/ecmascript/{gencommon.js → legacy-gencommon.js} +50 -98
  14. package/dist/cjs/ecmascript/opaque-printables.d.ts +27 -0
  15. package/dist/{esm/ecmascript/export-declaration.js → cjs/ecmascript/opaque-printables.js} +2 -7
  16. package/dist/cjs/ecmascript/parameter.d.ts +2 -2
  17. package/dist/cjs/ecmascript/schema.d.ts +2 -2
  18. package/dist/cjs/ecmascript/schema.js +2 -2
  19. package/dist/cjs/ecmascript/transpile.js +1 -1
  20. package/dist/cjs/error.d.ts +2 -0
  21. package/dist/cjs/error.js +9 -1
  22. package/dist/cjs/index.d.ts +2 -2
  23. package/dist/cjs/run-node.js +1 -1
  24. package/dist/esm/create-es-plugin.d.ts +1 -1
  25. package/dist/esm/ecmascript/file-preamble.d.ts +2 -0
  26. package/dist/esm/ecmascript/file-preamble.js +70 -0
  27. package/dist/esm/ecmascript/generated-file.d.ts +2 -2
  28. package/dist/esm/ecmascript/generated-file.js +90 -31
  29. package/dist/esm/ecmascript/import-path.d.ts +1 -1
  30. package/dist/esm/ecmascript/index.d.ts +4 -8
  31. package/dist/esm/ecmascript/index.js +3 -10
  32. package/dist/esm/ecmascript/jsdoc.d.ts +6 -0
  33. package/dist/esm/ecmascript/jsdoc.js +1 -0
  34. package/dist/esm/ecmascript/{custom-options.d.ts → legacy-custom-options.d.ts} +2 -2
  35. package/dist/esm/ecmascript/{custom-options.js → legacy-custom-options.js} +1 -1
  36. package/dist/esm/ecmascript/{gencommon.d.ts → legacy-gencommon.d.ts} +12 -7
  37. package/dist/esm/ecmascript/{gencommon.js → legacy-gencommon.js} +49 -95
  38. package/dist/esm/ecmascript/opaque-printables.d.ts +27 -0
  39. package/dist/{cjs/ecmascript/export-declaration.js → esm/ecmascript/opaque-printables.js} +1 -11
  40. package/dist/esm/ecmascript/parameter.d.ts +2 -2
  41. package/dist/esm/ecmascript/schema.d.ts +2 -2
  42. package/dist/esm/ecmascript/schema.js +2 -2
  43. package/dist/esm/ecmascript/transpile.js +1 -1
  44. package/dist/esm/error.d.ts +2 -0
  45. package/dist/esm/error.js +7 -0
  46. package/dist/esm/index.d.ts +2 -2
  47. package/dist/esm/run-node.js +2 -2
  48. package/package.json +5 -16
  49. package/dist/cjs/ecmascript/export-declaration.d.ts +0 -7
  50. package/dist/esm/ecmascript/export-declaration.d.ts +0 -7
  51. package/dist/proxy/ecmascript/index.d.ts +0 -1
  52. package/dist/proxy/ecmascript/index.js +0 -1
  53. package/dist/proxy/index.d.ts +0 -1
  54. package/dist/proxy/index.js +0 -1
  55. /package/dist/cjs/ecmascript/{custom-options.js → legacy-custom-options.js} +0 -0
@@ -11,67 +11,26 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { codegenInfo, Edition, LongType, ScalarType, } from "@bufbuild/protobuf";
15
- const { localName, getUnwrappedFieldType, scalarDefaultValue } = codegenInfo;
16
- export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
17
- const builder = [];
18
- const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
19
- const writeLeadingComments = (comments) => {
20
- for (let comment of comments.leadingDetached) {
21
- comment = trimSuffix(comment, "\n");
22
- for (const line of comment.split("\n")) {
23
- builder.push(`//${line}\n`);
24
- }
25
- builder.push("\n");
26
- }
27
- if (comments.leading !== undefined) {
28
- const comment = trimSuffix(comments.leading, "\n");
29
- for (const line of comment.split("\n")) {
30
- builder.push(`//${line}\n`);
31
- }
32
- builder.push("\n");
33
- }
34
- };
35
- writeLeadingComments(file.getSyntaxComments());
36
- builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
37
- if (parameter !== "") {
38
- builder.push(` with parameter "${parameter}"`);
39
- }
40
- builder.push("\n");
41
- builder.push(`// @generated from file ${file.name}.proto (`);
42
- if (file.proto.package !== undefined) {
43
- builder.push(`package ${file.proto.package}, `);
44
- }
45
- switch (file.edition) {
46
- case Edition.EDITION_PROTO2:
47
- builder.push(`syntax proto2)\n`);
48
- break;
49
- case Edition.EDITION_PROTO3:
50
- builder.push(`syntax proto3)\n`);
51
- break;
52
- default: {
53
- const editionString = Edition[file.edition];
54
- if (typeof editionString == "string") {
55
- const e = editionString.replace("EDITION_", "").toLowerCase();
56
- builder.push(`edition ${e})\n`);
57
- }
58
- else {
59
- builder.push(`unknown edition\n`);
60
- }
61
- break;
62
- }
63
- }
64
- builder.push("/* eslint-disable */\n");
65
- if (tsNoCheck) {
66
- builder.push("// @ts-nocheck\n");
67
- }
68
- builder.push("\n");
69
- writeLeadingComments(file.getPackageComments());
70
- return trimSuffix(builder.join(""), "\n");
14
+ import { codegenInfo, LongType, ScalarType } from "@bufbuild/protobuf";
15
+ const { localName, getUnwrappedFieldType, scalarZeroValue } = codegenInfo;
16
+ /**
17
+ * @deprecated Please use GeneratedFile.string() instead
18
+ */
19
+ export function literalString(value) {
20
+ return ('"' +
21
+ value
22
+ .split("\\")
23
+ .join("\\\\")
24
+ .split('"')
25
+ .join('\\"')
26
+ .split("\r")
27
+ .join("\\r")
28
+ .split("\n")
29
+ .join("\\n") +
30
+ '"');
71
31
  }
72
32
  /**
73
- * Returns an expression for the TypeScript typing of a field,
74
- * and whether the property should be optional.
33
+ * @deprecated
75
34
  */
76
35
  export function getFieldTyping(field, file) {
77
36
  const typing = [];
@@ -133,40 +92,9 @@ export function getFieldTyping(field, file) {
133
92
  }
134
93
  return { typing, optional };
135
94
  }
136
- export function scalarTypeScriptType(type, longType) {
137
- switch (type) {
138
- case ScalarType.STRING:
139
- return "string";
140
- case ScalarType.BOOL:
141
- return "boolean";
142
- case ScalarType.UINT64:
143
- case ScalarType.SFIXED64:
144
- case ScalarType.FIXED64:
145
- case ScalarType.SINT64:
146
- case ScalarType.INT64:
147
- if (longType === LongType.STRING) {
148
- return "string";
149
- }
150
- return "bigint";
151
- case ScalarType.BYTES:
152
- return "Uint8Array";
153
- default:
154
- return "number";
155
- }
156
- }
157
- export function literalString(value) {
158
- return ('"' +
159
- value
160
- .split("\\")
161
- .join("\\\\")
162
- .split('"')
163
- .join('\\"')
164
- .split("\r")
165
- .join("\\r")
166
- .split("\n")
167
- .join("\\n") +
168
- '"');
169
- }
95
+ /**
96
+ * @deprecated
97
+ */
170
98
  export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
171
99
  switch (field.fieldKind) {
172
100
  case "enum": {
@@ -222,6 +150,9 @@ export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
222
150
  }
223
151
  return undefined;
224
152
  }
153
+ /**
154
+ * @deprecated
155
+ */
225
156
  export function getFieldIntrinsicDefaultValue(field) {
226
157
  if (field.repeated) {
227
158
  return {
@@ -257,8 +188,7 @@ export function getFieldIntrinsicDefaultValue(field) {
257
188
  defaultValue = literalString("");
258
189
  }
259
190
  else {
260
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
261
- defaultValue = scalarDefaultValue(field.scalar, field.longType);
191
+ defaultValue = scalarZeroValue(field.scalar, field.longType);
262
192
  if (typeof defaultValue === "string") {
263
193
  defaultValue = literalString(defaultValue);
264
194
  }
@@ -274,3 +204,27 @@ export function getFieldIntrinsicDefaultValue(field) {
274
204
  typingInferrable,
275
205
  };
276
206
  }
207
+ /**
208
+ * @deprecated
209
+ */
210
+ function scalarTypeScriptType(type, longType) {
211
+ switch (type) {
212
+ case ScalarType.STRING:
213
+ return "string";
214
+ case ScalarType.BOOL:
215
+ return "boolean";
216
+ case ScalarType.UINT64:
217
+ case ScalarType.SFIXED64:
218
+ case ScalarType.FIXED64:
219
+ case ScalarType.SINT64:
220
+ case ScalarType.INT64:
221
+ if (longType === LongType.STRING) {
222
+ return "string";
223
+ }
224
+ return "bigint";
225
+ case ScalarType.BYTES:
226
+ return "Uint8Array";
227
+ default:
228
+ return "number";
229
+ }
230
+ }
@@ -0,0 +1,27 @@
1
+ import type { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf";
2
+ import { LongType, ScalarType } from "@bufbuild/protobuf";
3
+ export type LiteralProtoInt64 = {
4
+ readonly kind: "es_proto_int64";
5
+ type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
6
+ longType: LongType;
7
+ value: bigint | string;
8
+ };
9
+ export type LiteralString = {
10
+ readonly kind: "es_string";
11
+ value: string;
12
+ };
13
+ export type RefDescMessage = {
14
+ readonly kind: "es_ref_message";
15
+ type: DescMessage;
16
+ typeOnly: boolean;
17
+ };
18
+ export type RefDescEnum = {
19
+ readonly kind: "es_ref_enum";
20
+ type: DescEnum;
21
+ typeOnly: boolean;
22
+ };
23
+ export type ExportDeclaration = {
24
+ readonly kind: "es_export_decl";
25
+ declaration: string;
26
+ name: string | DescMessage | DescEnum | DescExtension;
27
+ };
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  // Copyright 2021-2024 Buf Technologies, Inc.
3
2
  //
4
3
  // Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,13 +11,4 @@
12
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
12
  // See the License for the specific language governing permissions and
14
13
  // limitations under the License.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.createExportDeclaration = void 0;
17
- function createExportDeclaration(declaration, name) {
18
- return {
19
- kind: "es_export_decl",
20
- declaration,
21
- name,
22
- };
23
- }
24
- exports.createExportDeclaration = createExportDeclaration;
14
+ import { LongType, ScalarType } from "@bufbuild/protobuf";
@@ -1,5 +1,5 @@
1
- import { Target } from "./target.js";
2
- import { RewriteImports } from "./import-path.js";
1
+ import type { Target } from "./target.js";
2
+ import type { RewriteImports } from "./import-path.js";
3
3
  export interface ParsedParameter {
4
4
  targets: Target[];
5
5
  tsNocheck: boolean;
@@ -1,9 +1,9 @@
1
1
  import type { CodeGeneratorRequest, DescFile } from "@bufbuild/protobuf";
2
2
  import { FeatureSetDefaults } from "@bufbuild/protobuf";
3
3
  import type { FileInfo, GeneratedFile } from "./generated-file.js";
4
- import { RuntimeImports } from "./runtime-imports.js";
4
+ import type { RuntimeImports } from "./runtime-imports.js";
5
5
  import type { Target } from "./target.js";
6
- import { ParsedParameter } from "./parameter";
6
+ import type { ParsedParameter } from "./parameter.js";
7
7
  /**
8
8
  * Schema describes the files and types that the plugin is requested to
9
9
  * generate.
@@ -11,12 +11,12 @@
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, createDescriptorSet, } from "@bufbuild/protobuf";
14
+ import { codegenInfo, createDescriptorSet, FeatureSetDefaults, } from "@bufbuild/protobuf";
15
15
  import { createGeneratedFile } from "./generated-file.js";
16
16
  import { createRuntimeImports } from "./runtime-imports.js";
17
17
  import { createImportSymbol } from "./import-symbol.js";
18
18
  import { deriveImportPath, makeImportPath, rewriteImportPath, } from "./import-path.js";
19
- import { makeFilePreamble } from "./gencommon";
19
+ import { makeFilePreamble } from "./file-preamble.js";
20
20
  export function createSchema(request, parameter, pluginName, pluginVersion, featureSetDefaults) {
21
21
  const descriptorSet = createDescriptorSet(request.protoFile, {
22
22
  featureSetDefaults,
@@ -112,7 +112,7 @@ export function transpile(files, transpileJs, transpileDts, jsImportStyle) {
112
112
  throw err;
113
113
  }
114
114
  if (result.emitSkipped) {
115
- throw Error("An problem occurred during transpilation and files were not generated. Contact the plugin author for support.");
115
+ throw Error("A problem occurred during transpilation and files were not generated. Contact the plugin author for support.");
116
116
  }
117
117
  return results;
118
118
  }
@@ -1,4 +1,6 @@
1
1
  export declare class PluginOptionError extends Error {
2
+ name: string;
2
3
  constructor(option: string, reason?: unknown);
3
4
  }
4
5
  export declare function reasonToString(reason: unknown): string;
6
+ export declare function isPluginOptionError(arg: unknown): arg is PluginOptionError;
package/dist/esm/error.js CHANGED
@@ -17,6 +17,7 @@ export class PluginOptionError extends Error {
17
17
  super(detail.length > 0
18
18
  ? `invalid option "${option}": ${detail}`
19
19
  : `invalid option "${option}"`);
20
+ this.name = "PluginOptionError";
20
21
  }
21
22
  }
22
23
  export function reasonToString(reason) {
@@ -28,3 +29,9 @@ export function reasonToString(reason) {
28
29
  }
29
30
  return String(reason);
30
31
  }
32
+ export function isPluginOptionError(arg) {
33
+ if (!(arg instanceof Error)) {
34
+ return false;
35
+ }
36
+ return arg.name === "PluginOptionError";
37
+ }
@@ -1,5 +1,5 @@
1
- import { Schema as SchemaInternal } from "./ecmascript/schema.js";
2
- export { Plugin } from "./plugin.js";
1
+ import type { Schema as SchemaInternal } from "./ecmascript/schema.js";
2
+ export type { Plugin } from "./plugin.js";
3
3
  export { runNodeJs } from "./run-node.js";
4
4
  export { createEcmaScriptPlugin } from "./create-es-plugin.js";
5
5
  /**
@@ -12,7 +12,7 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { CodeGeneratorRequest } from "@bufbuild/protobuf";
15
- import { PluginOptionError, reasonToString } from "./error.js";
15
+ import { isPluginOptionError, reasonToString } from "./error.js";
16
16
  /**
17
17
  * Run a plugin with Node.js.
18
18
  *
@@ -43,7 +43,7 @@ export function runNodeJs(plugin) {
43
43
  })
44
44
  .then(() => process.exit(0))
45
45
  .catch((reason) => {
46
- const message = reason instanceof PluginOptionError
46
+ const message = isPluginOptionError(reason)
47
47
  ? reason.message
48
48
  : reasonToString(reason);
49
49
  process.stderr.write(`${plugin.name}: ${message}\n`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "1.7.2",
3
+ "version": "1.9.0",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
6
  "repository": {
@@ -10,10 +10,9 @@
10
10
  },
11
11
  "scripts": {
12
12
  "clean": "rm -rf ./dist/*",
13
- "build": "npm run build:cjs && npm run build:esm && npm run build:proxy",
14
- "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
15
- "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --outDir ./dist/esm --declaration --declarationDir ./dist/esm",
16
- "build:proxy": "node ../../scripts/gen-esm-proxy.mjs . ecmascript",
13
+ "build": "npm run build:cjs && npm run build:esm",
14
+ "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --verbatimModuleSyntax false --moduleResolution node10 --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
15
+ "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/esm --declaration --declarationDir ./dist/esm",
17
16
  "attw": "attw --pack"
18
17
  },
19
18
  "type": "module",
@@ -21,20 +20,10 @@
21
20
  "main": "./dist/cjs/index.js",
22
21
  "exports": {
23
22
  ".": {
24
- "node": {
25
- "import": "./dist/proxy/index.js",
26
- "require": "./dist/cjs/index.js"
27
- },
28
- "module": "./dist/esm/index.js",
29
23
  "import": "./dist/esm/index.js",
30
24
  "require": "./dist/cjs/index.js"
31
25
  },
32
26
  "./ecmascript": {
33
- "node": {
34
- "import": "./dist/proxy/ecmascript/index.js",
35
- "require": "./dist/cjs/ecmascript/index.js"
36
- },
37
- "module": "./dist/esm/ecmascript/index.js",
38
27
  "import": "./dist/esm/ecmascript/index.js",
39
28
  "require": "./dist/cjs/ecmascript/index.js"
40
29
  }
@@ -47,7 +36,7 @@
47
36
  }
48
37
  },
49
38
  "dependencies": {
50
- "@bufbuild/protobuf": "1.7.2",
39
+ "@bufbuild/protobuf": "1.9.0",
51
40
  "@typescript/vfs": "^1.4.0",
52
41
  "typescript": "4.5.2"
53
42
  },
@@ -1,7 +0,0 @@
1
- import { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf";
2
- export type ExportDeclaration = {
3
- readonly kind: "es_export_decl";
4
- declaration: string;
5
- name: string | DescMessage | DescEnum | DescExtension;
6
- };
7
- export declare function createExportDeclaration(declaration: string, name: ExportDeclaration["name"]): ExportDeclaration;
@@ -1,7 +0,0 @@
1
- import { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf";
2
- export type ExportDeclaration = {
3
- readonly kind: "es_export_decl";
4
- declaration: string;
5
- name: string | DescMessage | DescEnum | DescExtension;
6
- };
7
- export declare function createExportDeclaration(declaration: string, name: ExportDeclaration["name"]): ExportDeclaration;
@@ -1 +0,0 @@
1
- export * from "../../cjs/ecmascript/index.js";
@@ -1 +0,0 @@
1
- export * from "../../cjs/ecmascript/index.js";
@@ -1 +0,0 @@
1
- export * from "../cjs/index.js";
@@ -1 +0,0 @@
1
- export * from "../cjs/index.js";