All files / src/compiler/phases/3-transform/client/visitors Fragment.js

100% Statements 274/274
98.5% Branches 66/67
100% Functions 8/8
100% Lines 267/267

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 2682x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 162x 162x 3952x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 4115x 224x 224x 3952x 4115x 250x 250x 250x 3952x 3952x 3952x 3952x 3952x 3952x 2580x 2580x 94x 94x 94x 94x 94x 94x 94x 2580x 2580x 3952x 3952x 4115x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 1672x 6x 6x 1672x 1672x 1672x 1672x 1672x 4115x 31x 31x 2280x 147x 147x 147x 147x 147x 147x 147x 2249x 2080x 2080x 2080x 2080x 2080x 2080x 2080x 165x 165x 165x 165x 165x 165x 165x 165x 165x 165x 2080x 1915x 367x 367x 1915x 1548x 1548x 1548x 1548x 1548x 1548x 1548x 1548x 5x 5x 1547x 1548x 639x 639x 1547x 908x 908x 908x 908x 1547x 1547x 1547x 1914x 1914x 1914x 2102x 22x 22x 3951x 4113x 1296x 1296x 3951x 3951x 3951x 4097x 3531x 3531x 3531x 3531x 3531x 3951x 3951x 3951x 2x 2x 2x 2x 2580x 2580x 2580x 2580x 2580x 17723x 17719x 17723x 4x 4x 4x 17723x 2580x 2580x 2584x 2584x 2580x 2580x 2580x 2580x 2580x 2x 2x 2x 2x 2x 2x 2x 2580x 2580x 2580x 2580x 44x 44x 2580x 2536x 2536x 2531x 2580x 2580x 2x 2x 2x 2x 95x 95x 95x 117x 117x 117x 1x 1x 117x 117x 95x 95x 95x  
/** @import { Expression, Identifier, Statement } from 'estree' */
/** @import { AST, Namespace } from '#compiler' */
/** @import { SourceLocation } from '#shared' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
import { TEMPLATE_FRAGMENT, TEMPLATE_USE_IMPORT_NODE } from '../../../../../constants.js';
import { dev } from '../../../../state.js';
import * as b from '../../../../utils/builders.js';
import { sanitize_template_string } from '../../../../utils/sanitize_template_string.js';
import { clean_nodes, infer_namespace } from '../../utils.js';
import { process_children } from './shared/fragment.js';
import { build_render_statement } from './shared/utils.js';
 
/**
 * @param {AST.Fragment} node
 * @param {ComponentContext} context
 */
