From 3e1fe07ba7d72fa34f121e84811cb362862db8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 23 Dec 2021 09:49:17 +0100 Subject: [PATCH] [REF] compiler: factorize a common pattern --- src/compiler/code_generator.ts | 45 +++++++++++++--------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index 1494c9f4..22196a8e 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -287,6 +287,18 @@ export class CodeGenerator { return code; } + compileInNewTarget(prefix: string, ast: AST, ctx: Context): string { + const name = this.generateId(prefix); + const initialTarget = this.target; + const target = new CodeTarget(name); + this.targets.push(target); + this.target = target; + const subCtx: Context = createContext(ctx); + this.compileAST(ast, subCtx); + this.target = initialTarget; + return name; + } + addLine(line: string) { this.target.addLine(line); } @@ -296,10 +308,6 @@ export class CodeGenerator { return prefix + this.ids[prefix]; } - generateBlockName(): string { - return `block${this.blocks.length + 1}`; - } - insertAnchor(block: BlockDescription) { const tag = `block-child-${block.children.length}`; const anchor = xmlDoc.createElement(tag); @@ -966,15 +974,9 @@ export class CodeGenerator { this.helpers.add("isBoundary").add("withDefault"); const expr = ast.value ? compileExpr(ast.value || "") : "null"; if (ast.body) { - const initialTarget = this.target; this.helpers.add("LazyValue"); - let name = this.generateId("value"); - const fn = new CodeTarget(name); - this.targets.push(fn); - this.target = fn; - const subCtx: Context = createContext(ctx); - this.compileAST({ type: ASTType.Multi, content: ast.body }, subCtx); - this.target = initialTarget; + const bodyAst: AST = { type: ASTType.Multi, content: ast.body }; + const name = this.compileInNewTarget("value", bodyAst, ctx); let value = `new LazyValue(${name}, ctx, node)`; value = ast.value ? (value ? `withDefault(${expr}, ${value})` : expr) : value; this.addLine(`ctx[\`${ast.name}\`] = ${value};`); @@ -1036,14 +1038,9 @@ export class CodeGenerator { this.addLine(`const ${ctxStr} = capture(ctx);`); } let slotStr: string[] = []; - const initialTarget = this.target; for (let slotName in ast.slots) { - let name = this.generateId("slot"); - const slot = new CodeTarget(name); - this.targets.push(slot); - this.target = slot; - const subCtx: Context = createContext(ctx); - this.compileAST(ast.slots[slotName].content, subCtx); + const slotAst = ast.slots[slotName].content; + const name = this.compileInNewTarget("slot", slotAst, ctx); const params = [`__render: ${name}, __ctx: ${ctxStr}`]; const scope = ast.slots[slotName].scope; if (scope) { @@ -1057,7 +1054,6 @@ export class CodeGenerator { const slotInfo = `{${params.join(", ")}}`; slotStr.push(`'${slotName}': ${slotInfo}`); } - this.target = initialTarget; slotDef = `{${slotStr.join(", ")}}`; } @@ -1142,14 +1138,7 @@ export class CodeGenerator { } if (ast.defaultContent) { - let name = this.generateId("defaultContent"); - const slot = new CodeTarget(name); - this.targets.push(slot); - const initialTarget = this.target; - const subCtx: Context = createContext(ctx); - this.target = slot; - this.compileAST(ast.defaultContent, subCtx); - this.target = initialTarget; + const name = this.compileInNewTarget("defaultContent", ast.defaultContent, ctx); blockString = `callSlot(ctx, node, key, ${slotName}, ${dynamic}, ${scope}, ${name})`; } else { if (dynamic) {