@bufbuild/protoplugin 2.0.0-alpha.1 → 2.0.0-alpha.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.
Files changed (69) hide show
  1. package/dist/cjs/create-es-plugin.d.ts +3 -3
  2. package/dist/cjs/create-es-plugin.js +6 -6
  3. package/dist/cjs/{ecmascript/file-preamble.js → file-preamble.js} +4 -1
  4. package/dist/cjs/{ecmascript/generated-file.d.ts → generated-file.d.ts} +3 -3
  5. package/dist/cjs/{ecmascript/generated-file.js → generated-file.js} +13 -14
  6. package/dist/cjs/index.d.ts +7 -0
  7. package/dist/cjs/index.js +5 -1
  8. package/dist/cjs/{ecmascript/jsdoc.js → jsdoc.js} +18 -6
  9. package/dist/cjs/{ecmascript/parameter.js → parameter.js} +2 -2
  10. package/dist/{esm/ecmascript → cjs}/printable.d.ts +2 -3
  11. package/dist/cjs/{ecmascript/schema.js → schema.js} +10 -2
  12. package/dist/cjs/source-code-info.d.ts +6 -1
  13. package/dist/cjs/source-code-info.js +74 -47
  14. package/dist/esm/create-es-plugin.d.ts +3 -3
  15. package/dist/esm/create-es-plugin.js +4 -4
  16. package/dist/esm/{ecmascript/file-preamble.js → file-preamble.js} +4 -1
  17. package/dist/esm/{ecmascript/generated-file.d.ts → generated-file.d.ts} +3 -3
  18. package/dist/esm/{ecmascript/generated-file.js → generated-file.js} +12 -13
  19. package/dist/esm/index.d.ts +7 -0
  20. package/dist/esm/index.js +2 -0
  21. package/dist/esm/{ecmascript/jsdoc.js → jsdoc.js} +18 -6
  22. package/dist/esm/{ecmascript/parameter.js → parameter.js} +2 -2
  23. package/dist/{cjs/ecmascript → esm}/printable.d.ts +2 -3
  24. package/dist/esm/{ecmascript/schema.js → schema.js} +11 -3
  25. package/dist/esm/source-code-info.d.ts +6 -1
  26. package/dist/esm/source-code-info.js +73 -47
  27. package/dist/esm/{ecmascript/printable.js → target.js} +1 -1
  28. package/package.json +2 -13
  29. package/dist/cjs/ecmascript/index.d.ts +0 -7
  30. package/dist/cjs/ecmascript/index.js +0 -20
  31. package/dist/esm/ecmascript/index.d.ts +0 -7
  32. package/dist/esm/ecmascript/index.js +0 -15
  33. /package/dist/cjs/{ecmascript/file-preamble.d.ts → file-preamble.d.ts} +0 -0
  34. /package/dist/cjs/{ecmascript/import-path.d.ts → import-path.d.ts} +0 -0
  35. /package/dist/cjs/{ecmascript/import-path.js → import-path.js} +0 -0
  36. /package/dist/cjs/{ecmascript/import-symbol.d.ts → import-symbol.d.ts} +0 -0
  37. /package/dist/cjs/{ecmascript/import-symbol.js → import-symbol.js} +0 -0
  38. /package/dist/cjs/{ecmascript/jsdoc.d.ts → jsdoc.d.ts} +0 -0
  39. /package/dist/cjs/{ecmascript/names.d.ts → names.d.ts} +0 -0
  40. /package/dist/cjs/{ecmascript/names.js → names.js} +0 -0
  41. /package/dist/cjs/{ecmascript/parameter.d.ts → parameter.d.ts} +0 -0
  42. /package/dist/cjs/{ecmascript/printable.js → printable.js} +0 -0
  43. /package/dist/cjs/{ecmascript/runtime-imports.d.ts → runtime-imports.d.ts} +0 -0
  44. /package/dist/cjs/{ecmascript/runtime-imports.js → runtime-imports.js} +0 -0
  45. /package/dist/cjs/{ecmascript/safe-identifier.d.ts → safe-identifier.d.ts} +0 -0
  46. /package/dist/cjs/{ecmascript/safe-identifier.js → safe-identifier.js} +0 -0
  47. /package/dist/cjs/{ecmascript/schema.d.ts → schema.d.ts} +0 -0
  48. /package/dist/cjs/{ecmascript/target.d.ts → target.d.ts} +0 -0
  49. /package/dist/cjs/{ecmascript/target.js → target.js} +0 -0
  50. /package/dist/cjs/{ecmascript/transpile.d.ts → transpile.d.ts} +0 -0
  51. /package/dist/cjs/{ecmascript/transpile.js → transpile.js} +0 -0
  52. /package/dist/esm/{ecmascript/file-preamble.d.ts → file-preamble.d.ts} +0 -0
  53. /package/dist/esm/{ecmascript/import-path.d.ts → import-path.d.ts} +0 -0
  54. /package/dist/esm/{ecmascript/import-path.js → import-path.js} +0 -0
  55. /package/dist/esm/{ecmascript/import-symbol.d.ts → import-symbol.d.ts} +0 -0
  56. /package/dist/esm/{ecmascript/import-symbol.js → import-symbol.js} +0 -0
  57. /package/dist/esm/{ecmascript/jsdoc.d.ts → jsdoc.d.ts} +0 -0
  58. /package/dist/esm/{ecmascript/names.d.ts → names.d.ts} +0 -0
  59. /package/dist/esm/{ecmascript/names.js → names.js} +0 -0
  60. /package/dist/esm/{ecmascript/parameter.d.ts → parameter.d.ts} +0 -0
  61. /package/dist/esm/{ecmascript/target.js → printable.js} +0 -0
  62. /package/dist/esm/{ecmascript/runtime-imports.d.ts → runtime-imports.d.ts} +0 -0
  63. /package/dist/esm/{ecmascript/runtime-imports.js → runtime-imports.js} +0 -0
  64. /package/dist/esm/{ecmascript/safe-identifier.d.ts → safe-identifier.d.ts} +0 -0
  65. /package/dist/esm/{ecmascript/safe-identifier.js → safe-identifier.js} +0 -0
  66. /package/dist/esm/{ecmascript/schema.d.ts → schema.d.ts} +0 -0
  67. /package/dist/esm/{ecmascript/target.d.ts → target.d.ts} +0 -0
  68. /package/dist/esm/{ecmascript/transpile.d.ts → transpile.d.ts} +0 -0
  69. /package/dist/esm/{ecmascript/transpile.js → transpile.js} +0 -0
