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

100% Statements 333/333
100% Branches 99/99
100% Functions 12/12
100% Lines 328/328

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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 3292x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9619x 9619x 1217x 9619x 9619x 2x 2x 2x 2x 2x 2x 2x 14884x 6397x 6397x 6397x 6397x 6397x 6397x 6397x 8487x 8487x 8487x 2x 2x 2x 2x 2x 2x 174x 174x 20x 20x 20x 20x 20x 20x 20x 174x 174x 2x 2x 2x 2x 2x 2x 204x 204x 204x 204x 204x 204x 204x 204x 204x 204x 204x 227x 227x 227x 227x 204x 204x 303x 303x 303x 226x 7x 7x 7x 7x 226x 226x 226x 226x 226x 226x 226x 226x 226x 226x 226x 226x 1x 226x 1x 226x 225x 225x 26x 225x 6x 225x 219x 219x 219x 212x 212x 212x 212x 212x 1x 1x 212x 226x 303x 204x 204x 2x 2x 2x 2x 2x 2x 2x 204x 204x 204x 204x 204x 204x 204x 193x 180x 180x 180x 204x 11x 11x 11x 11x 204x 204x 204x 204x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2026x 2026x 2026x 2026x 2026x 2026x 72x 72x 2026x 2026x 64x 64x 2026x 2026x 2026x 95x 95x 95x 2026x 1961x 1961x 2026x 2026x 2026x 2026x 2026x 1009x 1009x 780x 1009x 229x 229x 229x 19x 229x 10x 229x 219x 219x 229x 229x 229x 1009x 2026x 2026x 2007x 2007x 2007x 2026x 2026x 2026x 2x 2x 2x 2x 2x 2x 2x 2x 2308x 2308x 2308x 2308x 2308x 2308x 241x 241x 2308x 2308x 2308x 2x 2x 2x 2x 2x 2x 855x 855x 855x 855x 855x 855x 855x 855x 407x 855x 501x 501x 855x 85x 85x 85x 85x 85x 85x 85x 85x 85x 20x 85x 19x 19x 85x 335x 335x 2x 2x 2x 2x 2x 2x 2x 2x 2x 731x 648x 648x 83x 83x 2x 2x 2x 2x 2x 2x 2x 113x 78x 78x 78x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 97x 89x 89x 89x 89x 89x 89x 35x 35x 35x 2x 2x 2x 2x 2x 2x 2x 385x 385x  
/** @import { ArrowFunctionExpression, Expression, FunctionDeclaration, FunctionExpression, Identifier, Pattern, PrivateIdentifier, Statement } from 'estree' */
/** @import { Binding, SvelteNode } from '#compiler' */
/** @import { ClientTransformState, ComponentClientTransformState, ComponentContext } from './types.js' */
/** @import { Scope } from '../../scope.js' */
import * as b from '../../../utils/builders.js';
import { extract_identifiers, is_simple_expression } from '../../../utils/ast.js';
import {
	PROPS_IS_LAZY_INITIAL,
	PROPS_IS_IMMUTABLE,
	PROPS_IS_RUNES,
	PROPS_IS_UPDATED
} from '../../../../constants.js';
import { dev } from '../../../state.js';
import { get_value } from './visitors/shared/declarations.js';
 
/**
 * @param {Binding} binding
 * @param {ClientTransformState} state
 * @returns {boolean}
 */
export function is_state_source(binding, state) {
	return (
		(binding.kind === 'state' || binding.kind === 'frozen_state') &&
		(!state.analysis.immutable || binding.reassigned || state.analysis.accessors)
	);
}
 
/**
 * @param {Identifier} node
 * @param {ClientTransformState} state
 * @returns {Expression}
 */
export function build_getter(node, state) {
	if (Object.hasOwn(state.transform, node.name)) {
		const binding = state.scope.get(node.name);
 
		// don't transform the declaration itself
		if (node !== binding?.node) {
			return state.transform[node.name].read(node);
		}
	}
 
	return node;
}
 
/**
 * @param {Expression} value
 * @param {PrivateIdentifier | string} proxy_reference
 */
