Home / File/ IfBlock.js — svelte Source File

IfBlock.js — svelte Source File

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

File javascript Compiler Transformer 4 imports 1 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  8adf4cb0_b818_cd31_913b_0cd69a5da391["IfBlock.js"]
  d04d7971_88df_542d_dd4f_26170ce6f581["utils.js"]
  8adf4cb0_b818_cd31_913b_0cd69a5da391 --> d04d7971_88df_542d_dd4f_26170ce6f581
  66243dec_e2be_a66d_3992_3ea42cf964b5["build_expression"]
  8adf4cb0_b818_cd31_913b_0cd69a5da391 --> 66243dec_e2be_a66d_3992_3ea42cf964b5
  575bfb79_8777_5076_eda3_be015882482a["add_svelte_meta"]
  8adf4cb0_b818_cd31_913b_0cd69a5da391 --> 575bfb79_8777_5076_eda3_be015882482a
  95c28355_f14c_c3cd_5a03_d5a53ca255bc["builders"]
  8adf4cb0_b818_cd31_913b_0cd69a5da391 --> 95c28355_f14c_c3cd_5a03_d5a53ca255bc
  7665e008_f37d_b860_a594_f2539a66af4e["transform-client.js"]
  7665e008_f37d_b860_a594_f2539a66af4e --> 8adf4cb0_b818_cd31_913b_0cd69a5da391
  style 8adf4cb0_b818_cd31_913b_0cd69a5da391 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { BlockStatement, Expression, IfStatement, Statement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '#compiler/builders';
import { build_expression, add_svelte_meta } from './shared/utils.js';

/**
 * @param {AST.IfBlock} node
 * @param {ComponentContext} context
 */
export function IfBlock(node, context) {
	context.state.template.push_comment();

	/** @type {Statement[]} */
	const statements = [];

	const has_await = node.metadata.expression.has_await;
	const has_blockers = node.metadata.expression.has_blockers();
	const expression = build_expression(context, node.test, node.metadata.expression);

	// Build the if/else-if/else chain
	let index = 0;
	/** @type {IfStatement | undefined} */
	let first_if;
	/** @type {IfStatement | undefined} */
	let last_if;
	/** @type {AST.IfBlock | undefined} */
	let last_alt;

	for (const branch of [node, ...(node.metadata.flattened ?? [])]) {
		const consequent = /** @type {BlockStatement} */ (context.visit(branch.consequent));
		const consequent_id = b.id(context.state.scope.generate('consequent'));
		statements.push(b.var(consequent_id, b.arrow([b.id('$$anchor')], consequent)));

		// Build the test expression for this branch
		/** @type {Expression} */
		let test;

		if (branch.metadata.expression.has_await) {
			// Top-level condition with await: already resolved by $.async wrapper
			test = b.call('$.get', b.id('$$condition'));
		} else {
			const expression = build_expression(context, branch.test, branch.metadata.expression);

			if (branch.metadata.expression.has_call) {
				const derived_id = b.id(context.state.scope.generate('d'));
				statements.push(b.var(derived_id, b.call('$.derived', b.arrow([], expression))));
				test = b.call('$.get', derived_id);
			} else {
				test = expression;
			}
		}

		const render_call = b.stmt(b.call('$$render', consequent_id, index > 0 && b.literal(index)));
		const new_if = b.if(test, render_call);

		if (last_if) {
			last_if.alternate = new_if;
		} else {
			first_if = new_if;
// ... (72 more lines)

Domain

Subdomains

Functions

Frequently Asked Questions

What does IfBlock.js do?
IfBlock.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 IfBlock.js?
IfBlock.js defines 1 function(s): IfBlock.
What does IfBlock.js depend on?
IfBlock.js imports 4 module(s): add_svelte_meta, build_expression, builders, utils.js.
What files import IfBlock.js?
IfBlock.js is imported by 1 file(s): transform-client.js.
Where is IfBlock.js in the architecture?
IfBlock.js is located at packages/svelte/src/compiler/phases/3-transform/client/visitors/IfBlock.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