Home / File/ EachBlock.js — svelte Source File

EachBlock.js — svelte Source File

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

File javascript Compiler Transformer 9 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  7cf83ecb_26f4_e326_a28c_a905bbed7141["EachBlock.js"]
  495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> 495501a4_a342_6a4d_ac11_e3e2fee8b218
  a146f6ac_0088_8736_b6ce_318f9f115170["e"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> a146f6ac_0088_8736_b6ce_318f9f115170
  0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c["ast.js"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> 0c5c28a7_226d_4e7c_e75e_0853c0a9fc2c
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> c12e0147_3f27_cf17_5878_e54ffdc328d5
  c4b4ac8d_9914_5ede_1aea_723bf80d2e9b["fragment.js"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> c4b4ac8d_9914_5ede_1aea_723bf80d2e9b
  313d2a82_30ea_3161_3aad_0cc2094979aa["mark_subtree_dynamic"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> 313d2a82_30ea_3161_3aad_0cc2094979aa
  bf0d8f1b_17da_970d_bf44_fbcf099d5371["utils.js"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> bf0d8f1b_17da_970d_bf44_fbcf099d5371
  ea08cb05_2664_4e93_7551_6103e0cb3a87["validate_block_not_empty"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> ea08cb05_2664_4e93_7551_6103e0cb3a87
  7148e639_69d8_a03d_3f08_bd23f41e718a["validate_opening_tag"]
  7cf83ecb_26f4_e326_a28c_a905bbed7141 --> 7148e639_69d8_a03d_3f08_bd23f41e718a
  4aa8a188_84d4_0274_ed83_cac0ab1d3572["index.js"]
  4aa8a188_84d4_0274_ed83_cac0ab1d3572 --> 7cf83ecb_26f4_e326_a28c_a905bbed7141
  style 7cf83ecb_26f4_e326_a28c_a905bbed7141 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { Expression } from 'estree' */
/** @import { AST, Binding } from '#compiler' */
/** @import { Context } from '../types' */
/** @import { Scope } from '../../scope' */
import * as e from '../../../errors.js';
import { extract_identifiers } from '../../../utils/ast.js';
import { mark_subtree_dynamic } from './shared/fragment.js';
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';

/**
 * @param {AST.EachBlock} node
 * @param {Context} context
 */
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);
}

/**
 * @param {Binding} binding
 * @param {Set<Binding>} bindings
 * @returns {void}
 */
function collect_transitive_dependencies(binding, bindings) {
	if (bindings.has(binding)) {
		return;
	}
	bindings.add(binding);

	if (binding.kind === 'legacy_reactive') {
		for (const dep of binding.legacy_dependencies) {
			collect_transitive_dependencies(dep, bindings);
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does EachBlock.js do?
EachBlock.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 EachBlock.js?
EachBlock.js defines 2 function(s): EachBlock, collect_transitive_dependencies.
What does EachBlock.js depend on?
EachBlock.js imports 9 module(s): ast.js, e, errors.js, extract_identifiers, fragment.js, mark_subtree_dynamic, utils.js, validate_block_not_empty, and 1 more.
What files import EachBlock.js?
EachBlock.js is imported by 1 file(s): index.js.
Where is EachBlock.js in the architecture?
EachBlock.js is located at packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/2-analyze/visitors).

Analyze Your Own Codebase

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

Try Supermodel Free