ts-proto 1.166.4 → 1.167.1

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.markdown CHANGED
@@ -539,7 +539,9 @@ Generated code will be placed in the Gradle build directory.
539
539
 
540
540
  This will disable `exportCommonSymbols` to avoid name collisions on the common symbols.
541
541
 
542
- - With `--emitDefaultValues=json-methods`, the generated toJSON method will emit scalars like `0` and `""` as json fields.
542
+ - With `--ts_proto_opt=emitDefaultValues=json-methods`, the generated toJSON method will emit scalars like `0` and `""` as json fields.
543
+
544
+ - With `--ts_proto_opt=comments=false`, comments won't be copied from the proto files to the generated code.
543
545
 
544
546
  ### NestJS Support
545
547
 
package/build/enums.js CHANGED
@@ -11,7 +11,7 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
11
11
  const { options } = ctx;
12
12
  const chunks = [];
13
13
  let unrecognizedEnum = { present: false };
14
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = enumDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
14
+ (0, utils_1.maybeAddComment)(options, sourceInfo, chunks, (_a = enumDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
15
15
  if (options.enumsAsLiterals) {
16
16
  chunks.push((0, ts_poet_1.code) `export const ${(0, ts_poet_1.def)(fullName)} = {`);
17
17
  }
@@ -27,7 +27,7 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
27
27
  if (valueDesc.number === options.unrecognizedEnumValue) {
28
28
  unrecognizedEnum = { present: true, name: memberName };
29
29
  }
30
- (0, utils_1.maybeAddComment)(info, chunks, (_a = valueDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated, `${memberName} - `);
30
+ (0, utils_1.maybeAddComment)(options, info, chunks, (_a = valueDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated, `${memberName} - `);
31
31
  chunks.push((0, ts_poet_1.code) `${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`);
32
32
  });
33
33
  if (options.unrecognizedEnum && !unrecognizedEnum.present) {
@@ -13,7 +13,7 @@ const utils_1 = require("./utils");
13
13
  function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
14
14
  var _a, _b, _c;
15
15
  const chunks = [];
16
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
16
+ (0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
17
17
  // Service definition type
18
18
  const name = (0, ts_poet_1.def)(`${serviceDesc.name}Definition`);
19
19
  chunks.push((0, ts_poet_1.code) `
@@ -31,7 +31,7 @@ function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc
31
31
  `);
32
32
  for (const [index, methodDesc] of serviceDesc.method.entries()) {
33
33
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
34
- (0, utils_1.maybeAddComment)(info, chunks, (_c = methodDesc.options) === null || _c === void 0 ? void 0 : _c.deprecated);
34
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_c = methodDesc.options) === null || _c === void 0 ? void 0 : _c.deprecated);
35
35
  chunks.push((0, ts_poet_1.code) `
36
36
  ${(0, case_1.uncapitalize)(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
37
37
  `);
@@ -41,7 +41,7 @@ exports.generateGrpcJsService = generateGrpcJsService;
41
41
  function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
42
42
  var _a, _b;
43
43
  const chunks = [];
44
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
44
+ (0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
45
45
  // Service definition type
46
46
  const name = (0, ts_poet_1.def)(`${serviceDesc.name}Service`);
47
47
  chunks.push((0, ts_poet_1.code) `
@@ -56,7 +56,7 @@ function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
56
56
  const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
57
57
  const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
58
58
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
59
- (0, utils_1.maybeAddComment)(info, chunks, (_b = methodDesc.options) === null || _b === void 0 ? void 0 : _b.deprecated);
59
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_b = methodDesc.options) === null || _b === void 0 ? void 0 : _b.deprecated);
60
60
  const inputEncoder = (0, encode_1.generateEncoder)(ctx, methodDesc.inputType);
61
61
  const outputEncoder = (0, encode_1.generateEncoder)(ctx, methodDesc.outputType);
62
62
  const inputDecoder = (0, encode_1.generateDecoder)(ctx, methodDesc.inputType);
@@ -87,7 +87,7 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
87
87
  const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
88
88
  const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
89
89
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
90
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
90
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
91
91
  const callType = methodDesc.clientStreaming
92
92
  ? methodDesc.serverStreaming
93
93
  ? handleBidiStreamingCall
@@ -111,7 +111,7 @@ function generateClientStub(ctx, sourceInfo, serviceDesc) {
111
111
  const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
112
112
  const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
113
113
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
114
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
114
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
115
115
  const responseCallback = (0, ts_poet_1.code) `(error: ${ServiceError} | null, response: ${outputType}) => void`;
116
116
  if (methodDesc.clientStreaming) {
117
117
  if (methodDesc.serverStreaming) {
@@ -11,7 +11,7 @@ function generateNestjsServiceController(ctx, fileDesc, sourceInfo, serviceDesc)
11
11
  const { options } = ctx;
12
12
  const chunks = [];
13
13
  const Metadata = (0, ts_poet_1.imp)("Metadata@@grpc/grpc-js");
14
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
14
+ (0, utils_1.maybeAddComment)(options, sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
15
15
  const t = options.context ? `<${main_1.contextTypeVar}>` : "";
16
16
  chunks.push((0, ts_poet_1.code) `
17
17
  export interface ${serviceDesc.name}Controller${t} {
@@ -20,7 +20,7 @@ function generateNestjsServiceController(ctx, fileDesc, sourceInfo, serviceDesc)
20
20
  var _a;
21
21
  (0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
22
22
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
23
- (0, utils_1.maybeAddComment)(info, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
23
+ (0, utils_1.maybeAddComment)(options, info, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
24
24
  const params = [];
25
25
  if (options.context) {
26
26
  params.push((0, ts_poet_1.code) `ctx: Context`);
@@ -74,7 +74,7 @@ function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
74
74
  const { options } = ctx;
75
75
  const chunks = [];
76
76
  const Metadata = (0, ts_poet_1.imp)("Metadata@@grpc/grpc-js");
77
- (0, utils_1.maybeAddComment)(sourceInfo, chunks);
77
+ (0, utils_1.maybeAddComment)(options, sourceInfo, chunks);
78
78
  const t = options.context ? `<${main_1.contextTypeVar}>` : ``;
79
79
  chunks.push((0, ts_poet_1.code) `
80
80
  export interface ${serviceDesc.name}Client${t} {
@@ -98,7 +98,7 @@ function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
98
98
  // Return observable since nestjs client always returns an Observable
99
99
  const returns = (0, types_1.responseObservable)(ctx, methodDesc);
100
100
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
101
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
101
+ (0, utils_1.maybeAddComment)(options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
102
102
  chunks.push((0, ts_poet_1.code) `
103
103
  ${methodDesc.formattedName}(
104
104
  ${(0, ts_poet_1.joinCode)(params, { on: "," })}
@@ -32,7 +32,7 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
32
32
  }
33
33
  const ServerStreamingMethodResult = ctx.utils.NiceGrpcServerStreamingMethodResult;
34
34
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
35
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
35
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
36
36
  if (methodDesc.clientStreaming) {
37
37
  if (methodDesc.serverStreaming) {
38
38
  // bidi streaming
@@ -89,7 +89,7 @@ function generateClientStub(ctx, sourceInfo, serviceDesc) {
89
89
  }
90
90
  const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType, { keepValueType: true });
91
91
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
92
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
92
+ (0, utils_1.maybeAddComment)(ctx.options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
93
93
  if (methodDesc.clientStreaming) {
94
94
  if (methodDesc.serverStreaming) {
95
95
  // bidi streaming
@@ -20,14 +20,14 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
20
20
  var _a;
21
21
  const { options } = ctx;
22
22
  const chunks = [];
23
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
23
+ (0, utils_1.maybeAddComment)(options, sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
24
24
  const maybeTypeVar = options.context ? `<${main_1.contextTypeVar}>` : "";
25
25
  chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(serviceDesc.name)}${maybeTypeVar} {`);
26
26
  serviceDesc.method.forEach((methodDesc, index) => {
27
27
  var _a;
28
28
  (0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
29
29
  const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
30
- (0, utils_1.maybeAddComment)(info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
30
+ (0, utils_1.maybeAddComment)(options, info, chunks, (_a = methodDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
31
31
  const params = [];
32
32
  if (options.context) {
33
33
  params.push((0, ts_poet_1.code) `ctx: Context`);
package/build/main.js CHANGED
@@ -52,7 +52,7 @@ function generateFile(ctx, fileDesc) {
52
52
  // Syntax, unlike most fields, is not repeated and thus does not use an index
53
53
  const sourceInfo = sourceInfo_1.default.fromDescriptor(fileDesc);
54
54
  const headerComment = sourceInfo.lookup(sourceInfo_1.Fields.file.syntax, undefined);
55
- (0, utils_1.maybeAddComment)(headerComment, chunks, (_a = fileDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
55
+ (0, utils_1.maybeAddComment)(options, headerComment, chunks, (_a = fileDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
56
56
  // Apply formatting to methods here, so they propagate globally
57
57
  for (let svc of fileDesc.service) {
58
58
  for (let i = 0; i < svc.method.length; i++) {
@@ -249,6 +249,14 @@ function generateFile(ctx, fileDesc) {
249
249
  if (options.outputSchema) {
250
250
  chunks.push(...(0, schema_1.generateSchema)(ctx, fileDesc, sourceInfo));
251
251
  }
252
+ // https://www.typescriptlang.org/docs/handbook/2/modules.html:
253
+ // > In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.
254
+ // > Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
255
+ //
256
+ // Thus, to mark an empty file a module, we need to add `export {}` to it.
257
+ if (options.esModuleInterop && chunks.length === 0) {
258
+ chunks.push((0, ts_poet_1.code) `export {};`);
259
+ }
252
260
  chunks.push(...Object.values(utils).map((v) => {
253
261
  if (v instanceof ConditionalOutput_1.ConditionalOutput) {
254
262
  return (0, ts_poet_1.code) `${v.ifUsed}`;
@@ -624,7 +632,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
624
632
  var _a;
625
633
  const { options } = ctx;
626
634
  const chunks = [];
627
- (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = messageDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
635
+ (0, utils_1.maybeAddComment)(options, sourceInfo, chunks, (_a = messageDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
628
636
  // interface name should be defined to avoid import collisions
629
637
  chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(fullName)} {`);
630
638
  if ((0, options_1.addTypeToMessages)(options)) {
@@ -643,7 +651,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
643
651
  return;
644
652
  }
645
653
  const info = sourceInfo.lookup(sourceInfo_1.Fields.message.field, index);
646
- (0, utils_1.maybeAddComment)(info, chunks, (_a = fieldDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
654
+ (0, utils_1.maybeAddComment)(options, info, chunks, (_a = fieldDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
647
655
  const fieldKey = (0, utils_1.safeAccessor)((0, utils_1.getFieldName)(fieldDesc, options));
648
656
  const isOptional = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options);
649
657
  const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc, isOptional);
@@ -672,14 +680,14 @@ function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
672
680
  // that ability. For now just concatenate all comments into one big one.
673
681
  let comments: Array<string> = [];
674
682
  const info = sourceInfo.lookup(Fields.message.oneof_decl, oneofIndex);
675
- maybeAddComment(info, (text) => comments.push(text));
683
+ maybeAddComment(options, info, (text) => comments.push(text));
676
684
  messageDesc.field.forEach((field, index) => {
677
685
  if (!isWithinOneOf(field) || field.oneofIndex !== oneofIndex) {
678
686
  return;
679
687
  }
680
688
  const info = sourceInfo.lookup(Fields.message.field, index);
681
689
  const name = maybeSnakeToCamel(field.name, options);
682
- maybeAddComment(info, (text) => comments.push(name + '\n' + text));
690
+ maybeAddComment(options, info, (text) => comments.push(name + '\n' + text));
683
691
  });
684
692
  if (comments.length) {
685
693
  prop = prop.addJavadoc(comments.join('\n'));
@@ -1170,7 +1178,7 @@ function generateEncode(ctx, fullName, messageDesc) {
1170
1178
  for (const v of ${messageProperty}) {
1171
1179
  if (BigInt.asUintN(64, v) !== v) {
1172
1180
  throw new ${utils.globalThis}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
1173
- }
1181
+ }
1174
1182
  writer.${(0, types_1.toReaderCall)(field)}(${rhs("v")});
1175
1183
  }
1176
1184
  writer.ldelim();
@@ -1921,14 +1929,14 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1921
1929
  chunks.push((0, ts_poet_1.code) `
1922
1930
  create<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(base?: I): ${fullName} {
1923
1931
  return ${fullName}.fromPartial(base ?? ({} as any));
1924
- },
1932
+ },
1925
1933
  `);
1926
1934
  }
1927
1935
  else {
1928
1936
  chunks.push((0, ts_poet_1.code) `
1929
1937
  create(base?: ${utils.DeepPartial}<${fullName}>): ${fullName} {
1930
1938
  return ${fullName}.fromPartial(base ?? {});
1931
- },
1939
+ },
1932
1940
  `);
1933
1941
  }
1934
1942
  // create the fromPartial function declaration
package/build/options.js CHANGED
@@ -97,6 +97,7 @@ function defaultOptions() {
97
97
  rpcBeforeRequest: false,
98
98
  rpcAfterResponse: false,
99
99
  rpcErrorHandler: false,
100
+ comments: true,
100
101
  };
101
102
  }
102
103
  exports.defaultOptions = defaultOptions;
package/build/utils.js CHANGED
@@ -79,7 +79,10 @@ exports.upperFirst = upperFirst;
79
79
  // Since we don't know what form the comment originally took, it may contain closing block comments.
80
80
  const CloseComment = /\*\//g;
81
81
  /** Removes potentially harmful characters from comments and pushes it into chunks. */
82
- function maybeAddComment(desc, chunks, deprecated, prefix = "") {
82
+ function maybeAddComment(options, desc, chunks, deprecated, prefix = "") {
83
+ if (!options.comments) {
84
+ return;
85
+ }
83
86
  let lines = [];
84
87
  if (desc.leadingComments || desc.trailingComments) {
85
88
  let content = (desc.leadingComments || desc.trailingComments || "").replace(CloseComment, "* /").trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.166.4",
3
+ "version": "1.167.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",