@@ -11,8 +11,7 @@
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 { protoInt64 } from "@bufbuild/protobuf";
15
- import { LongType, ScalarType } from "@bufbuild/protobuf/reflect";
14
+ import { protoInt64, ScalarType, } from "@bufbuild/protobuf";
16
15
  import { createImportSymbol } from "./import-symbol.js";
17
16
  import { makeImportPathRelative } from "./import-path.js";
18
17
  import { createJsDocTextFromDesc, formatJsDocBlock } from "./jsdoc.js";
@@ -45,7 +44,7 @@ export function createGeneratedFile(name, importPath, jsImportStyle, rewriteImpo
45
44
  }, printables);
46
45
  el.push("\n");
47
46
  },
48
- exportDecl(declaration, name) {
47
+ export(declaration, name) {
49
48
  return {
50
49
  kind: "es_export_stmt",
51
50
  name,
@@ -227,16 +226,15 @@ function printableToEl(opt, printables) {
227
226
  el.push(escapeString(p.value));
228
227
  break;
229
228
  case "es_proto_int64":
230
- switch (p.longType) {
231
- case LongType.STRING:
232
- el.push(escapeString(p.value.toString()));
233
- break;
234
- case LongType.BIGINT:
235
- if (p.value == protoInt64.zero) {
236
- // Loose comparison will match between 0n and 0.
237
- el.push(opt.runtime.protoInt64, ".zero");
238
- break;
239
- }
229
+ if (p.longAsString) {
230
+ el.push(escapeString(p.value.toString()));
231
+ }
232
+ else {
233
+ if (p.value == protoInt64.zero) {
234
+ // Loose comparison will match between 0n and 0.
235
+ el.push(opt.runtime.protoInt64, ".zero");
236
+ }
237
+ else {
240
238
  switch (p.type) {
241
239
  case ScalarType.UINT64:
242
240
  case ScalarType.FIXED64:
@@ -246,6 +244,7 @@ function printableToEl(opt, printables) {
246
244
  el.push(opt.runtime.protoInt64, ".parse(", escapeString(p.value.toString()), ")");
247
245
  break;
248
246
  }
247
+ }
249
248
  }
250
249
  break;
251
250
  default:
@@ -2,3 +2,10 @@ 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
+ export type { Schema } from "./schema.js";
7
+ export type { GeneratedFile, FileInfo } from "./generated-file.js";
8
+ export type { ImportSymbol } from "./import-symbol.js";
9
+ export { createImportSymbol } from "./import-symbol.js";
10
+ export type { Printable } from "./printable.js";
11
+ export { safeIdentifier } from "./safe-identifier.js";
package/dist/esm/index.js CHANGED
@@ -14,3 +14,5 @@
14
14
  export { runNodeJs } from "./run-node.js";
15
15
  export { createEcmaScriptPlugin } from "./create-es-plugin.js";
16
16
  export { getComments, getDeclarationString, getPackageComments, getSyntaxComments, } from "./source-code-info.js";
17
+ export { createImportSymbol } from "./import-symbol.js";
18
+ export { safeIdentifier } from "./safe-identifier.js";
@@ -11,7 +11,8 @@
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 { getComments, getDeclarationString } from "../source-code-info.js";
14
+ import { getComments, getDeclarationString, getFeatureOptionStrings, } from "./source-code-info.js";
15
+ import { parentTypes } from "@bufbuild/protobuf/reflect";
15
16
  export function createJsDocTextFromDesc(desc) {
16
17
  const comments = getComments(desc);
17
18
  let text = "";
@@ -47,18 +48,29 @@ export function createJsDocTextFromDesc(desc) {
47
48
  case "extension":
48
49
  text += `@generated from extension: ${getDeclarationString(desc)};`;
49
50
  break;
51
+ case "message":
52
+ case "enum": {
53
+ text += `@generated from ${desc.toString()}`;
54
+ const featureOptions = getFeatureOptionStrings(desc);
55
+ if (featureOptions.length > 0) {
56
+ text += `\n@generated with ${featureOptions.length > 1 ? "options" : "option"} ${featureOptions.join(", ")}`;
57
+ }
58
+ break;
59
+ }
50
60
  default:
51
61
  text += `@generated from ${desc.toString()}`;
52
62
  break;
53
63
  }
54
- let deprecated = desc.deprecated;
64
+ let deprecated;
55
65
  switch (desc.kind) {
56
- case "enum":
57
- case "message":
58
- case "service":
59
- deprecated = deprecated || desc.file.deprecated;
66
+ case "field":
67
+ case "enum_value":
68
+ case "rpc":
69
+ deprecated = desc.deprecated;
60
70
  break;
61
71
  default:
72
+ deprecated =
73
+ desc.deprecated || parentTypes(desc).some((d) => d.deprecated);
62
74
  break;
63
75
  }
64
76
  if (deprecated) {
@@ -11,14 +11,14 @@
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 { PluginOptionError } from "../error.js";
14
+ import { PluginOptionError } from "./error.js";
15
15
  export function parseParameter(parameter, parseExtraOption) {
16
16
  let targets = ["js", "dts"];
17
17
  let tsNocheck = false;
18
18
  let bootstrapWkt = false;
19
19
  let keepEmptyFiles = false;
20
20
  const rewriteImports = [];
21
- let importExtension = ".js";
21
+ let importExtension = "";
22
22
  let jsImportStyle = "module";
23
23
  const rawParameters = [];
24
24
  for (const { key, value, raw } of splitParameter(parameter)) {
@@ -1,5 +1,4 @@
1
- import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "@bufbuild/protobuf";
2
- import { ScalarType, LongType } from "@bufbuild/protobuf/reflect";
1
+ import type { DescEnum, DescExtension, DescFile, DescMessage, DescService, ScalarType } from "@bufbuild/protobuf";
3
2
  import type { ImportSymbol } from "./import-symbol.js";
4
3
  /**
5
4
  * All types that can be passed to GeneratedFile.print()
@@ -13,7 +12,7 @@ export type ExportStatement = {
13
12
  export type LiteralProtoInt64 = {
14
13
  readonly kind: "es_proto_int64";
15
14
  type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
16
- longType: LongType;
15
+ longAsString: boolean;
17
16
  value: bigint | string;
18
17
  };
19
18
  export type LiteralString = {
@@ -11,9 +11,9 @@
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 { create } from "@bufbuild/protobuf";
14
+ import { create, createFileRegistry } from "@bufbuild/protobuf";
15
15
  import { Edition, FileDescriptorSetDesc } from "@bufbuild/protobuf/wkt";
16
- import { createFileRegistry, nestedTypes } from "@bufbuild/protobuf/reflect";
16
+ import { nestedTypes } from "@bufbuild/protobuf/reflect";
17
17
  import { createGeneratedFile } from "./generated-file.js";
18
18
  import { createImportSymbol } from "./import-symbol.js";
19
19
  import { deriveImportPath, rewriteImportPath } from "./import-path.js";
@@ -88,8 +88,16 @@ function getFilesToGenerate(request, minimumEdition, maximumEdition) {
88
88
  }
89
89
  }
90
90
  }
91
+ // Our goal is to provide options with source retention to plugin authors.
92
+ // CodeGeneratorRequest.proto_file elides options with source retention for
93
+ // files to generate. For these files, we take the file from source_file_descriptors,
94
+ // which does include options with source retention.
95
+ const allProtoWithSourceOptions = request.protoFile.map((protoFile) => {
96
+ const sourceFile = request.sourceFileDescriptors.find((s) => s.name == protoFile.name);
97
+ return sourceFile !== null && sourceFile !== void 0 ? sourceFile : protoFile;
98
+ });
91
99
  const registry = createFileRegistry(create(FileDescriptorSetDesc, {
92
- file: request.protoFile,
100
+ file: allProtoWithSourceOptions,
93
101
  }));
94
102
  const allFiles = [];
95
103
  const filesToGenerate = [];
@@ -1,4 +1,4 @@
1
- import type { DescField, DescEnumValue, DescExtension, DescComments, AnyDesc, DescFile } from "@bufbuild/protobuf";
1
+ import { type AnyDesc, type DescComments, type DescEnumValue, type DescExtension, type DescField, type DescFile } from "@bufbuild/protobuf";
2
2
  /**
3
3
  * Get comments on the package element in the protobuf source.
4
4
  */
@@ -11,6 +11,11 @@ export declare function getSyntaxComments(desc: DescFile): DescComments;
11
11
  * Get comments on the element in the protobuf source.
12
12
  */
13
13
  export declare function getComments(desc: Exclude<AnyDesc, DescFile>): DescComments;
14
+ /**
15
+ * Get feature options set on the element in the protobuf source. This returns
16
+ * compact (e.g. fields) or regular options (e.g. files) as an array of strings.
17
+ */
18
+ export declare function getFeatureOptionStrings(desc: AnyDesc): string[];
14
19
  /**
15
20
  * Return a string that matches the definition of a field in the protobuf
16
21
  * source. Does not take custom options into account.
@@ -11,15 +11,15 @@
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 { isFieldSet } from "@bufbuild/protobuf";
15
- import { ScalarType, protoCamelCase } from "@bufbuild/protobuf/reflect";
16
- import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoDesc, FieldOptions_JSType, FieldOptionsDesc, SourceCodeInfo_LocationDesc, } from "@bufbuild/protobuf/wkt";
14
+ import { isFieldSet, ScalarType, } from "@bufbuild/protobuf";
15
+ import { protoCamelCase, reflect } from "@bufbuild/protobuf/reflect";
16
+ import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoDesc, FieldOptions_JSType, FieldOptionsDesc, FeatureSetDesc, SourceCodeInfo_LocationDesc, FileDescriptorProtoDesc, DescriptorProtoDesc, EnumDescriptorProtoDesc, ServiceDescriptorProtoDesc, } from "@bufbuild/protobuf/wkt";
17
17
  /**
18
18
  * Get comments on the package element in the protobuf source.
19
19
  */
20
20
  export function getPackageComments(desc) {
21
21
  return findComments(desc.proto.sourceCodeInfo, [
22
- FieldNumber.FileDescriptorProto_Package,
22
+ FileDescriptorProtoDesc.field.package.number,
23
23
  ]);
24
24
  }
25
25
  /**
@@ -27,7 +27,7 @@ export function getPackageComments(desc) {
27
27
  */
28
28
  export function getSyntaxComments(desc) {
29
29
  return findComments(desc.proto.sourceCodeInfo, [
30
- FieldNumber.FileDescriptorProto_Syntax,
30
+ FileDescriptorProtoDesc.field.syntax.number,
31
31
  ]);
32
32
  }
33
33
  /**
@@ -41,11 +41,11 @@ export function getComments(desc) {
41
41
  path = desc.parent
42
42
  ? [
43
43
  ...getComments(desc.parent).sourcePath,
44
- FieldNumber.DescriptorProto_EnumType,
44
+ DescriptorProtoDesc.field.enumType.number,
45
45
  desc.parent.proto.enumType.indexOf(desc.proto),
46
46
  ]
47
47
  : [
48
- FieldNumber.FileDescriptorProto_EnumType,
48
+ FileDescriptorProtoDesc.field.enumType.number,
49
49
  desc.file.proto.enumType.indexOf(desc.proto),
50
50
  ];
51
51
  file = desc.file;
@@ -53,7 +53,7 @@ export function getComments(desc) {
53
53
  case "oneof":
54
54
  path = [
55
55
  ...getComments(desc.parent).sourcePath,
56
- FieldNumber.DescriptorProto_OneofDecl,
56
+ DescriptorProtoDesc.field.oneofDecl.number,
57
57
  desc.parent.proto.oneofDecl.indexOf(desc.proto),
58
58
  ];
59
59
  file = desc.parent.file;
@@ -62,11 +62,11 @@ export function getComments(desc) {
62
62
  path = desc.parent
63
63
  ? [
64
64
  ...getComments(desc.parent).sourcePath,
65
- FieldNumber.DescriptorProto_NestedType,
65
+ DescriptorProtoDesc.field.nestedType.number,
66
66
  desc.parent.proto.nestedType.indexOf(desc.proto),
67
67
  ]
68
68
  : [
69
- FieldNumber.FileDescriptorProto_MessageType,
69
+ FileDescriptorProtoDesc.field.messageType.number,
70
70
  desc.file.proto.messageType.indexOf(desc.proto),
71
71
  ];
72
72
  file = desc.file;
@@ -74,7 +74,7 @@ export function getComments(desc) {
74
74
  case "enum_value":
75
75
  path = [
76
76
  ...getComments(desc.parent).sourcePath,
77
- FieldNumber.EnumDescriptorProto_Value,
77
+ EnumDescriptorProtoDesc.field.value.number,
78
78
  desc.parent.proto.value.indexOf(desc.proto),
79
79
  ];
80
80
  file = desc.parent.file;
@@ -82,7 +82,7 @@ export function getComments(desc) {
82
82
  case "field":
83
83
  path = [
84
84
  ...getComments(desc.parent).sourcePath,
85
- FieldNumber.DescriptorProto_Field,
85
+ DescriptorProtoDesc.field.field.number,
86
86
  desc.parent.proto.field.indexOf(desc.proto),
87
87
  ];
88
88
  file = desc.parent.file;
@@ -91,18 +91,18 @@ export function getComments(desc) {
91
91
  path = desc.parent
92
92
  ? [
93
93
  ...getComments(desc.parent).sourcePath,
94
- FieldNumber.DescriptorProto_Extension,
94
+ DescriptorProtoDesc.field.extension.number,
95
95
  desc.parent.proto.extension.indexOf(desc.proto),
96
96
  ]
97
97
  : [
98
- FieldNumber.FileDescriptorProto_Extension,
98
+ FileDescriptorProtoDesc.field.extension.number,
99
99
  desc.file.proto.extension.indexOf(desc.proto),
100
100
  ];
101
101
  file = desc.file;
102
102
  break;
103
103
  case "service":
104
104
  path = [
105
- FieldNumber.FileDescriptorProto_Service,
105
+ FileDescriptorProtoDesc.field.service.number,
106
106
  desc.file.proto.service.indexOf(desc.proto),
107
107
  ];
108
108
  file = desc.file;
@@ -110,7 +110,7 @@ export function getComments(desc) {
110
110
  case "rpc":
111
111
  path = [
112
112
  ...getComments(desc.parent).sourcePath,
113
- FieldNumber.ServiceDescriptorProto_Method,
113
+ ServiceDescriptorProtoDesc.field.method.number,
114
114
  desc.parent.proto.method.indexOf(desc.proto),
115
115
  ];
116
116
  file = desc.parent.file;
@@ -118,6 +118,29 @@ export function getComments(desc) {
118
118
  }
119
119
  return findComments(file.proto.sourceCodeInfo, path);
120
120
  }
121
+ /**
122
+ * Get feature options set on the element in the protobuf source. This returns
123
+ * compact (e.g. fields) or regular options (e.g. files) as an array of strings.
124
+ */
125
+ export function getFeatureOptionStrings(desc) {
126
+ var _a, _b;
127
+ const strings = [];
128
+ const features = (_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.features;
129
+ if (features !== undefined) {
130
+ const r = reflect(FeatureSetDesc, features);
131
+ for (const f of r.fields) {
132
+ if (f.fieldKind != "enum" || !r.isSet(f)) {
133
+ continue;
134
+ }
135
+ const val = r.get(f);
136
+ const name = (_b = f.enum.values.find((v) => v.number == val)) === null || _b === void 0 ? void 0 : _b.name;
137
+ if (name !== undefined) {
138
+ strings.push(`features.${f.name} = ${name}`);
139
+ }
140
+ }
141
+ }
142
+ return strings;
143
+ }
121
144
  /**
122
145
  * Return a string that matches the definition of a field in the protobuf
123
146
  * source. Does not take custom options into account.
@@ -131,7 +154,6 @@ export function getDeclarationString(desc) {
131
154
  }
132
155
  return str;
133
156
  }
134
- const file = desc.kind === "extension" ? desc.file : desc.parent.file;
135
157
  const parts = [];
136
158
  function typeName(f) {
137
159
  if (f.message) {
@@ -146,12 +168,10 @@ export function getDeclarationString(desc) {
146
168
  case "scalar":
147
169
  case "enum":
148
170
  case "message":
149
- if (file.edition === Edition.EDITION_PROTO2 &&
150
- isFieldSet(FieldDescriptorProtoDesc, desc.proto, "label") &&
151
- desc.proto.label == FieldDescriptorProto_Label.REQUIRED) {
171
+ if (fieldHasRequiredKeyword(desc)) {
152
172
  parts.push("required");
153
173
  }
154
- if (desc.optional) {
174
+ if (fieldHasOptionalKeyword(desc)) {
155
175
  parts.push("optional");
156
176
  }
157
177
  parts.push(typeName(desc));
@@ -170,10 +190,10 @@ export function getDeclarationString(desc) {
170
190
  const options = [];
171
191
  const protoOptions = desc.proto.options;
172
192
  if (protoOptions !== undefined &&
173
- isFieldSet(FieldOptionsDesc, protoOptions, "packed")) {
193
+ isFieldSet(protoOptions, FieldOptionsDesc.field.packed)) {
174
194
  options.push(`packed = ${protoOptions.packed.toString()}`);
175
195
  }
176
- if (isFieldSet(FieldDescriptorProtoDesc, desc.proto, "defaultValue")) {
196
+ if (isFieldSet(desc.proto, FieldDescriptorProtoDesc.field.defaultValue)) {
177
197
  let defaultValue = desc.proto.defaultValue;
178
198
  if (desc.proto.type == FieldDescriptorProto_Type.BYTES ||
179
199
  desc.proto.type == FieldDescriptorProto_Type.STRING) {
@@ -185,18 +205,44 @@ export function getDeclarationString(desc) {
185
205
  options.push(`json_name = "${desc.jsonName}"`);
186
206
  }
187
207
  if (protoOptions !== undefined &&
188
- isFieldSet(FieldOptionsDesc, protoOptions, "jstype")) {
208
+ isFieldSet(protoOptions, FieldOptionsDesc.field.jstype)) {
189
209
  options.push(`jstype = ${FieldOptions_JSType[protoOptions.jstype]}`);
190
210
  }
191
211
  if (protoOptions !== undefined &&
192
- isFieldSet(FieldOptionsDesc, protoOptions, "deprecated")) {
212
+ isFieldSet(protoOptions, FieldOptionsDesc.field.deprecated)) {
193
213
  options.push(`deprecated = true`);
194
214
  }
215
+ options.push(...getFeatureOptionStrings(desc));
195
216
  if (options.length > 0) {
196
217
  parts.push("[" + options.join(", ") + "]");
197
218
  }
198
219
  return parts.join(" ");
199
220
  }
221
+ /**
222
+ * Whether this field was declared with `required` in the protobuf source.
223
+ */
224
+ function fieldHasRequiredKeyword(field) {
225
+ const edition = (field.kind == "extension" ? field.file : field.parent.file)
226
+ .edition;
227
+ return (edition == Edition.EDITION_PROTO2 &&
228
+ field.proto.label == FieldDescriptorProto_Label.REQUIRED);
229
+ }
230
+ /**
231
+ * Whether this field was declared with `optional` in the protobuf source.
232
+ * Note that message fields are always optional. It is impossible to determine
233
+ * whether the keyword was used.
234
+ */
235
+ function fieldHasOptionalKeyword(field) {
236
+ const edition = (field.kind == "extension" ? field.file : field.parent.file)
237
+ .edition;
238
+ if (edition == Edition.EDITION_PROTO2) {
239
+ return (!field.oneof && field.proto.label == FieldDescriptorProto_Label.OPTIONAL);
240
+ }
241
+ if (edition == Edition.EDITION_PROTO3) {
242
+ return field.proto.proto3Optional;
243
+ }
244
+ return false;
245
+ }
200
246
  /**
201
247
  * Find comments.
202
248
  */
@@ -216,10 +262,10 @@ function findComments(sourceCodeInfo, sourcePath) {
216
262
  }
217
263
  return {
218
264
  leadingDetached: location.leadingDetachedComments,
219
- leading: isFieldSet(SourceCodeInfo_LocationDesc, location, "leadingComments")
265
+ leading: isFieldSet(location, SourceCodeInfo_LocationDesc.field.leadingComments)
220
266
  ? location.leadingComments
221
267
  : undefined,
222
- trailing: isFieldSet(SourceCodeInfo_LocationDesc, location, "trailingComments")
268
+ trailing: isFieldSet(location, SourceCodeInfo_LocationDesc.field.trailingComments)
223
269
  ? location.trailingComments
224
270
  : undefined,
225
271
  sourcePath,
@@ -230,23 +276,3 @@ function findComments(sourceCodeInfo, sourcePath) {
230
276
  sourcePath,
231
277
  };
232
278
  }
233
- /**
234
- * The following field numbers are used to find comments in
235
- * google.protobuf.SourceCodeInfo.
236
- */
237
- var FieldNumber;
238
- (function (FieldNumber) {
239
- FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package";
240
- FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType";
241
- FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType";
242
- FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service";
243
- FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension";
244
- FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax";
245
- FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field";
246
- FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType";
247
- FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType";
248
- FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension";
249
- FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl";
250
- FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value";
251
- FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method";
252
- })(FieldNumber || (FieldNumber = {}));
@@ -11,4 +11,4 @@
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 { ScalarType, LongType } from "@bufbuild/protobuf/reflect";
14
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "2.0.0-alpha.1",
3
+ "version": "2.0.0-alpha.3",
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": {
@@ -22,21 +22,10 @@
22
22
  ".": {
23
23
  "import": "./dist/esm/index.js",
24
24
  "require": "./dist/cjs/index.js"
25
- },
26
- "./ecmascript": {
27
- "import": "./dist/esm/ecmascript/index.js",
28
- "require": "./dist/cjs/ecmascript/index.js"
29
- }
30
- },
31
- "typesVersions": {
32
- "*": {
33
- "ecmascript": [
34
- "./dist/cjs/ecmascript/index.d.ts"
35
- ]
36
25
  }
37
26
  },
38
27
  "dependencies": {
39
- "@bufbuild/protobuf": "2.0.0-alpha.1",
28
+ "@bufbuild/protobuf": "2.0.0-alpha.3",
40
29
  "@typescript/vfs": "^1.4.0",
41
30
  "typescript": "4.5.2"
42
31
  },
@@ -1,7 +0,0 @@
1
- export type { Target } from "./target.js";
2
- export type { Schema } from "./schema.js";
3
- export type { GeneratedFile, FileInfo } from "./generated-file.js";
4
- export type { ImportSymbol } from "./import-symbol.js";
5
- export { createImportSymbol } from "./import-symbol.js";
6
- export type { Printable } from "./printable.js";
7
- export { safeIdentifier } from "./safe-identifier.js";
@@ -1,20 +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 });
16
- exports.safeIdentifier = exports.createImportSymbol = void 0;
17
- var import_symbol_js_1 = require("./import-symbol.js");
18
- Object.defineProperty(exports, "createImportSymbol", { enumerable: true, get: function () { return import_symbol_js_1.createImportSymbol; } });
19
- var safe_identifier_js_1 = require("./safe-identifier.js");
20
- Object.defineProperty(exports, "safeIdentifier", { enumerable: true, get: function () { return safe_identifier_js_1.safeIdentifier; } });
@@ -1,7 +0,0 @@
1
- export type { Target } from "./target.js";
2
- export type { Schema } from "./schema.js";
3
- export type { GeneratedFile, FileInfo } from "./generated-file.js";
4
- export type { ImportSymbol } from "./import-symbol.js";
5
- export { createImportSymbol } from "./import-symbol.js";
6
- export type { Printable } from "./printable.js";
7
- export { safeIdentifier } from "./safe-identifier.js";
@@ -1,15 +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 { createImportSymbol } from "./import-symbol.js";
15
- export { safeIdentifier } from "./safe-identifier.js";
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes