Home / File/ SlotElement.js — svelte Source File

SlotElement.js — svelte Source File

Architecture documentation for SlotElement.js, a javascript file in the svelte codebase. 5 imports, 1 dependents.

File javascript Compiler Transformer 5 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  06302d5e_b0f5_f57d_20cf_13efce492ae0["SlotElement.js"]
  7880f852_1b7b_9ea3_4e22_732f4a684bbe["element.js"]
  06302d5e_b0f5_f57d_20cf_13efce492ae0 --> 7880f852_1b7b_9ea3_4e22_732f4a684bbe
  e31bc2c2_91c1_b9a9_f78a_2832bddce6c5["build_attribute_value"]
  06302d5e_b0f5_f57d_20cf_13efce492ae0 --> e31bc2c2_91c1_b9a9_f78a_2832bddce6c5
  d04d7971_88df_542d_dd4f_26170ce6f581["utils.js"]
  06302d5e_b0f5_f57d_20cf_13efce492ae0 --> d04d7971_88df_542d_dd4f_26170ce6f581
  be60a9d7_77eb_5ad3_ab7e_2f9bd93a3db1["Memoizer"]
  06302d5e_b0f5_f57d_20cf_13efce492ae0 --> be60a9d7_77eb_5ad3_ab7e_2f9bd93a3db1
  95c28355_f14c_c3cd_5a03_d5a53ca255bc["builders"]
  06302d5e_b0f5_f57d_20cf_13efce492ae0 --> 95c28355_f14c_c3cd_5a03_d5a53ca255bc
  7665e008_f37d_b860_a594_f2539a66af4e["transform-client.js"]
  7665e008_f37d_b860_a594_f2539a66af4e --> 06302d5e_b0f5_f57d_20cf_13efce492ae0
  style 06302d5e_b0f5_f57d_20cf_13efce492ae0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { BlockStatement, Expression, ExpressionStatement, Literal, Property, Statement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '#compiler/builders';
import { build_attribute_value } from './shared/element.js';
import { Memoizer } from './shared/utils.js';

/**
 * @param {AST.SlotElement} node
 * @param {ComponentContext} context
 */
export function SlotElement(node, context) {
	// <slot {a}>fallback</slot>  -->   $.slot($$slots.default, { get a() { .. } }, () => ...fallback);
	context.state.template.push_comment();

	/** @type {Property[]} */
	const props = [];

	/** @type {Expression[]} */
	const spreads = [];

	/** @type {ExpressionStatement[]} */
	const lets = [];

	const memoizer = new Memoizer();

	let name = b.literal('default');

	for (const attribute of node.attributes) {
		if (attribute.type === 'SpreadAttribute') {
			spreads.push(b.thunk(/** @type {Expression} */ (context.visit(attribute))));
		} else if (attribute.type === 'Attribute') {
			const { value, has_state } = build_attribute_value(
				attribute.value,
				context,
				(value, metadata) =>
					metadata.has_call || metadata.has_await
						? b.call('$.get', memoizer.add(value, metadata))
						: value
			);

			if (attribute.name === 'name') {
				name = /** @type {Literal} */ (value);
			} else if (attribute.name !== 'slot') {
				if (has_state) {
					props.push(b.get(attribute.name, [b.return(value)]));
				} else {
					props.push(b.init(attribute.name, value));
				}
			}
		} else if (attribute.type === 'LetDirective') {
			context.visit(attribute, { ...context.state, let_directives: lets });
		}
	}

	memoizer.apply();

	// Let bindings first, they can be used on attributes
	context.state.init.push(...lets);

	/** @type {Statement[]} */
	const statements = memoizer.deriveds(context.state.analysis.runes);

	const props_expression =
		spreads.length === 0 ? b.object(props) : b.call('$.spread_props', b.object(props), ...spreads);

	const fallback =
		node.fragment.nodes.length === 0
			? b.null
			: b.arrow([b.id('$$anchor')], /** @type {BlockStatement} */ (context.visit(node.fragment)));

	statements.push(
		b.stmt(b.call('$.slot', context.state.node, b.id('$$props'), name, props_expression, fallback))
	);

	const async_values = memoizer.async_values();
	const blockers = memoizer.blockers();

	if (async_values || blockers) {
		context.state.init.push(
			b.stmt(
				b.call(
					'$.async',
					context.state.node,
					blockers,
					async_values,
					b.arrow([context.state.node, ...memoizer.async_ids()], b.block(statements))
				)
			)
		);
	} else {
		context.state.init.push(statements.length === 1 ? statements[0] : b.block(statements));
	}
}

Domain

Subdomains

Functions

Frequently Asked Questions

What does SlotElement.js do?
SlotElement.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Transformer subdomain.
What functions are defined in SlotElement.js?
SlotElement.js defines 1 function(s): SlotElement.
What does SlotElement.js depend on?
SlotElement.js imports 5 module(s): Memoizer, build_attribute_value, builders, element.js, utils.js.
What files import SlotElement.js?
SlotElement.js is imported by 1 file(s): transform-client.js.
Where is SlotElement.js in the architecture?
SlotElement.js is located at packages/svelte/src/compiler/phases/3-transform/client/visitors/SlotElement.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/3-transform/client/visitors).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free