All files / src/compiler/phases/3-transform/client/visitors AwaitBlock.js

100% Statements 62/62
100% Branches 13/13
100% Functions 1/1
100% Lines 60/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 612x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 87x 87x 87x 87x 87x 87x 87x 87x 87x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 87x 87x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x  
/** @import { BlockStatement, Expression, Pattern, Statement } from 'estree' */
/** @import { AwaitBlock } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '../../../../utils/builders.js';
import { create_derived_block_argument } from '../utils.js';
 
/**
 * @param {AwaitBlock} node
 * @param {ComponentContext} context
 */
export function AwaitBlock(node, context) {
	context.state.template.push('<!>');
 
	// Visit {#await <expression>} first to ensure that scopes are in the correct order
	const expression = b.thunk(/** @type {Expression} */ (context.visit(node.expression)));
 
	let then_block;
	let catch_block;
 
	if (node.then) {
		const argument = node.value && create_derived_block_argument(node.value, context);
 
		/** @type {Pattern[]} */
		const args = [b.id('$$anchor')];
		if (argument) args.push(argument.id);
 
		const declarations = argument?.declarations ?? [];
		const block = /** @type {BlockStatement} */ (context.visit(node.then));
 
		then_block = b.arrow(args, b.block([...declarations, ...block.body]));
	}
 
	if (node.catch) {
		const argument = node.error && create_derived_block_argument(node.error, context);
 
		/** @type {Pattern[]} */
		const args = [b.id('$$anchor')];
		if (argument) args.push(argument.id);
 
		const declarations = argument?.declarations ?? [];
		const block = /** @type {BlockStatement} */ (context.visit(node.catch));
 
		catch_block = b.arrow(args, b.block([...declarations, ...block.body]));
	}
 
	context.state.init.push(
		b.stmt(
			b.call(
				'$.await',
				context.state.node,
				expression,
				node.pending
					? b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.pending)))
					: b.literal(null),
				then_block,
				catch_block
			)
		)
	);
}