Home / Function/ sort_const_tags() — svelte Function Reference

sort_const_tags() — svelte Function Reference

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

Function javascript Compiler Transformer calls 4 called by 1

Entity Profile

Dependency Diagram

graph TD
  1fda09d5_590d_7925_9231_ec17cf081e87["sort_const_tags()"]
  f3fad5a9_6b91_ed4f_9331_7f9fc18491c3["utils.js"]
  1fda09d5_590d_7925_9231_ec17cf081e87 -->|defined in| f3fad5a9_6b91_ed4f_9331_7f9fc18491c3
  48b02cf8_c90b_7278_8655_c24e3431a4b3["clean_nodes()"]
  48b02cf8_c90b_7278_8655_c24e3431a4b3 -->|calls| 1fda09d5_590d_7925_9231_ec17cf081e87
  c12e0147_3f27_cf17_5878_e54ffdc328d5["extract_identifiers()"]
  1fda09d5_590d_7925_9231_ec17cf081e87 -->|calls| c12e0147_3f27_cf17_5878_e54ffdc328d5
  627dc2f8_4dbc_5bb1_8f54_cee503e17098["get()"]
  1fda09d5_590d_7925_9231_ec17cf081e87 -->|calls| 627dc2f8_4dbc_5bb1_8f54_cee503e17098
  d733cb1e_c294_8865_008a_cc79d441b336["check_graph_for_cycles()"]
  1fda09d5_590d_7925_9231_ec17cf081e87 -->|calls| d733cb1e_c294_8865_008a_cc79d441b336
  20ef4950_b934_7d43_6774_38040f901d47["const_tag_cycle()"]
  1fda09d5_590d_7925_9231_ec17cf081e87 -->|calls| 20ef4950_b934_7d43_6774_38040f901d47
  style 1fda09d5_590d_7925_9231_ec17cf081e87 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/utils.js lines 21–109

function sort_const_tags(nodes, state) {
	/**
	 * @typedef {{
	 *   node: AST.ConstTag;
	 *   deps: Set<Binding>;
	 * }} Tag
	 */

	const other = [];

	/** @type {Map<Binding, Tag>} */
	const tags = new Map();

	for (const node of nodes) {
		if (node.type === 'ConstTag') {
			const declaration = node.declaration.declarations[0];

			const bindings = extract_identifiers(declaration.id).map((id) => {
				return /** @type {Binding} */ (state.scope.get(id.name));
			});

			/** @type {Set<Binding>} */
			const deps = new Set();

			walk(declaration.init, state, {
				// @ts-expect-error don't know, don't care
				_: set_scope,
				Identifier(node, context) {
					const parent = /** @type {Expression} */ (context.path.at(-1));

					if (is_reference(node, parent)) {
						const binding = context.state.scope.get(node.name);
						if (binding) deps.add(binding);
					}
				}
			});

			for (const binding of bindings) {
				tags.set(binding, { node, deps });
			}
		} else {
			other.push(node);
		}
	}

	if (tags.size === 0) {
		return nodes;
	}

	/** @type {Array<[Binding, Binding]>} */
	const edges = [];

	for (const [id, tag] of tags) {
		for (const dep of tag.deps) {
			if (tags.has(dep)) {
				edges.push([id, dep]);
			}
		}
	}

	const cycle = check_graph_for_cycles(edges);
	if (cycle?.length) {
		const tag = /** @type {Tag} */ (tags.get(cycle[0]));
		e.const_tag_cycle(tag.node, cycle.map((binding) => binding.node.name).join(' → '));
	}

	/** @type {AST.ConstTag[]} */
	const sorted = [];

	/** @param {Tag} tag */
	function add(tag) {
		if (sorted.includes(tag.node)) {
			return;
		}

		for (const dep of tag.deps) {
			const dep_tag = tags.get(dep);
			if (dep_tag) add(dep_tag);
		}

		sorted.push(tag.node);

Domain

Subdomains

Called By

Frequently Asked Questions

What does sort_const_tags() do?
sort_const_tags() is a function in the svelte codebase, defined in packages/svelte/src/compiler/phases/3-transform/utils.js.
Where is sort_const_tags() defined?
sort_const_tags() is defined in packages/svelte/src/compiler/phases/3-transform/utils.js at line 21.
What does sort_const_tags() call?
sort_const_tags() calls 4 function(s): check_graph_for_cycles, const_tag_cycle, extract_identifiers, get.
What calls sort_const_tags()?
sort_const_tags() is called by 1 function(s): clean_nodes.

Analyze Your Own Codebase

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

Try Supermodel Free