All files / src/compiler/phases/3-transform/server/visitors LetDirective.js

100% Statements 41/41
100% Branches 11/11
100% Functions 1/1
100% Lines 40/40

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 412x 2x 2x 2x 2x 2x 2x 2x 2x 103x 95x 95x 95x 8x 8x 8x 8x 103x 14x 14x 8x 8x 8x 8x 8x 8x 8x 8x 103x 6x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x  
/** @import { LetDirective } from '#compiler' */
/** @import { ComponentContext } from '../types.js' */
import * as b from '../../../../utils/builders.js';
 
/**
 * @param {LetDirective} node
 * @param {ComponentContext} context
 */
export function LetDirective(node, context) {
	if (node.expression === null || node.expression.type === 'Identifier') {
		const name = node.expression === null ? node.name : node.expression.name;
		return b.const(name, b.member(b.id('$$slotProps'), b.id(node.name)));
	}
 
	const name = context.state.scope.generate(node.name);
	const bindings = context.state.scope.get_bindings(node);
 
	for (const binding of bindings) {
		context.state.getters[binding.node.name] = b.member(b.id(name), b.id(binding.node.name));
	}
 
	return b.const(
		name,
		b.call(
			b.thunk(
				b.block([
					b.let(
						node.expression.type === 'ObjectExpression'
							? // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
								b.object_pattern(node.expression.properties)
							: // @ts-expect-error types don't match, but it can't contain spread elements and the structure is otherwise fine
								b.array_pattern(node.expression.elements),
						b.member(b.id('$$slotProps'), b.id(node.name))
					),
					b.return(b.object(bindings.map((binding) => b.init(binding.node.name, binding.node))))
				])
			)
		)
	);
}