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

100% Statements 244/244
100% Branches 61/61
100% Functions 7/7
100% Lines 239/239

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 2442x 2x 2x 2x 2x         2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8265x 8265x 8265x 8265x 4852x 4852x 4852x 4852x 4852x 4852x 4852x 6838x 6838x 6838x 4302x 4302x 6838x 15x 15x 15x 2536x 2521x 2521x 2521x 2521x 2521x 6838x 4852x 4852x 7373x 7373x 4852x 4852x 4852x 8265x 8265x 12972x 12972x 12972x 6838x 12972x 6134x 1877x 1877x 1877x 6134x 6134x 6134x 12972x 8265x 8265x 2975x 2975x 8265x 2x 2x 2x 2x 2x 23324x 23324x 23324x 2x 2x 2x 2x 2x 2x 2x 2x 4372x 4372x 4372x 4372x 4372x 4372x 4372x 4372x 4372x 4372x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 5030x 4372x 4372x 4372x 23324x 23324x 23324x 2380x 1487x 1487x 2380x 2380x 23324x 20944x 5030x 5030x 20944x 20944x 15415x 20944x 4852x 4852x 4852x 5529x 677x 677x 677x 20944x 23324x 23324x 4372x 3543x 3543x 4372x 4372x 4372x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2328x 2328x 2328x 2328x 2328x 2328x 27x 27x 2301x 2328x 2231x 2231x 2231x 1257x 1257x 1257x 1257x 1257x 1257x 974x 974x 974x 70x 70x 70x 70x 70x 70x 70x 2245x 182x 182x 182x 95x 95x 95x 182x 87x 87x 87x 87x 87x 87x 87x 182x 70x 70x 70x 2x 2x 2x 2x 2x 2x 2x 13262x 13262x 13262x 3656x 3656x 3656x 9606x 13262x 225x 225x 225x 225x 225x 225x 225x 225x 9381x 9381x 9381x  
/** @import { AssignmentOperator, Expression, Identifier, Node, Statement, TemplateElement } from 'estree' */
/** @import { AST, SvelteNode } from '#compiler' */
/** @import { ComponentContext, ServerTransformState } from '../../types.js' */
 
import { escape_html } from '../../../../../../escaping.js';
import {
	BLOCK_CLOSE,
	BLOCK_OPEN,
	EMPTY_COMMENT
} from '../../../../../../internal/server/hydration.js';
import * as b from '../../../../../utils/builders.js';
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
import { regex_whitespaces_strict } from '../../../../patterns.js';
 
/** Opens an if/each block, so that we can remove nodes in the case of a mismatch */
export const block_open = b.literal(BLOCK_OPEN);
 
/** Closes an if/each block, so that we can remove nodes in the case of a mismatch. Also serves as an anchor for these blocks */
export const block_close = b.literal(BLOCK_CLOSE);
 
/** Empty comment to keep text nodes separate, or provide an anchor node for blocks */
export const empty_comment = b.literal(EMPTY_COMMENT);
 
/**
 * Processes an array of template nodes, joining sibling text/expression nodes and
 * recursing into child nodes.
 * @param {Array<SvelteNode>} nodes
 * @param {ComponentContext} context
 */
export function process_children(nodes, { visit, state }) {
	/** @type {Array<AST.Text | AST.Comment | AST.ExpressionTag>} */
	let sequence = [];
 
	function flush() {
		let quasi = b.quasi('', false);
		const quasis = [quasi];
 
		/** @type {Expression[]} */
		const expressions = [];
 
		for (let i = 0; i < sequence.length; i++) {
			const node = sequence[i];
 
			if (node.type === 'Text' || node.type === 'Comment') {
				quasi.value.cooked +=
					node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
			} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
				if (node.expression.value != null) {
					quasi.value.cooked += escape_html(node.expression.value + '');
				}
			} else {
				expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));
 
				quasi = b.quasi('', i + 1 === sequence.length);
				quasis.push(quasi);
			}
		}
 
		for (const quasi of quasis) {
			quasi.value.raw = sanitize_template_string(/** @type {string} */ (quasi.value.cooked));
		}
 
		state.template.push(b.template(quasis, expressions));
	}
 
	for (let i = 0; i < nodes.length; i += 1) {
		const node = nodes[i];
 
		if (node.type === 'Text' || node.type === 'Comment' || node.type === 'ExpressionTag') {
			sequence.push(node);
		} else {
			if (sequence.length > 0) {
				flush();
				sequence = [];
			}
 
			visit(node, { ...state });
		}
	}
 
	if (sequence.length > 0) {
		flush();
	}
}
 
