Home / Function/ EachBlock() — svelte Function Reference

EachBlock() — svelte Function Reference

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

Function javascript Compiler Transformer calls 7 called by 1

Entity Profile

Dependency Diagram

graph TD
  411cfb7b_abaa_704a_4915_960543b87198["EachBlock()"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141["EachBlock.js"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|defined in| 7cf83ecb_26f4_e326_a28c_a905bbed7141
  78a6ba9a_5003_f569_a638_76e4f1977809["analyze_component()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 411cfb7b_abaa_704a_4915_960543b87198
  7148e639_69d8_a03d_3f08_bd23f41e718a["validate_opening_tag()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| 7148e639_69d8_a03d_3f08_bd23f41e718a
  ea08cb05_2664_4e93_7551_6103e0cb3a87["validate_block_not_empty()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| ea08cb05_2664_4e93_7551_6103e0cb3a87
  a2b51285_e33f_02f6_875e_244185db2012["state_invalid_placement()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| a2b51285_e33f_02f6_875e_244185db2012
  2d3a4220_4d1f_42de_da7f_880a8cefc8bf["each_key_without_as()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| 2d3a4220_4d1f_42de_da7f_880a8cefc8bf
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| c12e0147_3f27_cf17_5878_e54ffdc328d5
  88067bb9_3321_4aa8_e647_8c07fb224b01["collect_transitive_dependencies()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| 88067bb9_3321_4aa8_e647_8c07fb224b01
  313d2a82_30ea_3161_3aad_0cc2094979aa["mark_subtree_dynamic()"]
  411cfb7b_abaa_704a_4915_960543b87198 -->|calls| 313d2a82_30ea_3161_3aad_0cc2094979aa
  style 411cfb7b_abaa_704a_4915_960543b87198 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js lines 14–79

export function EachBlock(node, context) {
	validate_opening_tag(node, context.state, '#');

	validate_block_not_empty(node.body, context);
	validate_block_not_empty(node.fallback, context);

	const id = node.context;
	if (id?.type === 'Identifier' && (id.name === '$state' || id.name === '$derived')) {
		// TODO weird that this is necessary
		e.state_invalid_placement(node, id.name);
	}

	if (node.key) {
		// treat `{#each items as item, i (i)}` as a normal indexed block, everything else as keyed
		node.metadata.keyed =
			node.key.type !== 'Identifier' || !node.index || node.key.name !== node.index;
	}

	if (node.metadata.keyed && !node.context) {
		e.each_key_without_as(/** @type {Expression} */ (node.key));
	}

	// evaluate expression in parent scope
	context.visit(node.expression, {
		...context.state,
		expression: node.metadata.expression,
		scope: /** @type {Scope} */ (context.state.scope.parent)
	});

	context.visit(node.body);
	if (node.key) context.visit(node.key);
	if (node.fallback) context.visit(node.fallback);

	if (!context.state.analysis.runes) {
		let mutated =
			!!node.context &&
			extract_identifiers(node.context).some((id) => {
				const binding = context.state.scope.get(id.name);
				return !!binding?.mutated;
			});

		// collect transitive dependencies...
		for (const binding of node.metadata.expression.dependencies) {
			if (binding.declaration_kind !== 'function') {
				collect_transitive_dependencies(binding, node.metadata.transitive_deps);
			}
		}

		// ...and ensure they are marked as state, so they can be turned
		// into mutable sources and invalidated
		if (mutated) {
			for (const binding of node.metadata.transitive_deps) {
				if (
					binding.kind === 'normal' &&
					(binding.declaration_kind === 'const' ||
						binding.declaration_kind === 'let' ||
						binding.declaration_kind === 'var')
				) {
					binding.kind = 'state';
				}
			}
		}
	}

	mark_subtree_dynamic(context.path);
}

Domain

Subdomains

Frequently Asked Questions

What does EachBlock() do?
EachBlock() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js.
Where is EachBlock() defined?
EachBlock() is defined in packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js at line 14.
What does EachBlock() call?
EachBlock() calls 7 function(s): collect_transitive_dependencies, each_key_without_as, extract_identifiers, mark_subtree_dynamic, state_invalid_placement, validate_block_not_empty, validate_opening_tag.
What calls EachBlock()?
EachBlock() is called by 1 function(s): analyze_component.

Analyze Your Own Codebase

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

Try Supermodel Free