Refactor withCapture
This commit is contained in:
parent
d372977023
commit
c8b752a73b
|
@ -103,19 +103,22 @@ export class FunctionContext {
|
|||
... items);
|
||||
}
|
||||
|
||||
withCapture(fieldName: string | undefined,
|
||||
sourceExpr: string,
|
||||
ks: (sourceExpr: string) => Item[],
|
||||
withCheck = true): Item
|
||||
withCapture<R>(
|
||||
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),
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue