Home / Function/ instance_script.LabeledStatement() — svelte Function Reference

instance_script.LabeledStatement() — svelte Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  5d553a43_30d1_f81d_668e_5d7134967ec9["instance_script.LabeledStatement()"]
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  5d553a43_30d1_f81d_668e_5d7134967ec9 -->|defined in| cab41022_1b55_3b7a_06c6_b90763bbea47
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  5d553a43_30d1_f81d_668e_5d7134967ec9 -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers()"]
  5d553a43_30d1_f81d_668e_5d7134967ec9 -->|calls| c12e0147_3f27_cf17_5878_e54ffdc328d5
  f37fcfdb_9d8e_8c81_7fc8_e49a289d52bc["extract_all_identifiers_from_expression()"]
  5d553a43_30d1_f81d_668e_5d7134967ec9 -->|calls| f37fcfdb_9d8e_8c81_7fc8_e49a289d52bc
  style 5d553a43_30d1_f81d_668e_5d7134967ec9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/migrate/index.js lines 925–1038

	LabeledStatement(node, { path, state, next }) {
		if (state.analysis.runes) return;
		if (path.length > 1) return;
		if (node.label.name !== '$') return;
		if (state.derived_labeled_statements.has(node)) return;

		next();

		/**
		 * @param {"state"|"derived"} rune
		 */
		function check_rune_binding(rune) {
			const has_rune_binding = state.scope.get(rune);
			if (has_rune_binding) {
				throw new MigrationError(
					`can't migrate \`$: ${state.str.original.substring(/** @type {number} */ (node.body.start), node.body.end)}\` to \`$${rune}\` because there's a variable named ${rune}.\n     Rename the variable and try again or migrate by hand.`
				);
			}
		}

		if (
			node.body.type === 'ExpressionStatement' &&
			node.body.expression.type === 'AssignmentExpression'
		) {
			const { left, right } = node.body.expression;

			const ids = extract_identifiers(left);
			const [, expression_ids] = extract_all_identifiers_from_expression(right);
			const bindings = ids.map((id) => /** @type {Binding} */ (state.scope.get(id.name)));

			if (bindings.every((b) => b.kind === 'legacy_reactive')) {
				if (
					right.type !== 'Literal' &&
					bindings.every((b) => b.kind !== 'store_sub') &&
					left.type !== 'MemberExpression'
				) {
					let { start, end } = /** @type {{ start: number, end: number }} */ (right);

					check_rune_binding('derived');

					// $derived
					state.str.update(
						/** @type {number} */ (node.start),
						/** @type {number} */ (node.body.expression.start),
						'let '
					);

					if (right.type === 'SequenceExpression') {
						while (state.str.original[start] !== '(') start -= 1;
						while (state.str.original[end - 1] !== ')') end += 1;
					}

					state.str.prependRight(start, `$derived(`);

					// in a case like `$: ({ a } = b())`, there's already a trailing parenthesis.
					// otherwise, we need to add one
					if (state.str.original[/** @type {number} */ (node.body.start)] !== '(') {
						state.str.appendLeft(end, `)`);
					}

					return;
				}

				for (const binding of bindings) {
					if (binding.reassigned && (ids.includes(binding.node) || expression_ids.length === 0)) {
						check_rune_binding('state');
						const init =
							binding.kind === 'state'
								? ' = $state()'
								: expression_ids.length === 0
									? ` = $state(${state.str.original.substring(/** @type {number} */ (right.start), right.end)})`
									: '';
						// implicitly-declared variable which we need to make explicit
						state.str.prependLeft(
							/** @type {number} */ (node.start),
							`let ${binding.node.name}${init};\n${state.indent}`
						);
					}
				}

				if (expression_ids.length === 0 && bindings.every((b) => b.kind !== 'store_sub')) {

Domain

Subdomains

Frequently Asked Questions

What does instance_script.LabeledStatement() do?
instance_script.LabeledStatement() is a function in the svelte codebase, defined in packages/svelte/src/compiler/migrate/index.js.
Where is instance_script.LabeledStatement() defined?
instance_script.LabeledStatement() is defined in packages/svelte/src/compiler/migrate/index.js at line 925.
What does instance_script.LabeledStatement() call?
instance_script.LabeledStatement() calls 3 function(s): extract_all_identifiers_from_expression, extract_identifiers, get.

Analyze Your Own Codebase

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

Try Supermodel Free