Home / Function/ Program() — svelte Function Reference

Program() — svelte Function Reference

Architecture documentation for the Program() function in Program.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  fa0e8946_29c3_f58a_ab56_307427332bf0["Program()"]
  446a5632_54bb_cf7e_dbdc_33bec913df9e["Program.js"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|defined in| 446a5632_54bb_cf7e_dbdc_33bec913df9e
  27fded45_bc11_247b_d3fe_94831379f9ed["build_getter()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| 27fded45_bc11_247b_d3fe_94831379f9ed
  5f828ae1_5f83_74a8_876a_ea54ea47b588["is_prop_source()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| 5f828ae1_5f83_74a8_876a_ea54ea47b588
  fb41e547_07c8_cc9a_e227_3f08aad6d1f5["add_state_transformers()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| fb41e547_07c8_cc9a_e227_3f08aad6d1f5
  6edb6f5e_a923_c9d9_63cc_ebec53916480["transform_body()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| 6edb6f5e_a923_c9d9_63cc_ebec53916480
  style fa0e8946_29c3_f58a_ab56_307427332bf0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js lines 12–153

export function Program(node, context) {
	if (!context.state.analysis.runes) {
		context.state.transform['$$props'] = {
			read: (node) => ({ ...node, name: '$$sanitized_props' })
		};

		for (const [name, binding] of context.state.scope.declarations) {
			if (binding.declaration_kind === 'import' && binding.mutated) {
				// the declaration itself is hoisted to the module scope, so we need
				// to resort to cruder measures to differentiate instance/module imports
				const { start, end } = context.state.analysis.instance.ast;
				const node = /** @type {ImportDeclaration} */ (binding.initial);
				const is_instance_import =
					/** @type {number} */ (node.start) > /** @type {number} */ (start) &&
					/** @type {number} */ (node.end) < /** @type {number} */ (end);

				if (is_instance_import) {
					const id = b.id('$$_import_' + name);

					context.state.transform[name] = {
						read: (_) => b.call(id),
						mutate: (_, mutation) => b.call(id, mutation)
					};

					context.state.legacy_reactive_imports.push(
						b.var(id, b.call('$.reactive_import', b.thunk(b.id(name))))
					);
				}
			}
		}
	}

	for (const [name, binding] of context.state.scope.declarations) {
		if (binding.kind === 'store_sub') {
			// read lazily, so that transforms added later are still applied
			/** @type {Expression} */
			let cached;

			const get_store = () => {
				return (cached ??= /** @type {Expression} */ (context.visit(b.id(name.slice(1)))));
			};

			context.state.transform[name] = {
				read: b.call,
				assign: (_, value) => b.call('$.store_set', get_store(), value),
				mutate: (node, mutation) => {
					// We need to untrack the store read, for consistency with Svelte 4
					const untracked = b.call('$.untrack', node);

					/**
					 *
					 * @param {Expression} n
					 * @returns {Expression}
					 */
					function replace(n) {
						if (n.type === 'MemberExpression') {
							return {
								...n,
								object: replace(/** @type {Expression} */ (n.object)),
								property: n.property
							};
						}

						return untracked;
					}

					return b.call(
						'$.store_mutate',
						get_store(),
						mutation.type === 'AssignmentExpression'
							? b.assignment(
									mutation.operator,
									/** @type {MemberExpression} */ (
										replace(/** @type {MemberExpression} */ (mutation.left))
									),
									mutation.right
								)
							: b.update(mutation.operator, replace(mutation.argument), mutation.prefix),
						untracked
					);
				},

Domain

Subdomains

Frequently Asked Questions

What does Program() do?
Program() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js.
Where is Program() defined?
Program() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/Program.js at line 12.
What does Program() call?
Program() calls 4 function(s): add_state_transformers, build_getter, is_prop_source, transform_body.

Analyze Your Own Codebase

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

Try Supermodel Free