/**
 * @param {Node} node
 * @returns {node is Statement}
 */
function is_statement(node) {
	return node.type.endsWith('Statement') || node.type.endsWith('Declaration');
}
 
/**
 * @param {Array<Statement | Expression>} template
 * @param {Identifier} out
 * @param {AssignmentOperator} operator
 * @returns {Statement[]}
 */
export function build_template(template, out = b.id('$$payload.out'), operator = '+=') {
	/** @type {string[]} */
	let strings = [];
 
	/** @type {Expression[]} */
	let expressions = [];
 
	/** @type {Statement[]} */
	const statements = [];
 
	const flush = () => {
		statements.push(
			b.stmt(
				b.assignment(
					operator,
					out,
					b.template(
						strings.map((cooked, i) => b.quasi(cooked, i === strings.length - 1)),
						expressions
					)
				)
			)
		);
		strings = [];
		expressions = [];
	};
 
	for (let i = 0; i < template.length; i++) {
		const node = template[i];
 
		if (is_statement(node)) {
			if (strings.length !== 0) {
				flush();
			}
 
			statements.push(node);
		} else {
			if (strings.length === 0) {
				strings.push('');
			}
 
			if (node.type === 'Literal') {
				strings[strings.length - 1] += node.value;
			} else if (node.type === 'TemplateLiteral') {
				strings[strings.length - 1] += node.quasis[0].value.cooked;
				strings.push(...node.quasis.slice(1).map((q) => /** @type {string} */ (q.value.cooked)));
				expressions.push(...node.expressions);
			} else {
				expressions.push(node);
				strings.push('');
			}
		}
	}
 
	if (strings.length !== 0) {
		flush();
	}
 
	return statements;
}
 
/**
 *
 * @param {AST.Attribute['value']} value
 * @param {ComponentContext} context
 * @param {boolean} trim_whitespace
 * @param {boolean} is_component
 * @returns {Expression}
 */
export function build_attribute_value(
	value,
	context,
	trim_whitespace = false,
	is_component = false
) {
	if (value === true) {
		return b.true;
	}
 
	if (!Array.isArray(value) || value.length === 1) {
		const chunk = Array.isArray(value) ? value[0] : value;
 
		if (chunk.type === 'Text') {
			const data = trim_whitespace
				? chunk.data.replace(regex_whitespaces_strict, ' ').trim()
				: chunk.data;
 
			return b.literal(is_component ? data : escape_html(data, true));
		}
 
		return /** @type {Expression} */ (context.visit(chunk.expression));
	}
 
	let quasi = b.quasi('', false);
	const quasis = [quasi];
 
	/** @type {Expression[]} */
	const expressions = [];
 
	for (let i = 0; i < value.length; i++) {
		const node = value[i];
 
		if (node.type === 'Text') {
			quasi.value.raw += trim_whitespace
				? node.data.replace(regex_whitespaces_strict, ' ')
				: node.data;
		} else {
			expressions.push(
				b.call('$.stringify', /** @type {Expression} */ (context.visit(node.expression)))
			);
 
			quasi = b.quasi('', i + 1 === value.length);
			quasis.push(quasi);
		}
	}
 
	return b.template(quasis, expressions);
}
 
/**
 * @param {Identifier} node
 * @param {ServerTransformState} state
 * @returns {Expression}
 */
export function build_getter(node, state) {
	const binding = state.scope.get(node.name);
 
	if (binding === null || node === binding.node) {
		// No associated binding or the declaration itself which shouldn't be transformed
		return node;
	}
 
	if (binding.kind === 'store_sub') {
		const store_id = b.id(node.name.slice(1));
		return b.call(
			'$.store_get',
			b.assignment('??=', b.id('$$store_subs'), b.object([])),
			b.literal(node.name),
			build_getter(store_id, state)
		);
	}
 
	return node;
}