Home / Function/ add_const_declaration() — svelte Function Reference

add_const_declaration() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  3db638b9_c8ef_82ff_27cb_ef03d1bc9940["add_const_declaration()"]
  c2ea7651_b458_9fe9_2c66_78f1a5171027["ConstTag.js"]
  3db638b9_c8ef_82ff_27cb_ef03d1bc9940 -->|defined in| c2ea7651_b458_9fe9_2c66_78f1a5171027
  5e885faa_389a_5da2_c8e7_33dd0668923e["ConstTag()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| 3db638b9_c8ef_82ff_27cb_ef03d1bc9940
  style 3db638b9_c8ef_82ff_27cb_ef03d1bc9940 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/visitors/ConstTag.js lines 96–134

function add_const_declaration(state, id, expression, metadata, bindings) {
	// we need to eagerly evaluate the expression in order to hit any
	// 'Cannot access x before initialization' errors
	const after = dev ? [b.stmt(b.call('$.get', id))] : [];

	const has_await = metadata.has_await;
	const blockers = [...metadata.dependencies]
		.map((dep) => dep.blocker)
		.filter((b) => b !== null && b.object !== state.async_consts?.id);

	if (has_await || state.async_consts || blockers.length > 0) {
		const run = (state.async_consts ??= {
			id: b.id(state.scope.generate('promises')),
			thunks: []
		});

		state.consts.push(b.let(id));

		const assignment = b.assignment('=', id, expression);
		const body = after.length === 0 ? assignment : b.block([b.stmt(assignment), ...after]);

		if (blockers.length === 1) {
			run.thunks.push(b.thunk(b.member(/** @type {Expression} */ (blockers[0]), 'promise')));
		} else if (blockers.length > 0) {
			run.thunks.push(b.thunk(b.call('$.wait', b.array(blockers))));
		}

		run.thunks.push(b.thunk(body, has_await));

		const blocker = b.member(run.id, b.literal(run.thunks.length - 1), true);

		for (const binding of bindings) {
			binding.blocker = blocker;
		}
	} else {
		state.consts.push(b.const(id, expression));
		state.consts.push(...after);
	}
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does add_const_declaration() do?
add_const_declaration() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/ConstTag.js.
Where is add_const_declaration() defined?
add_const_declaration() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/ConstTag.js at line 96.
What calls add_const_declaration()?
add_const_declaration() is called by 1 function(s): ConstTag.

Analyze Your Own Codebase

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

Try Supermodel Free