mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] compiler: factorize a common pattern
This commit is contained in:
committed by
Aaron Bohy
parent
5bf47500d5
commit
3e1fe07ba7
@@ -287,6 +287,18 @@ export class CodeGenerator {
|
|||||||
return code;
|
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) {
|
addLine(line: string) {
|
||||||
this.target.addLine(line);
|
this.target.addLine(line);
|
||||||
}
|
}
|
||||||
@@ -296,10 +308,6 @@ export class CodeGenerator {
|
|||||||
return prefix + this.ids[prefix];
|
return prefix + this.ids[prefix];
|
||||||
}
|
}
|
||||||
|
|
||||||
generateBlockName(): string {
|
|
||||||
return `block${this.blocks.length + 1}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
insertAnchor(block: BlockDescription) {
|
insertAnchor(block: BlockDescription) {
|
||||||
const tag = `block-child-${block.children.length}`;
|
const tag = `block-child-${block.children.length}`;
|
||||||
const anchor = xmlDoc.createElement(tag);
|
const anchor = xmlDoc.createElement(tag);
|
||||||
@@ -966,15 +974,9 @@ export class CodeGenerator {
|
|||||||
this.helpers.add("isBoundary").add("withDefault");
|
this.helpers.add("isBoundary").add("withDefault");
|
||||||
const expr = ast.value ? compileExpr(ast.value || "") : "null";
|
const expr = ast.value ? compileExpr(ast.value || "") : "null";
|
||||||
if (ast.body) {
|
if (ast.body) {
|
||||||
const initialTarget = this.target;
|
|
||||||
this.helpers.add("LazyValue");
|
this.helpers.add("LazyValue");
|
||||||
let name = this.generateId("value");
|
const bodyAst: AST = { type: ASTType.Multi, content: ast.body };
|
||||||
const fn = new CodeTarget(name);
|
const name = this.compileInNewTarget("value", bodyAst, ctx);
|
||||||
this.targets.push(fn);
|
|
||||||
this.target = fn;
|
|
||||||
const subCtx: Context = createContext(ctx);
|
|
||||||
this.compileAST({ type: ASTType.Multi, content: ast.body }, subCtx);
|
|
||||||
this.target = initialTarget;
|
|
||||||
let value = `new LazyValue(${name}, ctx, node)`;
|
let value = `new LazyValue(${name}, ctx, node)`;
|
||||||
value = ast.value ? (value ? `withDefault(${expr}, ${value})` : expr) : value;
|
value = ast.value ? (value ? `withDefault(${expr}, ${value})` : expr) : value;
|
||||||
this.addLine(`ctx[\`${ast.name}\`] = ${value};`);
|
this.addLine(`ctx[\`${ast.name}\`] = ${value};`);
|
||||||
@@ -1036,14 +1038,9 @@ export class CodeGenerator {
|
|||||||
this.addLine(`const ${ctxStr} = capture(ctx);`);
|
this.addLine(`const ${ctxStr} = capture(ctx);`);
|
||||||
}
|
}
|
||||||
let slotStr: string[] = [];
|
let slotStr: string[] = [];
|
||||||
const initialTarget = this.target;
|
|
||||||
for (let slotName in ast.slots) {
|
for (let slotName in ast.slots) {
|
||||||
let name = this.generateId("slot");
|
const slotAst = ast.slots[slotName].content;
|
||||||
const slot = new CodeTarget(name);
|
const name = this.compileInNewTarget("slot", slotAst, ctx);
|
||||||
this.targets.push(slot);
|
|
||||||
this.target = slot;
|
|
||||||
const subCtx: Context = createContext(ctx);
|
|
||||||
this.compileAST(ast.slots[slotName].content, subCtx);
|
|
||||||
const params = [`__render: ${name}, __ctx: ${ctxStr}`];
|
const params = [`__render: ${name}, __ctx: ${ctxStr}`];
|
||||||
const scope = ast.slots[slotName].scope;
|
const scope = ast.slots[slotName].scope;
|
||||||
if (scope) {
|
if (scope) {
|
||||||
@@ -1057,7 +1054,6 @@ export class CodeGenerator {
|
|||||||
const slotInfo = `{${params.join(", ")}}`;
|
const slotInfo = `{${params.join(", ")}}`;
|
||||||
slotStr.push(`'${slotName}': ${slotInfo}`);
|
slotStr.push(`'${slotName}': ${slotInfo}`);
|
||||||
}
|
}
|
||||||
this.target = initialTarget;
|
|
||||||
slotDef = `{${slotStr.join(", ")}}`;
|
slotDef = `{${slotStr.join(", ")}}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1142,14 +1138,7 @@ export class CodeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ast.defaultContent) {
|
if (ast.defaultContent) {
|
||||||
let name = this.generateId("defaultContent");
|
const name = this.compileInNewTarget("defaultContent", ast.defaultContent, ctx);
|
||||||
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;
|
|
||||||
blockString = `callSlot(ctx, node, key, ${slotName}, ${dynamic}, ${scope}, ${name})`;
|
blockString = `callSlot(ctx, node, key, ${slotName}, ${dynamic}, ${scope}, ${name})`;
|
||||||
} else {
|
} else {
|
||||||
if (dynamic) {
|
if (dynamic) {
|
||||||
|
|||||||
Reference in New Issue
Block a user