Home / Function/ migrate_slot_usage() — svelte Function Reference

migrate_slot_usage() — svelte Function Reference

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

Function javascript Compiler Transformer calls 2 called by 6

Entity Profile

Dependency Diagram

graph TD
  cdc1fb24_8bb0_cade_bc99_13f6efb7c385["migrate_slot_usage()"]
  cab41022_1b55_3b7a_06c6_b90763bbea47["index.js"]
  cdc1fb24_8bb0_cade_bc99_13f6efb7c385 -->|defined in| cab41022_1b55_3b7a_06c6_b90763bbea47
  a08d16d0_ebd3_b975_18ce_1fcee3350ed6["template.RegularElement()"]
  a08d16d0_ebd3_b975_18ce_1fcee3350ed6 -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  efe8c251_8c53_8933_be63_87752f930714["template.SvelteElement()"]
  efe8c251_8c53_8933_be63_87752f930714 -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  ab5678ee_461c_12c9_475a_410b00568874["template.Component()"]
  ab5678ee_461c_12c9_475a_410b00568874 -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  7c4b9e22_124a_58a4_bd27_fe6e869ae9b4["template.SvelteComponent()"]
  7c4b9e22_124a_58a4_bd27_fe6e869ae9b4 -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  bd253e16_7b1c_c3a7_9546_f0dd65a52306["template.SvelteFragment()"]
  bd253e16_7b1c_c3a7_9546_f0dd65a52306 -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  75841e11_8cd1_4c07_bd42_ece901a4a83a["template.SlotElement()"]
  75841e11_8cd1_4c07_bd42_ece901a4a83a -->|calls| cdc1fb24_8bb0_cade_bc99_13f6efb7c385
  653284b2_68fd_eee3_0064_918a4c065d4a["is_text_attribute()"]
  cdc1fb24_8bb0_cade_bc99_13f6efb7c385 -->|calls| 653284b2_68fd_eee3_0064_918a4c065d4a
  a53788f3_4288_6c23_bd50_e754a22ca567["is_reserved()"]
  cdc1fb24_8bb0_cade_bc99_13f6efb7c385 -->|calls| a53788f3_4288_6c23_bd50_e754a22ca567
  style cdc1fb24_8bb0_cade_bc99_13f6efb7c385 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/migrate/index.js lines 1420–1582

function migrate_slot_usage(node, path, state) {
	const parent = path.at(-2);
	// Bail on custom element slot usage
	if (
		parent?.type !== 'Component' &&
		parent?.type !== 'SvelteComponent' &&
		node.type !== 'Component' &&
		node.type !== 'SvelteComponent'
	) {
		return;
	}

	let snippet_name = 'children';
	let snippet_props = [];

	// if we stop the transform because the name is not correct we don't want to
	// remove the let directive and they could come before the name
	let removal_queue = [];

	for (let attribute of node.attributes) {
		if (
			attribute.type === 'Attribute' &&
			attribute.name === 'slot' &&
			is_text_attribute(attribute)
		) {
			snippet_name = attribute.value[0].data;
			// the default slot in svelte 4 if what the children slot is for svelte 5
			if (snippet_name === 'default') {
				snippet_name = 'children';
			}
			if (!regex_is_valid_identifier.test(snippet_name) || is_reserved(snippet_name)) {
				has_migration_task = true;
				state.str.appendLeft(
					node.start,
					`<!-- @migration-task: migrate this slot by hand, \`${snippet_name}\` is an invalid identifier -->\n${state.indent}`
				);
				return;
			}
			if (parent?.type === 'Component' || parent?.type === 'SvelteComponent') {
				for (let attribute of parent.attributes) {
					if (attribute.type === 'Attribute' || attribute.type === 'BindDirective') {
						if (attribute.name === snippet_name) {
							state.str.appendLeft(
								node.start,
								`<!-- @migration-task: migrate this slot by hand, \`${snippet_name}\` would shadow a prop on the parent component -->\n${state.indent}`
							);
							return;
						}
					}
				}
			}
			// flush the queue after we found the name
			for (let remove_let of removal_queue) {
				remove_let();
			}
			state.str.remove(attribute.start, attribute.end);
		}
		if (attribute.type === 'LetDirective') {
			snippet_props.push(
				attribute.name +
					(attribute.expression
						? `: ${state.str.original.substring(/** @type {number} */ (attribute.expression.start), /** @type {number} */ (attribute.expression.end))}`
						: '')
			);
			// we just add to the queue to remove them after we found if we need to migrate or we bail
			removal_queue.push(() => state.str.remove(attribute.start, attribute.end));
		}
	}

	if (removal_queue.length > 0) {
		for (let remove_let of removal_queue) {
			remove_let();
		}
	}

	if (node.type === 'SvelteFragment' && node.fragment.nodes.length > 0) {
		// remove node itself, keep content
		state.str.remove(node.start, node.fragment.nodes[0].start);
		state.str.remove(node.fragment.nodes[node.fragment.nodes.length - 1].end, node.end);
	}

Domain

Subdomains

Frequently Asked Questions

What does migrate_slot_usage() do?
migrate_slot_usage() is a function in the svelte codebase, defined in packages/svelte/src/compiler/migrate/index.js.
Where is migrate_slot_usage() defined?
migrate_slot_usage() is defined in packages/svelte/src/compiler/migrate/index.js at line 1420.
What does migrate_slot_usage() call?
migrate_slot_usage() calls 2 function(s): is_reserved, is_text_attribute.
What calls migrate_slot_usage()?
migrate_slot_usage() is called by 6 function(s): template.Component, template.RegularElement, template.SlotElement, template.SvelteComponent, template.SvelteElement, template.SvelteFragment.

Analyze Your Own Codebase

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

Try Supermodel Free