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

100% Statements 265/265
100% Branches 79/79
100% Functions 2/2
100% Lines 262/262

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 2632x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 1089x 1089x 1089x 1089x 1089x 539x 539x 1089x 704x 788x 72x 68x 68x 788x 26x 716x 507x 47x 47x 47x 47x 460x 507x 2x 2x 460x 460x 460x 690x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 788x 704x 704x 704x 704x 704x 704x 873x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 867x 867x 873x 244x 244x 244x 244x 244x 244x 244x 85x 85x 85x 243x 3x 3x 3x 3x 244x 867x 873x 873x 873x 704x 704x 704x 704x 704x 704x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 97x 317x 317x 317x 317x 317x 253x 253x 253x 253x 317x 86x 86x 106x 81x 81x 25x 102x 6x 6x 6x 19x 102x 2x 2x 2x 17x 17x 86x 86x 86x 86x 86x 253x 253x 253x 317x 167x 167x 112x 167x 110x 110x 110x 110x 110x 167x 57x 57x 57x 57x 57x 57x 317x 86x 86x 317x 704x 704x 222x 222x 704x 704x 704x 544x 704x 704x 26x 26x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 704x 6x 6x 704x 704x 704x 704x 704x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 704x 680x 47x 47x 680x 680x 680x 680x 331x 331x 680x 704x  
/** @import { BlockStatement, Expression, Pattern, Property, Statement } from 'estree' */
/** @import { Attribute, Component, LetDirective, SvelteComponent, SvelteSelf, TemplateNode, Text } from '#compiler' */
/** @import { ComponentContext } from '../../types.js' */
import { empty_comment, build_attribute_value } from './utils.js';
import * as b from '../../../../../utils/builders.js';
import { is_element_node } from '../../../../nodes.js';
 
/**
 * @param {Component | SvelteComponent | SvelteSelf} node
 * @param {Expression} expression
 * @param {ComponentContext} context
 */