export function build_proxy_reassignment(value, proxy_reference) {
	return dev
		? b.call(
				'$.proxy',
				value,
				b.null,
				typeof proxy_reference === 'string'
					? b.id(proxy_reference)
					: b.member(b.this, proxy_reference)
			)
		: b.call('$.proxy', value);
}
 
/**
 * @param {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} node
 * @param {ComponentContext} context
 * @returns {Pattern[]}
 */
function get_hoisted_params(node, context) {
	const scope = context.state.scope;
 
	/** @type {Identifier[]} */
	const params = [];
 
	/**
	 * We only want to push if it's not already present to avoid name clashing
	 * @param {Identifier} id
	 */
	function push_unique(id) {
		if (!params.find((param) => param.name === id.name)) {
			params.push(id);
		}
	}
 
	for (const [reference] of scope.references) {
		let binding = scope.get(reference);
 
		if (binding !== null && !scope.declarations.has(reference) && binding.initial !== node) {
			if (binding.kind === 'store_sub') {
				// We need both the subscription for getting the value and the store for updating
				push_unique(b.id(binding.node.name));
				binding = /** @type {Binding} */ (scope.get(binding.node.name.slice(1)));
			}
 
			let expression = context.state.transform[reference]?.read(b.id(binding.node.name));
 
			if (
				// If it's a destructured derived binding, then we can extract the derived signal reference and use that.
				// TODO this code is bad, we need to kill it
				expression != null &&
				typeof expression !== 'function' &&
				expression.type === 'MemberExpression' &&
				expression.object.type === 'CallExpression' &&
				expression.object.callee.type === 'Identifier' &&
				expression.object.callee.name === '$.get' &&
				expression.object.arguments[0].type === 'Identifier'
			) {
				push_unique(b.id(expression.object.arguments[0].name));
			} else if (
				// If we are referencing a simple $$props value, then we need to reference the object property instead
				(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
				!is_prop_source(binding, context.state)
			) {
				push_unique(b.id('$$props'));
			} else if (
				// imports don't need to be hoisted
				binding.declaration_kind !== 'import'
			) {
				// create a copy to remove start/end tags which would mess up source maps
				push_unique(b.id(binding.node.name));
				// rest props are often accessed through the $$props object for optimization reasons,
				// but we can't know if the delegated event handler will use it, so we need to add both as params
				if (binding.kind === 'rest_prop' && context.state.analysis.runes) {
					push_unique(b.id('$$props'));
				}
			}
		}
	}
	return params;
}
 
/**
 * @param {FunctionDeclaration | FunctionExpression | ArrowFunctionExpression} node
 * @param {ComponentContext} context
 * @returns {Pattern[]}
 */
export function build_hoisted_params(node, context) {
	const hoisted_params = get_hoisted_params(node, context);
	node.metadata.hoisted_params = hoisted_params;
 
	/** @type {Pattern[]} */
	const params = [];
 
	if (node.params.length === 0) {
		if (hoisted_params.length > 0) {
			// For the event object
			params.push(b.id('_'));
		}
	} else {
		for (const param of node.params) {
			params.push(/** @type {Pattern} */ (context.visit(param)));
		}
	}
 
	params.push(...hoisted_params);
	return params;
}
 
/**
 * @param {Binding} binding
 * @param {ComponentClientTransformState} state
 * @param {string} name
 * @param {Expression | null} [initial]
 * @returns
 */
export function get_prop_source(binding, state, name, initial) {
	/** @type {Expression[]} */
	const args = [b.id('$$props'), b.literal(name)];
 
	let flags = 0;
 
	if (state.analysis.immutable) {
		flags |= PROPS_IS_IMMUTABLE;
	}
 
	if (state.analysis.runes) {
		flags |= PROPS_IS_RUNES;
	}
 
	if (
		state.analysis.accessors ||
		(state.analysis.immutable
			? binding.reassigned || (state.analysis.runes && binding.mutated)
			: binding.mutated)
	) {
		flags |= PROPS_IS_UPDATED;
	}
 
	/** @type {Expression | undefined} */
	let arg;
 
	if (initial) {
		// To avoid eagerly evaluating the right-hand-side, we wrap it in a thunk if necessary
		if (is_simple_expression(initial)) {
			arg = initial;
		} else {
			if (
				initial.type === 'CallExpression' &&
				initial.callee.type === 'Identifier' &&
				initial.arguments.length === 0
			) {
				arg = initial.callee;
			} else {
				arg = b.thunk(initial);
			}
 
			flags |= PROPS_IS_LAZY_INITIAL;
		}
	}
 
	if (flags || arg) {
		args.push(b.literal(flags));
		if (arg) args.push(arg);
	}
 
	return b.call('$.prop', ...args);
}
 
/**
 *
 * @param {Binding} binding
 * @param {ClientTransformState} state
 * @returns
 */
export function is_prop_source(binding, state) {
	return (
		(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
		(!state.analysis.runes ||
			state.analysis.accessors ||
			binding.reassigned ||
			binding.initial ||
			// Until legacy mode is gone, we also need to use the prop source when only mutated is true,
			// because the parent could be a legacy component which needs coarse-grained reactivity
			binding.mutated)
	);
}
 
/**
 * @param {Expression} node
 * @param {Scope | null} scope
 */
export function should_proxy_or_freeze(node, scope) {
	if (
		!node ||
		node.type === 'Literal' ||
		node.type === 'TemplateLiteral' ||
		node.type === 'ArrowFunctionExpression' ||
		node.type === 'FunctionExpression' ||
		node.type === 'UnaryExpression' ||
		node.type === 'BinaryExpression' ||
		(node.type === 'Identifier' && node.name === 'undefined')
	) {
		return false;
	}
	if (node.type === 'Identifier' && scope !== null) {
		const binding = scope.get(node.name);
		// Let's see if the reference is something that can be proxied or frozen
		if (
			binding !== null &&
			!binding.reassigned &&
			binding.initial !== null &&
			binding.initial.type !== 'FunctionDeclaration' &&
			binding.initial.type !== 'ClassDeclaration' &&
			binding.initial.type !== 'ImportDeclaration' &&
			binding.initial.type !== 'EachBlock'
		) {
			return should_proxy_or_freeze(binding.initial, null);
		}
	}
	return true;
}
 
/**
 * Port over the location information from the source to the target identifier.
 * but keep the target as-is (i.e. a new id is created).
 * This ensures esrap can generate accurate source maps.
 * @param {Identifier} target
 * @param {Identifier} source
 */
export function with_loc(target, source) {
	if (source.loc) {
		return { ...target, loc: source.loc };
	}
	return target;
}
 
/**
 * @param {Pattern} node
 * @param {import('zimmerframe').Context<SvelteNode, ComponentClientTransformState>} context
 * @returns {{ id: Pattern, declarations: null | Statement[] }}
 */
export function create_derived_block_argument(node, context) {
	if (node.type === 'Identifier') {
		context.state.transform[node.name] = { read: get_value };
		return { id: node, declarations: null };
	}
 
	const pattern = /** @type {Pattern} */ (context.visit(node));
	const identifiers = extract_identifiers(node);
 
	const id = b.id('$$source');
	const value = b.id('$$value');
 
	const block = b.block([
		b.var(pattern, b.call('$.get', id)),
		b.return(b.object(identifiers.map((identifier) => b.prop('init', identifier, identifier))))
	]);
 
	const declarations = [b.var(value, create_derived(context.state, b.thunk(block)))];
 
	for (const id of identifiers) {
		context.state.transform[id.name] = { read: get_value };
 
		declarations.push(
			b.var(id, create_derived(context.state, b.thunk(b.member(b.call('$.get', value), id))))
		);
	}
 
	return { id, declarations };
}
 
/**
 * Svelte legacy mode should use safe equals in most places, runes mode shouldn't
 * @param {ComponentClientTransformState} state
 * @param {Expression} arg
 */
export function create_derived(state, arg) {
	return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
}