Home / Function/ add_state_transformers() — svelte Function Reference

add_state_transformers() — svelte Function Reference

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

Function javascript Compiler Transformer calls 1 called by 2

Entity Profile

Dependency Diagram

graph TD
  fb41e547_07c8_cc9a_e227_3f08aad6d1f5["add_state_transformers()"]
  1a53d630_ca18_6783_bd92_8c72517a9306["declarations.js"]
  fb41e547_07c8_cc9a_e227_3f08aad6d1f5 -->|defined in| 1a53d630_ca18_6783_bd92_8c72517a9306
  b555c552_6b1f_078c_a5da_1ef7d9983b25["BlockStatement()"]
  b555c552_6b1f_078c_a5da_1ef7d9983b25 -->|calls| fb41e547_07c8_cc9a_e227_3f08aad6d1f5
  fa0e8946_29c3_f58a_ab56_307427332bf0["Program()"]
  fa0e8946_29c3_f58a_ab56_307427332bf0 -->|calls| fb41e547_07c8_cc9a_e227_3f08aad6d1f5
  03ec0ace_6e52_c362_5e4b_06e8333b938f["is_state_source()"]
  fb41e547_07c8_cc9a_e227_3f08aad6d1f5 -->|calls| 03ec0ace_6e52_c362_5e4b_06e8333b938f
  style fb41e547_07c8_cc9a_e227_3f08aad6d1f5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/declarations.js lines 18–53

export function add_state_transformers(context) {
	for (const [name, binding] of context.state.scope.declarations) {
		if (
			is_state_source(binding, context.state.analysis) ||
			binding.kind === 'derived' ||
			binding.kind === 'legacy_reactive'
		) {
			context.state.transform[name] = {
				read: binding.declaration_kind === 'var' ? (node) => b.call('$.safe_get', node) : get_value,
				assign: (node, value, proxy = false) => {
					let call = b.call('$.set', node, value, proxy && b.true);

					if (context.state.scope.get(`$${node.name}`)?.kind === 'store_sub') {
						call = b.call('$.store_unsub', call, b.literal(`$${node.name}`), b.id('$$stores'));
					}

					return call;
				},
				mutate: (node, mutation) => {
					if (context.state.analysis.runes) {
						return mutation;
					}

					return b.call('$.mutate', node, mutation);
				},
				update: (node) => {
					return b.call(
						node.prefix ? '$.update_pre' : '$.update',
						node.argument,
						node.operator === '--' && b.literal(-1)
					);
				}
			};
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does add_state_transformers() do?
add_state_transformers() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/declarations.js.
Where is add_state_transformers() defined?
add_state_transformers() is defined in packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/declarations.js at line 18.
What does add_state_transformers() call?
add_state_transformers() calls 1 function(s): is_state_source.
What calls add_state_transformers()?
add_state_transformers() is called by 2 function(s): BlockStatement, Program.

Analyze Your Own Codebase

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

Try Supermodel Free