|
|
|
|
@ -9,31 +9,44 @@ import * as b from '../../../../utils/builders.js';
|
|
|
|
|
*/
|
|
|
|
|
export function IfBlock(node, context) {
|
|
|
|
|
context.state.template.push('<!>');
|
|
|
|
|
const statements = [];
|
|
|
|
|
|
|
|
|
|
const consequent = /** @type {BlockStatement} */ (context.visit(node.consequent));
|
|
|
|
|
const consequent_id = context.state.scope.generate('consequent');
|
|
|
|
|
|
|
|
|
|
context.state.init.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
|
|
|
|
|
statements.push(b.var(b.id(consequent_id), b.arrow([b.id('$$anchor')], consequent)));
|
|
|
|
|
|
|
|
|
|
let alternate_id;
|
|
|
|
|
|
|
|
|
|
if (node.alternate) {
|
|
|
|
|
const alternate = /** @type {BlockStatement} */ (context.visit(node.alternate));
|
|
|
|
|
alternate_id = context.state.scope.generate('alternate');
|
|
|
|
|
context.state.init.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
|
|
|
|
|
statements.push(b.var(b.id(alternate_id), b.arrow([b.id('$$anchor')], alternate)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @type {Expression[]} */
|
|
|
|
|
const args = [
|
|
|
|
|
context.state.node,
|
|
|
|
|
b.arrow(
|
|
|
|
|
[b.id('$$branch')],
|
|
|
|
|
[b.id('$$render')],
|
|
|
|
|
b.block([
|
|
|
|
|
b.if(
|
|
|
|
|
/** @type {Expression} */ (context.visit(node.test)),
|
|
|
|
|
b.stmt(b.call(b.id('$$branch'), b.literal(0), b.id(consequent_id))),
|
|
|
|
|
b.stmt(
|
|
|
|
|
b.call(
|
|
|
|
|
b.id('$$render'),
|
|
|
|
|
b.id(consequent_id),
|
|
|
|
|
node.alternate ? b.literal(true) : undefined
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
alternate_id
|
|
|
|
|
? b.stmt(b.call(b.id('$$branch'), b.literal(1), b.id(alternate_id)))
|
|
|
|
|
? b.stmt(
|
|
|
|
|
b.call(
|
|
|
|
|
b.id('$$render'),
|
|
|
|
|
b.id(alternate_id),
|
|
|
|
|
node.alternate ? b.literal(false) : undefined
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
: undefined
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
@ -65,5 +78,7 @@ export function IfBlock(node, context) {
|
|
|
|
|
args.push(b.literal(true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.state.init.push(b.stmt(b.call('$.if', ...args)));
|
|
|
|
|
statements.push(b.stmt(b.call('$.if', ...args)));
|
|
|
|
|
|
|
|
|
|
context.state.init.push(b.block(statements));
|
|
|
|
|
}
|
|
|
|
|
|