Home / Function/ transform_body() — svelte Function Reference

transform_body() — svelte Function Reference

Architecture documentation for the transform_body() function in transform-async.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  6edb6f5e_a923_c9d9_63cc_ebec53916480["transform_body()"]
  4a72fe76_77b1_749a_ab61_f53e30c4916a["transform-async.js"]
  6edb6f5e_a923_c9d9_63cc_ebec53916480 -->|defined in| 4a72fe76_77b1_749a_ab61_f53e30c4916a
  fa0e8946_29c3_f58a_ab56_307427332bf0["Program()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| 6edb6f5e_a923_c9d9_63cc_ebec53916480
  31bd644b_0a23_d66d_08af_327a56c7a9c4["Program()"]
  31bd644b_0a23_d66d_08af_327a56c7a9c4 -->|calls| 6edb6f5e_a923_c9d9_63cc_ebec53916480
  style 6edb6f5e_a923_c9d9_63cc_ebec53916480 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js lines 32–114

export function transform_body(instance_body, runner, transform) {
	// Any sync statements before the first await expression
	const statements = instance_body.sync.map(
		(node) => /** @type {ESTree.Statement | ESTree.VariableDeclaration} */ (transform(node))
	);

	// Declarations for the await expressions (they will assign to them; need to be hoisted to be available in whole instance scope)
	if (instance_body.declarations.length > 0) {
		statements.push(
			b.declaration(
				'var',
				instance_body.declarations.map((id) => b.declarator(id))
			)
		);
	}

	// Thunks for the await expressions
	if (instance_body.async.length > 0) {
		const thunks = instance_body.async.map((s) => {
			if (s.node.type === 'VariableDeclarator') {
				const visited = /** @type {ESTree.VariableDeclaration | ESTree.EmptyStatement} */ (
					transform(b.var(s.node.id, s.node.init))
				);

				const statements =
					visited.type === 'VariableDeclaration'
						? visited.declarations.map((node) => {
								if (
									node.id.type === 'Identifier' &&
									(node.id.name.startsWith('$$d') || node.id.name.startsWith('$$array'))
								) {
									// this is an intermediate declaration created in VariableDeclaration.js;
									// subsequent statements depend on it
									return b.var(node.id, node.init);
								}

								return b.stmt(b.assignment('=', node.id, node.init ?? b.void0));
							})
						: [];

				if (statements.length === 1) {
					const statement = /** @type {ESTree.ExpressionStatement} */ (statements[0]);
					return b.thunk(statement.expression, s.has_await);
				}

				return b.thunk(b.block(statements), s.has_await);
			}

			if (s.node.type === 'ClassDeclaration') {
				return b.thunk(
					b.assignment(
						'=',
						s.node.id,
						/** @type {ESTree.ClassExpression} */ ({ ...s.node, type: 'ClassExpression' })
					),
					s.has_await
				);
			}

			if (s.node.type === 'ExpressionStatement') {
				// the expression may be a $inspect call, which will be transformed into an empty statement
				const expression = /** @type {ESTree.Expression | ESTree.EmptyStatement} */ (
					transform(s.node.expression)
				);

				if (expression.type === 'EmptyStatement') {
					return null;
				}

				return expression.type === 'AwaitExpression'
					? b.thunk(expression, true)
					: b.thunk(b.unary('void', expression), s.has_await);
			}

			return b.thunk(b.block([/** @type {ESTree.Statement} */ (transform(s.node))]), s.has_await);
		});

		// TODO get the `$$promises` ID from scope
		statements.push(b.var('$$promises', b.call(runner, b.array(thunks))));
	}

Domain

Subdomains

Called By

Frequently Asked Questions

What does transform_body() do?
transform_body() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js.
Where is transform_body() defined?
transform_body() is defined in packages/svelte/src/compiler/phases/3-transform/shared/transform-async.js at line 32.
What calls transform_body()?
transform_body() is called by 2 function(s): Program, Program.

Analyze Your Own Codebase

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

Try Supermodel Free