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

100% Statements 77/77
100% Branches 8/8
100% Functions 1/1
100% Lines 72/72

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 61 62 63 64 65 66 67 68 69 70 71 72 732x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 75x 75x 32x 32x 32x 32x 75x 75x 11x 9x 9x 11x 11x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 11x 11x 75x  
/** @import { Location } from 'locate-character' */
/** @import { BlockStatement, Expression } from 'estree' */
/** @import { SvelteElement } from '#compiler' */
/** @import { ComponentContext } from '../types.js' */
import { dev, locator } from '../../../../state.js';
import * as b from '../../../../utils/builders.js';
import { determine_namespace_for_children } from '../../utils.js';
import { build_element_attributes } from './shared/element.js';
import { build_template } from './shared/utils.js';
 
/**
 * @param {SvelteElement} node
 * @param {ComponentContext} context
 */
export function SvelteElement(node, context) {
	let tag = /** @type {Expression} */ (context.visit(node.tag));
	if (tag.type !== 'Identifier') {
		const tag_id = context.state.scope.generate('$$tag');
		context.state.init.push(b.const(tag_id, tag));
		tag = b.id(tag_id);
	}
 
	if (dev) {
		if (node.fragment.nodes.length > 0) {
			context.state.init.push(b.stmt(b.call('$.validate_void_dynamic_element', b.thunk(tag))));
		}
		context.state.init.push(b.stmt(b.call('$.validate_dynamic_element_tag', b.thunk(tag))));
	}
 
	const state = {
		...context.state,
		namespace: determine_namespace_for_children(node, context.state.namespace),
		template: [],
		init: []
	};
 
	build_element_attributes(node, { ...context, state });
 
	if (dev) {
		const location = /** @type {Location} */ (locator(node.start));
		context.state.template.push(
			b.stmt(
				b.call(
					'$.push_element',
					b.id('$$payload'),
					tag,
					b.literal(location.line),
					b.literal(location.column)
				)
			)
		);
	}
 
	const attributes = b.block([...state.init, ...build_template(state.template)]);
	const children = /** @type {BlockStatement} */ (context.visit(node.fragment, state));
 
	context.state.template.push(
		b.stmt(
			b.call(
				'$.element',
				b.id('$$payload'),
				tag,
				attributes.body.length > 0 && b.thunk(attributes),
				children.body.length > 0 && b.thunk(children)
			)
		)
	);
 
	if (dev) {
		context.state.template.push(b.stmt(b.call('$.pop_element')));
	}
}