[FIX] compiler: handle t-set as functions

This commit is contained in:
Géry Debongnie
2021-12-22 15:06:28 +01:00
committed by Aaron Bohy
parent 75ad0835e9
commit 5bf47500d5
9 changed files with 416 additions and 40 deletions
+23
View File
@@ -101,6 +101,25 @@ function shallowEqual(l1: any[], l2: any[]): boolean {
return true;
}
class LazyValue {
fn: any;
ctx: any;
node: any;
constructor(fn: any, ctx: any, node: any) {
this.fn = fn;
this.ctx = capture(ctx);
this.node = node;
}
evaluate(): any {
return this.fn(this.ctx, this.node);
}
toString() {
return this.evaluate().toString();
}
}
/*
* Safely outputs `value` as a block depending on the nature of `value`
*/
@@ -113,6 +132,9 @@ export function safeOutput(value: any): ReturnType<typeof toggler> {
if (value instanceof Markup) {
safeKey = `string_safe`;
block = html(value as string);
} else if (value instanceof LazyValue) {
safeKey = `lazy_value`;
block = value.evaluate();
} else if (typeof value === "string") {
safeKey = "string_unsafe";
block = text(value);
@@ -172,6 +194,7 @@ export const UTILS = {
shallowEqual,
toNumber,
validateProps,
LazyValue,
safeOutput,
bind,
};
+9 -2
View File
@@ -966,10 +966,17 @@ 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);
const nextId = `b${BlockDescription.nextBlockId}`;
this.compileAST({ type: ASTType.Multi, content: ast.body }, subCtx);
const value = ast.value ? (nextId ? `withDefault(${expr}, ${nextId})` : expr) : nextId;
this.target = initialTarget;
let value = `new LazyValue(${name}, ctx, node)`;
value = ast.value ? (value ? `withDefault(${expr}, ${value})` : expr) : value;
this.addLine(`ctx[\`${ast.name}\`] = ${value};`);
} else {
let value: string;