Home / Function/ ConstTag() — svelte Function Reference

ConstTag() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  5e885faa_389a_5da2_c8e7_33dd0668923e["ConstTag()"]
  c2ea7651_b458_9fe9_2c66_78f1a5171027["ConstTag.js"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|defined in| c2ea7651_b458_9fe9_2c66_78f1a5171027
  66243dec_e2be_a66d_3992_3ea42cf964b5["build_expression()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| 66243dec_e2be_a66d_3992_3ea42cf964b5
  c6147fac_8ab6_4ed2_9c90_08e83553fb43["create_derived()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| c6147fac_8ab6_4ed2_9c90_08e83553fb43
  3db638b9_c8ef_82ff_27cb_ef03d1bc9940["add_const_declaration()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| 3db638b9_c8ef_82ff_27cb_ef03d1bc9940
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| c12e0147_3f27_cf17_5878_e54ffdc328d5
  804afe56_25d1_9f41_dafe_adc75e952134["object()"]
  5e885faa_389a_5da2_c8e7_33dd0668923e -->|calls| 804afe56_25d1_9f41_dafe_adc75e952134
  style 5e885faa_389a_5da2_c8e7_33dd0668923e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

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

export function ConstTag(node, context) {
	const declaration = node.declaration.declarations[0];
	// TODO we can almost certainly share some code with $derived(...)
	if (declaration.id.type === 'Identifier') {
		const init = build_expression(context, declaration.init, node.metadata.expression);

		let expression = create_derived(context.state, init, node.metadata.expression.has_await);

		if (dev) {
			expression = b.call('$.tag', expression, b.literal(declaration.id.name));
		}

		context.state.transform[declaration.id.name] = { read: get_value };

		add_const_declaration(
			context.state,
			declaration.id,
			expression,
			node.metadata.expression,
			context.state.scope.get_bindings(declaration)
		);
	} else {
		const identifiers = extract_identifiers(declaration.id);
		const tmp = b.id(context.state.scope.generate('computed_const'));

		const transform = { ...context.state.transform };

		// Make all identifiers that are declared within the following computed regular
		// variables, as they are not signals in that context yet
		for (const node of identifiers) {
			delete transform[node.name];
		}

		const child_state = /** @type {ComponentContext['state']} */ ({
			...context.state,
			transform
		});

		// TODO optimise the simple `{ x } = y` case — we can just return `y`
		// instead of destructuring it only to return a new object
		const init = build_expression(
			{ ...context, state: child_state },
			declaration.init,
			node.metadata.expression
		);

		const block = b.block([
			b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
			b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
		]);

		let expression = create_derived(context.state, block, node.metadata.expression.has_await);

		if (dev) {
			expression = b.call('$.tag', expression, b.literal('[@const]'));
		}

		add_const_declaration(
			context.state,
			tmp,
			expression,
			node.metadata.expression,
			context.state.scope.get_bindings(declaration)
		);

		for (const node of identifiers) {
			context.state.transform[node.name] = {
				read: (node) => b.member(b.call('$.get', tmp), node)
			};
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does ConstTag() do?
ConstTag() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/ConstTag.js.
Where is ConstTag() defined?
ConstTag() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/ConstTag.js at line 16.
What does ConstTag() call?
ConstTag() calls 5 function(s): add_const_declaration, build_expression, create_derived, extract_identifiers, object.

Analyze Your Own Codebase

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

Try Supermodel Free