ts-proto 2.4.1 → 2.4.2

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 (2) hide show
  1. package/build/src/main.js +26 -8
  2. package/package.json +1 -1
package/build/src/main.js CHANGED
@@ -2178,12 +2178,17 @@ function generateFromPartial(ctx, fullName, messageDesc) {
2178
2178
  createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
2179
2179
  }
2180
2180
  chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
2181
- let currentIfTarget = "";
2181
+ let currentSwitchTarget;
2182
2182
  // add a check for each incoming field
2183
2183
  messageDesc.field.forEach((field) => {
2184
2184
  const fieldName = (0, utils_1.getFieldName)(field, options);
2185
2185
  const messageProperty = (0, utils_1.getPropertyAccessor)("message", fieldName);
2186
2186
  const objectProperty = (0, utils_1.getPropertyAccessor)("object", fieldName);
2187
+ if (currentSwitchTarget && !(0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
2188
+ // We are exiting a switch, we need to close it
2189
+ chunks.push((0, ts_poet_1.code) `}`);
2190
+ currentSwitchTarget = undefined;
2191
+ }
2187
2192
  const readSnippet = (from) => {
2188
2193
  if (((0, types_1.isLong)(field) || (0, types_1.isLongValueType)(field)) &&
2189
2194
  options.forceLong === options_1.LongOption.LONG &&
@@ -2300,16 +2305,24 @@ function generateFromPartial(ctx, fullName, messageDesc) {
2300
2305
  const oneofNameWithObject = (0, utils_1.getPropertyAccessor)("object", oneofName);
2301
2306
  const valueName = (0, utils_1.oneofValueName)(fieldName, options);
2302
2307
  const v = readSnippet(`${oneofNameWithObject}.${valueName}`);
2308
+ // If we are entering a new switch, close the previous one
2309
+ if (currentSwitchTarget !== undefined && currentSwitchTarget !== oneofNameWithObject) {
2310
+ chunks.push((0, ts_poet_1.code) `}`);
2311
+ currentSwitchTarget = undefined;
2312
+ }
2313
+ if (currentSwitchTarget === undefined) {
2314
+ chunks.push((0, ts_poet_1.code) `switch (${oneofNameWithObject}?.$case) {`);
2315
+ }
2303
2316
  chunks.push((0, ts_poet_1.code) `
2304
- ${currentIfTarget === oneofNameWithObject ? "else " : ""}if (
2305
- ${oneofNameWithObject}?.$case === '${fieldName}'
2306
- && ${oneofNameWithObject}?.${valueName} !== undefined
2307
- && ${oneofNameWithObject}?.${valueName} !== null
2308
- ) {
2309
- ${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} };
2317
+ case '${fieldName}': {
2318
+ if (${oneofNameWithObject}?.${valueName} !== undefined
2319
+ && ${oneofNameWithObject}?.${valueName} !== null) {
2320
+ ${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} };
2321
+ }
2322
+ break;
2310
2323
  }
2311
2324
  `);
2312
- currentIfTarget = oneofNameWithObject;
2325
+ currentSwitchTarget = oneofNameWithObject;
2313
2326
  }
2314
2327
  else if (readSnippet(`x`).toCodeString([]) == "x") {
2315
2328
  // An optimized case of the else below that works when `readSnippet` returns the plain input
@@ -2325,6 +2338,11 @@ function generateFromPartial(ctx, fullName, messageDesc) {
2325
2338
  `);
2326
2339
  }
2327
2340
  });
2341
+ if (currentSwitchTarget) {
2342
+ // We are exiting a switch, we need to close it
2343
+ chunks.push((0, ts_poet_1.code) `}`);
2344
+ currentSwitchTarget = "";
2345
+ }
2328
2346
  // and then wrap up the switch/while/return
2329
2347
  chunks.push((0, ts_poet_1.code) `return message;`);
2330
2348
  chunks.push((0, ts_poet_1.code) `}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.4.1",
3
+ "version": "2.4.2",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",