All files / src/compiler/phases/3-transform utils.js

99.35% Statements 307/309
96.99% Branches 129/133
100% Functions 9/9
99.34% Lines 304/306

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 3072x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 570x 570x 570x 498x 570x 72x 72x 498x 498x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 35461x 71x 71x 35390x 35390x 35438x 35461x 35461x 35461x 35461x 35461x 35461x 35014x 35461x 446x 446x 35461x 34944x 34944x 35461x 14806x 14806x 106x 106x 14700x 14700x 14700x 14806x 7788x 7788x 14700x 14806x 3127x 3127x 3127x 14700x 14806x 3750x 3750x 14700x 14806x 2260x 2260x 2260x 14700x 14700x 14806x 14806x 14806x 14456x 6946x 6946x 6946x 6946x 6946x 6946x 6946x 6946x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 14806x 23293x 23293x 23293x 23293x 23293x 7643x 6447x 6447x 6447x 6447x 6447x 6447x 6447x 6447x 6447x 6447x 6447x 7643x 5648x 5648x 5648x 7643x 7516x 7516x 23293x 15650x 15650x 23293x 14700x 14700x 14700x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7618x 7618x 7618x 5357x 7618x 7618x 7618x 7617x     7617x 7617x 130x 12x 12x 130x 130x 7487x 7487x 7487x 7617x 7617x 7617x 7617x 7617x 2341x 7617x 5226x 5226x 3728x 3728x 5226x 7617x 3760x 3760x 3760x 2x 2x 2x 2x 2x 2x 2x 2x 5226x 5226x 5226x 5226x 5226x 5226x 3744x 3644x 3644x 3744x 86x 86x 5226x 5226x 5226x 11246x 11246x 11246x 11246x 11246x 16030x 16030x 16030x 16030x 16030x 16030x 16030x 16030x 10280x 16030x 12594x 12594x 11246x 11246x 11246x 11246x 6844x 322x 322x 6844x 11246x 11246x 11246x 11246x 11246x 1582x 1582x 1582x 2x 2x 2x 2x 2x 2x 2x 2x 7318x 1x 1x 7317x 7317x 10x 10x 7307x 7317x 172x 172x 7135x 7318x 7318x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 22x 22x 22x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 22x 18x 18x 18x 18x 18x 22x  
import {
	regex_ends_with_whitespaces,
	regex_not_whitespace,
	regex_starts_with_whitespaces
} from '../patterns.js';
import * as b from '../../utils/builders.js';
import { walk } from 'zimmerframe';
 
/**
 * @param {import('estree').Node} node
 * @returns {boolean}
 */
export function is_hoistable_function(node) {
	if (
		node.type === 'ArrowFunctionExpression' ||
		node.type === 'FunctionExpression' ||
		node.type === 'FunctionDeclaration'
	) {
		return node.metadata?.hoistable === true;
	}
	return false;
}
 
/**
 * Extract nodes that are hoisted and trim whitespace according to the following rules:
 * - trim leading and trailing whitespace, regardless of surroundings
 * - keep leading / trailing whitespace of inbetween text nodes,
 *   unless it's whitespace-only, in which case collapse to a single whitespace for all cases
 *   except when it's children of certain elements where we know ignore whitespace (like td/option/head),
 *   in which case we remove it entirely
 * @param {import('#compiler').SvelteNode} parent
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {import('#compiler').SvelteNode[]} path
 * @param {import('#compiler').Namespace} namespace
 * @param {boolean} preserve_whitespace
 * @param {boolean} preserve_comments
 */
export function clean_nodes(
	parent,
	nodes,
	path,
	namespace = 'html',
	preserve_whitespace,
	preserve_comments
) {
	/** @type {import('#compiler').SvelteNode[]} */
	const hoisted = [];
 
	/** @type {import('#compiler').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);
		}
	}
 
	if (preserve_whitespace) {
		return { hoisted, trimmed: regular };
	}
 
	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')) ||
		(parent.type === 'RegularElement' &&
			// TODO others?
			(parent.name === 'select' ||
				parent.name === 'tr' ||
				parent.name === 'table' ||
				parent.name === 'tbody' ||
				parent.name === 'thead' ||
				parent.name === 'tfoot' ||
				parent.name === 'colgroup' ||
				parent.name === 'datalist'));
 
	/** @type {import('#compiler').SvelteNode[]} */
	const trimmed = [];
 
	// Replace any whitespace between a text and non-text node with a single spaceand keep whitespace
	// as-is within text nodes, or between text nodes and expression tags (because in the end they count
	// as one text). This way whitespace is mostly preserved when using CSS with `white-space: pre-line`
	// and default slot content going into a pre tag (which we can't see).
	for (let i = 0; i < regular.length; i++) {
		const prev = regular[i - 1];
		const node = regular[i];
		const next = regular[i + 1];
 
		if (node.type === 'Text') {
			if (prev?.type !== 'ExpressionTag') {
				const prev_is_text_ending_with_whitespace =
					prev?.type === 'Text' && regex_ends_with_whitespaces.test(prev.data);
				node.data = node.data.replace(
					regex_starts_with_whitespaces,
					prev_is_text_ending_with_whitespace ? '' : ' '
				);
				node.raw = node.raw.replace(
					regex_starts_with_whitespaces,
					prev_is_text_ending_with_whitespace ? '' : ' '
				);
			}
			if (next?.type !== 'ExpressionTag') {
				node.data = node.data.replace(regex_ends_with_whitespaces, ' ');
				node.raw = node.raw.replace(regex_ends_with_whitespaces, ' ');
			}
			if (node.data && (node.data !== ' ' || !can_remove_entirely)) {
				trimmed.push(node);
			}
		} else {
			trimmed.push(node);
		}
	}
 
	return { hoisted, trimmed };
}
 
