MCPcopy
hub / github.com/sveltejs/svelte / clean_nodes

Function clean_nodes

packages/svelte/src/compiler/phases/3-transform/utils.js:126–306  ·  view source on GitHub ↗
(
	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
)

Source from the content-addressed store, hash-verified

124 * @param {boolean} preserve_comments
125 */
126export function clean_nodes(
127 parent,
128 nodes,
129 path,
130 namespace = 'html',
131 state,
132 // TODO give these defaults (state.options.preserveWhitespace and state.options.preserveComments).
133 // first, we need to make `Component(Client|Server)TransformState` inherit from a new `ComponentTransformState`
134 // rather than from `ClientTransformState` and `ServerTransformState`
135 preserve_whitespace,
136 preserve_comments
137) {
138 if (!state.analysis.runes) {
139 nodes = sort_const_tags(nodes, state);
140 }
141
142 /** @type {AST.SvelteNode[]} */
143 const hoisted = [];
144
145 /** @type {AST.SvelteNode[]} */
146 const regular = [];
147
148 for (const node of nodes) {
149 if (node.type === 'Comment' && !preserve_comments) {
150 continue;
151 }
152
153 if (
154 node.type === 'ConstTag' ||
155 node.type === 'DeclarationTag' ||
156 node.type === 'DebugTag' ||
157 node.type === 'SvelteBody' ||
158 node.type === 'SvelteWindow' ||
159 node.type === 'SvelteDocument' ||
160 node.type === 'SvelteHead' ||
161 node.type === 'TitleElement' ||
162 node.type === 'SnippetBlock'
163 ) {
164 // TODO others?
165 hoisted.push(node);
166 } else {
167 regular.push(node);
168 }
169 }
170
171 let trimmed = regular;
172
173 if (!preserve_whitespace) {
174 trimmed = [];
175
176 let first, last;
177
178 while (
179 (first = regular[0]) &&
180 first.type === 'Text' &&
181 !regex_not_whitespace.test(first.data)
182 ) {
183 regular.shift();

Callers 4

FragmentFunction · 0.90
RegularElementFunction · 0.90
FragmentFunction · 0.90
RegularElementFunction · 0.90

Calls 3

sort_const_tagsFunction · 0.85
pushMethod · 0.80
popMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…