export function build_inline_component(node, expression, context) {
	/** @type {Array<Property[] | Expression>} */
	const props_and_spreads = [];
 
	/** @type {Property[]} */
	const custom_css_props = [];
 
	/** @type {Record<string, LetDirective[]>} */
	const lets = { default: [] };
 
	/** @type {Record<string, TemplateNode[]>} */
	const children = {};
 
	/**
	 * If this component has a slot property, it is a named slot within another component. In this case
	 * the slot scope applies to the component itself, too, and not just its children.
	 */
	const slot_scope_applies_to_itself = node.attributes.some(
		(node) => node.type === 'Attribute' && node.name === 'slot'
	);
 
	/**
	 * Components may have a children prop and also have child nodes. In this case, we assume
	 * that the child component isn't using render tags yet and pass the slot as $$slots.default.
	 * We're not doing it for spread attributes, as this would result in too many false positives.
	 */
	let has_children_prop = false;
 
	/**
	 * @param {Property} prop
	 */
	function push_prop(prop) {
		const current = props_and_spreads.at(-1);
		const current_is_props = Array.isArray(current);
		const props = current_is_props ? current : [];
		props.push(prop);
		if (!current_is_props) {
			props_and_spreads.push(props);
		}
	}
	for (const attribute of node.attributes) {
		if (attribute.type === 'LetDirective') {
			if (!slot_scope_applies_to_itself) {
				lets.default.push(attribute);
			}
		} else if (attribute.type === 'SpreadAttribute') {
			props_and_spreads.push(/** @type {Expression} */ (context.visit(attribute)));
		} else if (attribute.type === 'Attribute') {
			if (attribute.name.startsWith('--')) {
				const value = build_attribute_value(attribute.value, context, false, true);
				custom_css_props.push(b.init(attribute.name, value));
				continue;
			}
 
			if (attribute.name === 'children') {
				has_children_prop = true;
			}
 
			const value = build_attribute_value(attribute.value, context, false, true);
			push_prop(b.prop('init', b.key(attribute.name), value));
		} else if (attribute.type === 'BindDirective' && attribute.name !== 'this') {
			// TODO this needs to turn the whole thing into a while loop because the binding could be mutated eagerly in the child
			push_prop(
				b.get(attribute.name, [
					b.return(/** @type {Expression} */ (context.visit(attribute.expression)))
				])
			);
			push_prop(
				b.set(attribute.name, [
					b.stmt(
						/** @type {Expression} */ (
							context.visit(b.assignment('=', attribute.expression, b.id('$$value')))
						)
					),
					b.stmt(b.assignment('=', b.id('$$settled'), b.false))
				])
			);
		}
	}
 
	/** @type {Statement[]} */
	const snippet_declarations = [];
 
	// Group children by slot
	for (const child of node.fragment.nodes) {
		if (child.type === 'SnippetBlock') {
			// the SnippetBlock visitor adds a declaration to `init`, but if it's directly
			// inside a component then we want to hoist them into a block so that they
			// can be used as props without creating conflicts
			context.visit(child, {
				...context.state,
				init: snippet_declarations
			});
 
			push_prop(b.prop('init', child.expression, child.expression));
 
			continue;
		}
 
		let slot_name = 'default';
		if (is_element_node(child)) {
			const slot = /** @type {Attribute | undefined} */ (
				child.attributes.find(
					(attribute) => attribute.type === 'Attribute' && attribute.name === 'slot'
				)
			);
 
			if (slot !== undefined) {
				slot_name = /** @type {Text[]} */ (slot.value)[0].data;
 
				lets[slot_name] = child.attributes.filter((attribute) => attribute.type === 'LetDirective');
			} else if (child.type === 'SvelteFragment') {
				lets.default.push(
					...child.attributes.filter((attribute) => attribute.type === 'LetDirective')
				);
			}
		}
 
		children[slot_name] = children[slot_name] || [];
		children[slot_name].push(child);
	}
 
	// Serialize each slot
	/** @type {Property[]} */
	const serialized_slots = [];
 
	for (const slot_name of Object.keys(children)) {
		const block = /** @type {BlockStatement} */ (
			context.visit(
				{
					...node.fragment,
					// @ts-expect-error
					nodes: children[slot_name]
				},
				{
					...context.state,
					scope:
						context.state.scopes.get(slot_name === 'default' ? children[slot_name][0] : node) ??
						context.state.scope
				}
			)
		);
 
		if (block.body.length === 0) continue;
 
		/** @type {Pattern[]} */
		const params = [b.id('$$payload')];
 
		if (lets[slot_name].length > 0) {
			const pattern = b.object_pattern(
				lets[slot_name].map((node) => {
					if (node.expression === null) {
						return b.init(node.name, b.id(node.name));
					}
 
					if (node.expression.type === 'ObjectExpression') {
						// @ts-expect-error it gets parsed as an `ObjectExpression` but is really an `ObjectPattern`
						return b.init(node.name, b.object_pattern(node.expression.properties));
					}
 
					if (node.expression.type === 'ArrayExpression') {
						// @ts-expect-error it gets parsed as an `ArrayExpression` but is really an `ArrayPattern`
						return b.init(node.name, b.array_pattern(node.expression.elements));
					}
 
					return b.init(node.name, node.expression);
				})
			);
 
			params.push(pattern);
		}
 
		const slot_fn = b.arrow(params, b.block(block.body));
 
		if (slot_name === 'default' && !has_children_prop) {
			if (
				lets.default.length === 0 &&
				children.default.every((node) => node.type !== 'SvelteFragment')
			) {
				// create `children` prop...
				push_prop(b.prop('init', b.id('children'), slot_fn));
 
				// and `$$slots.default: true` so that `<slot>` on the child works
				serialized_slots.push(b.init(slot_name, b.true));
			} else {
				// create `$$slots.default`...
				serialized_slots.push(b.init(slot_name, slot_fn));
 
				// and a `children` prop that errors
				push_prop(b.init('children', b.id('$.invalid_default_snippet')));
			}
		} else {
			serialized_slots.push(b.init(slot_name, slot_fn));
		}
	}
 
	if (serialized_slots.length > 0) {
		push_prop(b.prop('init', b.id('$$slots'), b.object(serialized_slots)));
	}
 
	const props_expression =
		props_and_spreads.length === 0 ||
		(props_and_spreads.length === 1 && Array.isArray(props_and_spreads[0]))
			? b.object(/** @type {Property[]} */ (props_and_spreads[0] || []))
			: b.call(
					'$.spread_props',
					b.array(props_and_spreads.map((p) => (Array.isArray(p) ? b.object(p) : p)))
				);
 
	/** @type {Statement} */
	let statement = b.stmt(
		(node.type === 'SvelteComponent' ? b.maybe_call : b.call)(
			expression,
			b.id('$$payload'),
			props_expression
		)
	);
 
	if (snippet_declarations.length > 0) {
		statement = b.block([...snippet_declarations, statement]);
	}
 
	const dynamic =
		node.type === 'SvelteComponent' || (node.type === 'Component' && node.metadata.dynamic);
 
	if (custom_css_props.length > 0) {
		context.state.template.push(
			b.stmt(
				b.call(
					'$.css_props',
					b.id('$$payload'),
					b.literal(context.state.namespace === 'svg' ? false : true),
					b.object(custom_css_props),
					b.thunk(b.block([statement])),
					dynamic && b.true
				)
			)
		);
	} else {
		if (dynamic) {
			context.state.template.push(empty_comment);
		}
 
		context.state.template.push(statement);
 
		if (!context.state.skip_hydration_boundaries) {
			context.state.template.push(empty_comment);
		}
	}
}