/**
 * Infers the namespace for the children of a node that should be used when creating the `$.template(...)`.
 * @param {import('#compiler').Namespace} namespace
 * @param {import('#compiler').SvelteNode} parent
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {import('#compiler').SvelteNode[]} path
 */
export function infer_namespace(namespace, parent, nodes, path) {
	const parent_node =
		parent.type === 'Fragment'
			? // Messy: We know that Fragment calls create_block directly, so we can do this here
				path.at(-1)
			: parent;
 
	if (namespace !== 'foreign') {
		if (parent_node?.type === 'RegularElement' && parent_node.name === 'foreignObject') {
			return 'html';
		}
 
		if (parent_node?.type === 'RegularElement' || parent_node?.type === 'SvelteElement') {
			if (parent_node.metadata.svg) {
				return 'svg';
			}
			return parent_node.metadata.mathml ? 'mathml' : 'html';
		}
 
		// Re-evaluate the namespace inside slot nodes that reset the namespace
		if (
			parent_node === undefined ||
			parent_node.type === 'Root' ||
			parent_node.type === 'Component' ||
			parent_node.type === 'SvelteComponent' ||
			parent_node.type === 'SvelteFragment' ||
			parent_node.type === 'SnippetBlock'
		) {
			const new_namespace = check_nodes_for_namespace(nodes, 'keep');
			if (new_namespace !== 'keep' && new_namespace !== 'maybe_html') {
				return new_namespace;
			}
		}
	}
 
	return namespace;
}
 
/**
 * Heuristic: Keep current namespace, unless we find a regular element,
 * in which case we always want html, or we only find svg nodes,
 * in which case we assume svg.
 * @param {import('#compiler').SvelteNode[]} nodes
 * @param {import('#compiler').Namespace | 'keep' | 'maybe_html'} namespace
 */
function check_nodes_for_namespace(nodes, namespace) {
	/**
	 * @param {import('#compiler').SvelteElement | import('#compiler').RegularElement} node}
	 * @param {{stop: () => void}} context
	 */
	const RegularElement = (node, { stop }) => {
		if (!node.metadata.svg && !node.metadata.mathml) {
			namespace = 'html';
			stop();
		} else if (namespace === 'keep') {
			namespace = node.metadata.svg ? 'svg' : 'mathml';
		}
	};
 
	for (const node of nodes) {
		walk(
			node,
			{},
			{
				_(node, { next }) {
					if (
						node.type === 'EachBlock' ||
						node.type === 'IfBlock' ||
						node.type === 'AwaitBlock' ||
						node.type === 'Fragment' ||
						node.type === 'KeyBlock' ||
						node.type === 'RegularElement' ||
						node.type === 'SvelteElement' ||
						node.type === 'Text'
					) {
						next();
					}
				},
				SvelteElement: RegularElement,
				RegularElement,
				Text(node) {
					if (node.data.trim() !== '') {
						namespace = 'maybe_html';
					}
				}
			}
		);
 
		if (namespace === 'html') return namespace;
	}
 
	return namespace;
}
 
/**
 * Determines the namespace the children of this node are in.
 * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement} node
 * @param {import('#compiler').Namespace} namespace
 * @returns {import('#compiler').Namespace}
 */
export function determine_namespace_for_children(node, namespace) {
	if (namespace === 'foreign') {
		return namespace;
	}
 
	if (node.name === 'foreignObject') {
		return 'html';
	}
 
	if (node.metadata.svg) {
		return 'svg';
	}
 
	return node.metadata.mathml ? 'mathml' : 'html';
}
 
/**
 * @template {import('./types.js').TransformState} T
 * @param {import('estree').CallExpression} node
 * @param {import('zimmerframe').Context<any, T>} context
 */
export function transform_inspect_rune(node, context) {
	const { state, visit } = context;
	const as_fn = state.options.generate === 'client';
 
	if (!state.options.dev) return b.unary('void', b.literal(0));
 
	if (node.callee.type === 'MemberExpression') {
		const raw_inspect_args = /** @type {import('estree').CallExpression} */ (node.callee.object)
			.arguments;
		const inspect_args =
			/** @type {Array<import('estree').Expression>} */
			(raw_inspect_args.map((arg) => visit(arg)));
		const with_arg = /** @type {import('estree').Expression} */ (visit(node.arguments[0]));
 
		return b.call(
			'$.inspect',
			as_fn ? b.thunk(b.array(inspect_args)) : b.array(inspect_args),
			with_arg
		);
	} else {
		const arg = node.arguments.map(
			(arg) => /** @type {import('estree').Expression} */ (visit(arg))
		);
		return b.call('$.inspect', as_fn ? b.thunk(b.array(arg)) : b.array(arg));
	}
}