Home / Function/ clean_nodes() — svelte Function Reference

clean_nodes() — svelte Function Reference

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

Function javascript Compiler Transformer calls 1 called by 4

Entity Profile

Dependency Diagram

graph TD
  48b02cf8_c90b_7278_8655_c24e3431a4b3["clean_nodes()"]
  f3fad5a9_6b91_ed4f_9331_7f9fc18491c3["utils.js"]
  48b02cf8_c90b_7278_8655_c24e3431a4b3 -->|defined in| f3fad5a9_6b91_ed4f_9331_7f9fc18491c3
  0236de7e_7608_f21c_040b_e454b977a608["Fragment()"]
  0236de7e_7608_f21c_040b_e454b977a608 -->|calls| 48b02cf8_c90b_7278_8655_c24e3431a4b3
  1e3b81f2_cd53_e5a5_0140_a9af38facf99["RegularElement()"]
  1e3b81f2_cd53_e5a5_0140_a9af38facf99 -->|calls| 48b02cf8_c90b_7278_8655_c24e3431a4b3
  018cf13b_00c1_1002_de36_76e71d0a0a5e["Fragment()"]
  018cf13b_00c1_1002_de36_76e71d0a0a5e -->|calls| 48b02cf8_c90b_7278_8655_c24e3431a4b3
  602abe50_d235_587e_7077_a4250fe9dc79["RegularElement()"]
  602abe50_d235_587e_7077_a4250fe9dc79 -->|calls| 48b02cf8_c90b_7278_8655_c24e3431a4b3
  1fda09d5_590d_7925_9231_ec17cf081e87["sort_const_tags()"]
  48b02cf8_c90b_7278_8655_c24e3431a4b3 -->|calls| 1fda09d5_590d_7925_9231_ec17cf081e87
  style 48b02cf8_c90b_7278_8655_c24e3431a4b3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/compiler/phases/3-transform/utils.js lines 126–305

export function clean_nodes(
	parent,
	nodes,
	path,
	namespace = 'html',
	state,
	// TODO give these defaults (state.options.preserveWhitespace and state.options.preserveComments).
	// first, we need to make `Component(Client|Server)TransformState` inherit from a new `ComponentTransformState`
	// rather than from `ClientTransformState` and `ServerTransformState`
	preserve_whitespace,
	preserve_comments
) {
	if (!state.analysis.runes) {
		nodes = sort_const_tags(nodes, state);
	}

	/** @type {AST.SvelteNode[]} */
	const hoisted = [];

	/** @type {AST.SvelteNode[]} */
	const regular = [];

	for (const node of nodes) {
		if (node.type === 'Comment' && !preserve_comments) {
			continue;
		}

		if (
			node.type === 'ConstTag' ||
			node.type === 'DebugTag' ||
			node.type === 'SvelteBody' ||
			node.type === 'SvelteWindow' ||
			node.type === 'SvelteDocument' ||
			node.type === 'SvelteHead' ||
			node.type === 'TitleElement' ||
			node.type === 'SnippetBlock'
		) {
			// TODO others?
			hoisted.push(node);
		} else {
			regular.push(node);
		}
	}

	let trimmed = regular;

	if (!preserve_whitespace) {
		trimmed = [];

		let first, last;

		while (
			(first = regular[0]) &&
			first.type === 'Text' &&
			!regex_not_whitespace.test(first.data)
		) {
			regular.shift();
		}

		if (first?.type === 'Text') {
			first.raw = first.raw.replace(regex_starts_with_whitespaces, '');
			first.data = first.data.replace(regex_starts_with_whitespaces, '');
		}

		while (
			(last = regular.at(-1)) &&
			last.type === 'Text' &&
			!regex_not_whitespace.test(last.data)
		) {
			regular.pop();
		}

		if (last?.type === 'Text') {
			last.raw = last.raw.replace(regex_ends_with_whitespaces, '');
			last.data = last.data.replace(regex_ends_with_whitespaces, '');
		}

		const can_remove_entirely =
			(namespace === 'svg' &&
				(parent.type !== 'RegularElement' || parent.name !== 'text') &&
				!path.some((n) => n.type === 'RegularElement' && n.name === 'text')) ||

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free