Home / Function/ analyze_component() — svelte Function Reference

analyze_component() — svelte Function Reference

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

Function javascript Compiler Transformer calls 39 called by 2

Entity Profile

Dependency Diagram

graph TD
  78a6ba9a_5003_f569_a638_76e4f1977809["analyze_component()"]
  4aa8a188_84d4_0274_ed83_cac0ab1d3572["index.js"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|defined in| 4aa8a188_84d4_0274_ed83_cac0ab1d3572
  10e19602_80ed_a1f9_ddcc_2ac1ab16e385["compile()"]
  10e19602_80ed_a1f9_ddcc_2ac1ab16e385 -->|calls| 78a6ba9a_5003_f569_a638_76e4f1977809
  ffed8565_a534_8183_a11f_bcffae15897e["migrate()"]
  ffed8565_a534_8183_a11f_bcffae15897e -->|calls| 78a6ba9a_5003_f569_a638_76e4f1977809
  e6258220_a976_05ad_470f_313b670ef126["js()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| e6258220_a976_05ad_470f_313b670ef126
  c531899f_2ddc_b054_bfe1_2cfdfd2b1c7f["create_scopes()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| c531899f_2ddc_b054_bfe1_2cfdfd2b1c7f
  14aee6bc_58b8_9da9_3853_23ecd1ef30fe["global_reference_invalid()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 14aee6bc_58b8_9da9_3853_23ecd1ef30fe
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  96840921_f43d_a26b_1d2e_cd28c0fd6d73["is_rune()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 96840921_f43d_a26b_1d2e_cd28c0fd6d73
  bed91719_d047_2256_e199_ee875d5f49b9["get_rune()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| bed91719_d047_2256_e199_ee875d5f49b9
  fe09b863_ce2a_faad_7b1b_3bc9e4c3c6a0["owner()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| fe09b863_ce2a_faad_7b1b_3bc9e4c3c6a0
  2b8edd40_2fdf_c267_717a_cd0338fdf2ce["store_invalid_scoped_subscription()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 2b8edd40_2fdf_c267_717a_cd0338fdf2ce
  e9c62701_98e6_ce15_470b_d43832927cb6["store_rune_conflict()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| e9c62701_98e6_ce15_470b_d43832927cb6
  4bcdf15d_d2f9_468d_1bea_b1c810052eae["store_invalid_subscription()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 4bcdf15d_d2f9_468d_1bea_b1c810052eae
  44642633_e3c9_8ae1_48af_53507481a2cb["declare()"]
  78a6ba9a_5003_f569_a638_76e4f1977809 -->|calls| 44642633_e3c9_8ae1_48af_53507481a2cb
  style 78a6ba9a_5003_f569_a638_76e4f1977809 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/2-analyze/index.js lines 330–936

export function analyze_component(root, source, options) {
	const scope_root = new ScopeRoot();

	const module = js(root.module, scope_root, false, null);
	const instance = js(root.instance, scope_root, true, module.scope);

	const { scope, scopes, has_await } = create_scopes(
		root.fragment,
		scope_root,
		false,
		instance.scope
	);

	/** @type {Template} */
	const template = { ast: root.fragment, scope, scopes };

	let synthetic_stores_legacy_check = [];

	// create synthetic bindings for store subscriptions
	for (const [name, references] of module.scope.references) {
		if (name[0] !== '$' || RESERVED.includes(name)) continue;
		if (name === '$' || name[1] === '$') {
			e.global_reference_invalid(references[0].node, name);
		}

		const store_name = name.slice(1);
		const declaration = instance.scope.get(store_name);
		const init = /** @type {ESTree.Node | undefined} */ (declaration?.initial);

		// If we're not in legacy mode through the compiler option, assume the user
		// is referencing a rune and not a global store.
		if (
			options.runes === false ||
			!is_rune(name) ||
			(declaration !== null &&
				// const state = $state(0) is valid
				(get_rune(init, instance.scope) === null ||
					// rune-line names received as props are valid too (but we have to protect against $props as store)
					(store_name !== 'props' && get_rune(init, instance.scope) === '$props')) &&
				// allow `import { derived } from 'svelte/store'` in the same file as `const x = $derived(..)` because one is not a subscription to the other
				!(
					name === '$derived' &&
					declaration.initial?.type === 'ImportDeclaration' &&
					declaration.initial.source.value === 'svelte/store'
				))
		) {
			let is_nested_store_subscription_node = undefined;
			search: for (const reference of references) {
				for (let i = reference.path.length - 1; i >= 0; i--) {
					const scope =
						scopes.get(reference.path[i]) ||
						module.scopes.get(reference.path[i]) ||
						instance.scopes.get(reference.path[i]);
					if (scope) {
						const owner = scope?.owner(store_name);
						if (!!owner && owner !== module.scope && owner !== instance.scope) {
							is_nested_store_subscription_node = reference.node;
							break search;
						}
						break;
					}
				}
			}

			if (is_nested_store_subscription_node) {
				e.store_invalid_scoped_subscription(is_nested_store_subscription_node);
			}

			if (options.runes !== false) {
				if (declaration === null && /[a-z]/.test(store_name[0])) {
					e.global_reference_invalid(references[0].node, name);
				} else if (declaration !== null && is_rune(name)) {
					for (const { node, path } of references) {
						if (path.at(-1)?.type === 'CallExpression') {
							w.store_rune_conflict(node, store_name);
						}
					}
				}
			}

			if (module.ast) {

Domain

Subdomains

Called By

Frequently Asked Questions

What does analyze_component() do?
analyze_component() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/2-analyze/index.js.
Where is analyze_component() defined?
analyze_component() is defined in packages/svelte/src/compiler/phases/2-analyze/index.js at line 330.
What does analyze_component() call?
analyze_component() calls 39 function(s): EachBlock, Identifier, Set, adjust, analyze_css, calculate_blockers, create_attribute, create_scopes, and 31 more.
What calls analyze_component()?
analyze_component() is called by 2 function(s): compile, migrate.

Analyze Your Own Codebase

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

Try Supermodel Free