Home / Function/ IfBlock() — svelte Function Reference

IfBlock() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  a9271596_16f4_fc39_63e6_7fcfca105e9c["IfBlock()"]
  349b9b06_99f6_79ed_a469_c74ec514305c["IfBlock.js"]
  a9271596_16f4_fc39_63e6_7fcfca105e9c -->|defined in| 349b9b06_99f6_79ed_a469_c74ec514305c
  ea08cb05_2664_4e93_7551_6103e0cb3a87["validate_block_not_empty()"]
  a9271596_16f4_fc39_63e6_7fcfca105e9c -->|calls| ea08cb05_2664_4e93_7551_6103e0cb3a87
  7148e639_69d8_a03d_3f08_bd23f41e718a["validate_opening_tag()"]
  a9271596_16f4_fc39_63e6_7fcfca105e9c -->|calls| 7148e639_69d8_a03d_3f08_bd23f41e718a
  313d2a82_30ea_3161_3aad_0cc2094979aa["mark_subtree_dynamic()"]
  a9271596_16f4_fc39_63e6_7fcfca105e9c -->|calls| 313d2a82_30ea_3161_3aad_0cc2094979aa
  style a9271596_16f4_fc39_63e6_7fcfca105e9c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/visitors/IfBlock.js lines 10–46

export function IfBlock(node, context) {
	validate_block_not_empty(node.consequent, context);
	validate_block_not_empty(node.alternate, context);

	if (context.state.analysis.runes) {
		validate_opening_tag(node, context.state, node.elseif ? ':' : '#');
	}

	mark_subtree_dynamic(context.path);

	context.visit(node.test, {
		...context.state,
		expression: node.metadata.expression
	});

	context.visit(node.consequent);
	if (node.alternate) context.visit(node.alternate);

	// Check if we can flatten branches
	const alt = node.alternate;

	if (alt && alt.nodes.length === 1 && alt.nodes[0].type === 'IfBlock' && alt.nodes[0].elseif) {
		const elseif = alt.nodes[0];

		// Don't flatten if this else-if has an await expression or new blockers
		// TODO would be nice to check the await expression itself to see if it's awaiting the same thing as a previous if expression
		if (
			!elseif.metadata.expression.has_await &&
			!elseif.metadata.expression.has_more_blockers_than(node.metadata.expression)
		) {
			// Roll the existing flattened branches (if any) into this one, then delete those of the else-if block
			// to avoid processing them multiple times as we walk down the chain during code transformation.
			node.metadata.flattened = [elseif, ...(elseif.metadata.flattened ?? [])];
			elseif.metadata.flattened = undefined;
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does IfBlock() do?
IfBlock() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/visitors/IfBlock.js.
Where is IfBlock() defined?
IfBlock() is defined in packages/svelte/src/compiler/phases/2-analyze/visitors/IfBlock.js at line 10.
What does IfBlock() call?
IfBlock() calls 3 function(s): mark_subtree_dynamic, validate_block_not_empty, validate_opening_tag.

Analyze Your Own Codebase

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

Try Supermodel Free