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

99.28% Statements 276/278
98.52% Branches 67/68
100% Functions 9/9
99.26% Lines 270/272

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 2732x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 160x 160x 3913x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 18582x 3716x 3716x 3716x 14866x 4074x 4074x 4x     4x 4x 4x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 4074x 224x 224x 3913x 4074x 246x 246x 246x 3913x 3913x 3913x 3913x 3913x 3913x 2551x 2551x 94x 94x 94x 94x 94x 94x 94x 2551x 2551x 3913x 3913x 4074x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 1658x 6x 6x 1658x 1658x 1658x 1658x 1658x 4074x 31x 31x 2255x 144x 144x 144x 144x 144x 144x 144x 2224x 2058x 2058x 2058x 2058x 2058x 2058x 2058x 164x 164x 164x 164x 164x 164x 164x 164x 164x 164x 2058x 1894x 362x 362x 1894x 1532x 1532x 1532x 1532x 1532x 1532x 1532x 1532x 4x 4x 1531x 1532x 638x 638x 1531x 893x 893x 893x 893x 893x 893x 893x 893x 893x 893x 1531x 1531x 1531x 1893x 1893x 1893x 2080x 22x 22x 3912x 4072x 1272x 1272x 3912x 3912x 3912x 4057x 3497x 3497x 3497x 3497x 3497x 3912x 3912x 3912x 2x 2x 2x 2x 2x 2x 2x 2551x 2551x 2551x 2551x 44x 44x 2551x 2507x 2507x 2502x 2551x 2551x 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 { 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 {string[]} */
	const quasi = [];
	/** @type {Expression[]} */
	const expressions = [];
 
	/** @type {ComponentClientTransformState} */
	const state = {
		...context.state,
		before_init: [],
		init: [],
		update: [],
		after_update: [],
		template: {
			push_quasi: (/** @type {string} */ quasi_to_add) => {
				if (quasi.length === 0) {
					quasi.push(quasi_to_add);
					return;
				}
				quasi[quasi.length - 1] = quasi[quasi.length - 1].concat(quasi_to_add);
			},
			push_expression: (/** @type {Expression} */ expression_to_add) => {
				if (quasi.length === 0) {
					quasi.push('');
				}
				expressions.push(expression_to_add);
				quasi.push('');
			}
		},
		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 = [
			b.template(
				quasi.map((q) => b.quasi(q, true)),
				expressions
			)
		];
 
		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 (quasi.length === 1 && quasi[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, [
						b.template(
							quasi.map((q) => b.quasi(q, true)),
							expressions
						),
						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 {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;
		})
	);
}