export function Fragment(node, context) {
	// Creates a new block which looks roughly like this:
	// ```js
	// // hoisted:
	// const block_name = $.template(`...`);
	//
	// // for the main block:
	// const id = block_name();
	// // init stuff and possibly render effect
	// $.append($$anchor, id);
	// ```
	// Adds the hoisted parts to `context.state.hoisted` and returns the statements of the main block.
 
	const parent = context.path.at(-1) ?? node;
 
	const namespace = infer_namespace(context.state.metadata.namespace, parent, node.nodes);
 
	const { hoisted, trimmed, is_standalone, is_text_first } = clean_nodes(
		parent,
		node.nodes,
		context.path,
		namespace,
		context.state,
		context.state.preserve_whitespace,
		context.state.options.preserveComments
	);
 
	if (hoisted.length === 0 && trimmed.length === 0) {
		return b.block([]);
	}
 
	const is_single_element = trimmed.length === 1 && trimmed[0].type === 'RegularElement';
	const is_single_child_not_needing_template =
		trimmed.length === 1 &&
		(trimmed[0].type === 'SvelteFragment' || trimmed[0].type === 'TitleElement');
 
	const template_name = context.state.scope.root.unique('root'); // TODO infer name from parent
 
	/** @type {Statement[]} */
	const body = [];
 
	/** @type {Statement | undefined} */
	let close = undefined;
 
	/** @type {ComponentClientTransformState} */
	const state = {
		...context.state,
		before_init: [],
		init: [],
		update: [],
		after_update: [],
		template: [],
		locations: [],
		transform: { ...context.state.transform },
		metadata: {
			context: {
				template_needs_import_node: false,
				template_contains_script_tag: false
			},
			namespace,
			bound_contenteditable: context.state.metadata.bound_contenteditable
		}
	};
 
	for (const node of hoisted) {
		context.visit(node, state);
	}
 
	if (is_text_first) {
		// skip over inserted comment
		body.push(b.stmt(b.call('$.next')));
	}
 
	/**
	 * @param {Identifier} template_name
	 * @param {Expression[]} args
	 */
	const add_template = (template_name, args) => {
		let call = b.call(get_template_function(namespace, state), ...args);
		if (dev) {
			call = b.call(
				'$.add_locations',
				call,
				b.member(b.id(context.state.analysis.name), '$.FILENAME', true),
				build_locations(state.locations)
			);
		}
 
		context.state.hoisted.push(b.var(template_name, call));
	};
 
	if (is_single_element) {
		const element = /** @type {AST.RegularElement} */ (trimmed[0]);
 
		const id = b.id(context.state.scope.generate(element.name));
 
		context.visit(element, {
			...state,
			node: id
		});
 
		/** @type {Expression[]} */
		const args = [join_template(state.template)];
 
		if (state.metadata.context.template_needs_import_node) {
			args.push(b.literal(TEMPLATE_USE_IMPORT_NODE));
		}
 
		add_template(template_name, args);
 
		body.push(b.var(id, b.call(template_name)), ...state.before_init, ...state.init);
		close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
	} else if (is_single_child_not_needing_template) {
		context.visit(trimmed[0], state);
		body.push(...state.before_init, ...state.init);
	} else if (trimmed.length === 1 && trimmed[0].type === 'Text') {
		const id = b.id(context.state.scope.generate('text'));
		body.push(
			b.var(id, b.call('$.text', b.literal(trimmed[0].data))),
			...state.before_init,
			...state.init
		);
		close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
	} else if (trimmed.length > 0) {
		const id = b.id(context.state.scope.generate('fragment'));
 
		const use_space_template =
			trimmed.some((node) => node.type === 'ExpressionTag') &&
			trimmed.every((node) => node.type === 'Text' || node.type === 'ExpressionTag');
 
		if (use_space_template) {
			// special case — we can use `$.text` instead of creating a unique template
			const id = b.id(context.state.scope.generate('text'));
 
			process_children(trimmed, () => id, false, {
				...context,
				state
			});
 
			body.push(b.var(id, b.call('$.text')), ...state.before_init, ...state.init);
			close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
		} else {
			if (is_standalone) {
				// no need to create a template, we can just use the existing block's anchor
				process_children(trimmed, () => b.id('$$anchor'), false, { ...context, state });
			} else {
				/** @type {(is_text: boolean) => Expression} */
				const expression = (is_text) => b.call('$.first_child', id, is_text && b.true);
 
				process_children(trimmed, expression, false, { ...context, state });
 
				let flags = TEMPLATE_FRAGMENT;
 
				if (state.metadata.context.template_needs_import_node) {
					flags |= TEMPLATE_USE_IMPORT_NODE;
				}
 
				if (state.template.length === 1 && state.template[0] === '<!>') {
					// special case — we can use `$.comment` instead of creating a unique template
					body.push(b.var(id, b.call('$.comment')));
				} else {
					add_template(template_name, [join_template(state.template), b.literal(flags)]);
 
					body.push(b.var(id, b.call(template_name)));
				}
 
				close = b.stmt(b.call('$.append', b.id('$$anchor'), id));
			}
 
			body.push(...state.before_init, ...state.init);
		}
	} else {
		body.push(...state.before_init, ...state.init);
	}
 
	if (state.update.length > 0) {
		body.push(build_render_statement(state.update));
	}
 
	body.push(...state.after_update);
 
	if (close !== undefined) {
		// It's important that close is the last statement in the block, as any previous statements
		// could contain element insertions into the template, which the close statement needs to
		// know of when constructing the list of current inner elements.
		body.push(close);
	}
 
	return b.block(body);
}
 
/**
 * @param {Array<string | Expression>} items
 */
function join_template(items) {
	let quasi = b.quasi('');
	const template = b.template([quasi], []);
 
	for (const item of items) {
		if (typeof item === 'string') {
			quasi.value.cooked += item;
		} else {
			template.expressions.push(item);
			template.quasis.push((quasi = b.quasi('')));
		}
	}
 
	for (const quasi of template.quasis) {
		quasi.value.raw = sanitize_template_string(/** @type {string} */ (quasi.value.cooked));
	}
 
	quasi.tail = true;
 
	return template;
}
 
/**
 *
 * @param {Namespace} namespace
 * @param {ComponentClientTransformState} state
 * @returns
 */
function get_template_function(namespace, state) {
	const contains_script_tag = state.metadata.context.template_contains_script_tag;
	return namespace === 'svg'
		? contains_script_tag
			? '$.svg_template_with_script'
			: '$.ns_template'
		: namespace === 'mathml'
			? '$.mathml_template'
			: contains_script_tag
				? '$.template_with_script'
				: '$.template';
}
 
/**
 * @param {SourceLocation[]} locations
 */
function build_locations(locations) {
	return b.array(
		locations.map((loc) => {
			const expression = b.array([b.literal(loc[0]), b.literal(loc[1])]);
 
			if (loc.length === 3) {
				expression.elements.push(build_locations(loc[2]));
			}
 
			return expression;
		})
	);
}