Repository: sveltejs/svelte
File: packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js (JavaScript)

Code before:
				states++;
			}
		}
	}

	return { states, calls };
}

/**
 * @param {Array<AST.Text | AST.ExpressionTag>} values
 * @param {(node: SvelteNode, state: any) => any} visit
 * @param {ComponentClientTransformState} state
 * @returns {{ value: Expression, has_state: boolean, has_call: boolean }}
 */
export function build_template_chunk(values, visit, state) {
	/** @type {Expression[]} */
	const expressions = [];

	let quasi = b.quasi('');
	const quasis = [quasi];

	const { states, calls } = get_states_and_calls(values);

	let has_call = calls > 0;
	let has_state = states > 0;
	let contains_multiple_call_expression = calls > 1;

	for (let i = 0; i < values.length; i++) {
		const node = values[i];

		if (node.type === 'Text') {
			quasi.value.cooked += node.data;
		} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
			if (node.expression.value != null) {
				quasi.value.cooked += node.expression.value + '';
			}
		} else {
			if (contains_multiple_call_expression) {
				const id = b.id(state.scope.generate('stringified_text'));
				state.init.push(
					b.const(
						id,
						create_derived(
							state,
							b.thunk(
								b.logical(
									'??',
									/** @type {Expression} */ (visit(node.expression, state)),
									b.literal('')
								)
							)

Review comment (suggestion):
```suggestion
	let has_call = false;
	let has_state = false;
	let contains_multiple_call_expression = false;

	for (const node of values) {
		if (node.type === 'ExpressionTag') {
			const metadata = node.metadata.expression;

			contains_multiple_call_expression ||= has_call && metadata.has_call;
			has_call ||= metadata.has_call;
			has_state ||= metadata.has_state;
		}
	}
```

Code after:
	let has_state = false;
	let contains_multiple_call_expression = false;

	for (const node of values) {
		if (node.type === 'ExpressionTag') {
			const metadata = node.metadata.expression;

			contains_multiple_call_expression ||= has_call && metadata.has_call;
			has_call ||= metadata.has_call;
			has_state ||= metadata.has_state;
		}
	}

	for (let i = 0; i < values.length; i++) {
		const node = values[i];

		if (node.type === 'Text') {
			quasi.value.cooked += node.data;
		} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
			if (node.expression.value != null) {
				quasi.value.cooked += node.expression.value + '';
			}
		} else {
			if (contains_multiple_call_expression) {
				const id = b.id(state.scope.generate('stringified_text'));
				state.init.push(
					b.const(
						id,
						create_derived(
							state,
							b.thunk(
								b.logical(
									'??',
									/** @type {Expression} */ (visit(node.expression, state)),
									b.literal('')
								)
							)
						)
					)
				);
				expressions.push(b.call('$.get', id));
			} else if (values.length === 1) {
				// If we have a single expression, then pass that in directly to possibly avoid doing
				// extra work in the template_effect (instead we do the work in set_text).
				return { value: visit(node.expression, state), has_state, has_call };
			} else {
				expressions.push(b.logical('??', visit(node.expression, state), b.literal('')));
			}

			quasi = b.quasi('', i + 1 === values.length);
			quasis.push(quasi);