diff --git a/implementations/javascript/packages/schema/src/compiler/context.ts b/implementations/javascript/packages/schema/src/compiler/context.ts index e0e8a48..d2fbf19 100644 --- a/implementations/javascript/packages/schema/src/compiler/context.ts +++ b/implementations/javascript/packages/schema/src/compiler/context.ts @@ -103,19 +103,22 @@ export class FunctionContext { ... items); } - withCapture(fieldName: string | undefined, - sourceExpr: string, - ks: (sourceExpr: string) => Item[], - withCheck = true): Item + withCapture( + fieldName: string | undefined, sourceExpr: string, ks: (sourceExpr: string) => R): R { if (fieldName !== void 0) this.captures.push({ fieldName, sourceExpr }); - const result = withCheck - ? seq(`if (${sourceExpr} !== void 0) `, this.block(() => ks(sourceExpr))) - : block(... ks(sourceExpr)); + const result = ks(sourceExpr); if (fieldName !== void 0) this.captures.pop(); return result; } + convertCapture( + fieldName: string | undefined, sourceExpr: string, ks: (sourceExpr: string) => Item[]): Item + { + return this.withCapture(fieldName, sourceExpr, sourceExpr => + seq(`if (${sourceExpr} !== void 0) `, this.block(() => ks(sourceExpr)))); + } + buildCapturedCompound(dest: string): Item { return seq(`${dest} = `, braces( ... variantInitFor(this.variantName), diff --git a/implementations/javascript/packages/schema/src/compiler/converter.ts b/implementations/javascript/packages/schema/src/compiler/converter.ts index b4519db..3a8fd4d 100644 --- a/implementations/javascript/packages/schema/src/compiler/converter.ts +++ b/implementations/javascript/packages/schema/src/compiler/converter.ts @@ -47,8 +47,7 @@ function converterForAlternative( } else if (ctx.variantName !== void 0) { return [ctx.withCapture('value', simpleValue, - () => [ctx.buildCapturedCompound(dest)], - false)]; + () => ctx.buildCapturedCompound(dest))]; } else { return [`${dest} = ${simpleValue}`]; } @@ -102,7 +101,7 @@ function converterForArray(ctx: FunctionContext, ... converterFor(ctx, arrayType, v, vv => [`${r}.push(${vv})`, `continue`]), seq(`${r} = void 0`), seq(`break`)])), - ctx.withCapture(M.nameFor(arrayType), r, k)]; + ctx.convertCapture(M.nameFor(arrayType), r, k)]; }; return (checkArray ? seq(`if (_.Array.isArray(${src})) `, ctx.block(postCheck)) @@ -122,7 +121,7 @@ function converterFor( if (M.isSimplePattern(p)) { const dest = ctx.gentemp(typeFor(ctx.mod, p), ` | undefined`); return [... converterForSimple(ctx, p, src, dest), - ctx.withCapture(maybeName, dest, ks)]; + ctx.convertCapture(maybeName, dest, ks)]; } else { switch (p.label) { case M.$setof: { @@ -137,7 +136,7 @@ function converterFor( [`${r}.add(${vv})`, `continue`]), seq(`${r} = void 0`), seq(`break`)])), - ctx.withCapture(maybeName, r, ks)]))]; + ctx.convertCapture(maybeName, r, ks)]))]; } case M.$dictof: { const keyPattern = p[0]; @@ -154,7 +153,7 @@ function converterFor( [`${r}.set(${kk}, ${vv})`, `continue`])), seq(`${r} = void 0`), seq(`break`)])), - ctx.withCapture(maybeName, r, ks)]))]; + ctx.convertCapture(maybeName, r, ks)]))]; } default: { const arrayType = M.simpleArray(p);