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

96.11% Statements 322/335
94.38% Branches 84/89
100% Functions 3/3
96.03% Lines 315/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 2x 3303x 3303x 3303x 3303x 855x 856x 856x 856x 856x 856x 856x 856x 856x 856x 608x 856x 248x 7x 7x 7x 7x 7x 7x 241x 241x 241x 608x 856x 120x 120x 120x 120x     120x 120x 1x 1x 1x 1x       1x 1x 119x 119x 119x 119x 172x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 4x 158x 2x 2x 158x 158x 63x 63x 63x 63x 172x 14x 14x 14x 14x 14x 1x 1x 1x 14x 14x 14x 172x 119x 120x 120x 120x 120x 488x 488x 488x 856x 856x 856x 428x 428x 428x 428x 428x 428x 428x 428x 428x 132x 132x 428x 322x 322x 428x 428x 428x 428x 426x 426x 426x 428x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 428x 428x 428x 60x 856x 60x 55x 55x 55x 55x 55x 55x 60x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 60x 60x 856x 3303x 2448x 2788x 2788x 2788x 2788x 2788x 510x 510x 510x                 510x 510x 510x 510x 2278x 2788x 1937x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5x 5x 5x 1932x 1932x 1932x 1932x 1932x 1932x 1932x 1932x 1932x 1937x 1937x 1937x 1937x 1937x 1937x 1937x 1937x 341x 341x 341x 341x 341x 2788x 2788x 2788x 2788x 2448x 3303x 3303x 76x 76x 3227x 3227x 3227x 3227x 3227x 3227x 2x 2x 2x 2x 2x 2x 2x 341x 341x 339x 339x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 2x 2x 2x  
/** @import { CallExpression, Expression, Identifier, Literal, VariableDeclaration, VariableDeclarator } from 'estree' */
/** @import { Binding } from '#compiler' */
/** @import { ComponentContext } from '../types' */
/** @import { Scope } from '../../../scope' */
import { dev } from '../../../../state.js';
import { extract_paths } from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js';
import * as assert from '../../../../utils/assert.js';
import { get_rune } from '../../../scope.js';
import {
	get_prop_source,
	is_prop_source,
	is_state_source,
	should_proxy_or_freeze
} from '../utils.js';
import { is_hoistable_function } from '../../utils.js';
 
/**
 * @param {VariableDeclaration} node
 * @param {ComponentContext} context
 */
export function VariableDeclaration(node, context) {
	/** @type {VariableDeclarator[]} */
	const declarations = [];
 
	if (context.state.analysis.runes) {
		for (const declarator of node.declarations) {
			const init = declarator.init;
			const rune = get_rune(init, context.state.scope);
 
			if (
				!rune ||
				rune === '$effect.tracking' ||
				rune === '$effect.root' ||
				rune === '$inspect' ||
				rune === '$state.snapshot' ||
				rune === '$state.is'
			) {
				if (init != null && is_hoistable_function(init)) {
					const hoistable_function = context.visit(init);
					context.state.hoisted.push(
						b.declaration('const', declarator.id, /** @type {Expression} */ (hoistable_function))
					);
					continue;
				}
				declarations.push(/** @type {VariableDeclarator} */ (context.visit(declarator)));
				continue;
			}
 
			if (rune === '$props') {
				/** @type {string[]} */
				const seen = ['$$slots', '$$events', '$$legacy'];
 
				if (context.state.analysis.custom_element) {
					seen.push('$$host');
				}
 
				if (declarator.id.type === 'Identifier') {
					/** @type {Expression[]} */
					const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
 
					if (dev) {
						// include rest name, so we can provide informative error messages
						args.push(b.literal(declarator.id.name));
					}
 
					declarations.push(b.declarator(declarator.id, b.call('$.rest_props', ...args)));
				} else {
					assert.equal(declarator.id.type, 'ObjectPattern');
 
					for (const property of declarator.id.properties) {
						if (property.type === 'Property') {
							const key = /** @type {Identifier | Literal} */ (property.key);
							const name = key.type === 'Identifier' ? key.name : /** @type {string} */ (key.value);
 
							seen.push(name);
 
							let id =
								property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
							assert.equal(id.type, 'Identifier');
							const binding = /** @type {Binding} */ (context.state.scope.get(id.name));
							let initial =
								binding.initial && /** @type {Expression} */ (context.visit(binding.initial));
							// We're adding proxy here on demand and not within the prop runtime function so that
							// people not using proxied state anywhere in their code don't have to pay the additional bundle size cost
							if (
								initial &&
								binding.kind === 'bindable_prop' &&
								should_proxy_or_freeze(initial, context.state.scope)
							) {
								initial = b.call('$.proxy', initial);
							}
 
							if (is_prop_source(binding, context.state)) {
								declarations.push(
									b.declarator(id, get_prop_source(binding, context.state, name, initial))
								);
							}
						} else {
							// RestElement
							/** @type {Expression[]} */
							const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
 
							if (dev) {
								// include rest name, so we can provide informative error messages
								args.push(b.literal(/** @type {Identifier} */ (property.argument).name));
							}
 
							declarations.push(b.declarator(property.argument, b.call('$.rest_props', ...args)));
						}
					}
				}
 
				// TODO
				continue;
			}
 
			const args = /** @type {CallExpression} */ (init).arguments;
			const value =
				args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (context.visit(args[0]));
 
			if (rune === '$state' || rune === '$state.frozen') {
				/**
				 * @param {Identifier} id
				 * @param {Expression} value
				 */
				const create_state_declarator = (id, value) => {
					const binding = /** @type {import('#compiler').Binding} */ (
						context.state.scope.get(id.name)
					);
					if (should_proxy_or_freeze(value, context.state.scope)) {
						value = b.call(rune === '$state' ? '$.proxy' : '$.freeze', value);
					}
					if (is_state_source(binding, context.state)) {
						value = b.call('$.source', value);
					}
					return value;
				};
 
				if (declarator.id.type === 'Identifier') {
					declarations.push(
						b.declarator(declarator.id, create_state_declarator(declarator.id, value))
					);
				} else {
					const tmp = context.state.scope.generate('tmp');
					const paths = extract_paths(declarator.id);
					declarations.push(
						b.declarator(b.id(tmp), value),
						...paths.map((path) => {
							const value = path.expression?.(b.id(tmp));
							const binding = context.state.scope.get(/** @type {Identifier} */ (path.node).name);
							return b.declarator(
								path.node,
								binding?.kind === 'state' || binding?.kind === 'frozen_state'
									? create_state_declarator(binding.node, value)
									: value
							);
						})
					);
				}
 
				continue;
			}
 
			if (rune === '$derived' || rune === '$derived.by') {
				if (declarator.id.type === 'Identifier') {
					declarations.push(
						b.declarator(
							declarator.id,
							b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
						)
					);
				} else {
					const bindings = context.state.scope.get_bindings(declarator);
					const object_id = context.state.scope.generate('derived_object');
					const values_id = context.state.scope.generate('derived_values');
					declarations.push(
						b.declarator(
							b.id(object_id),
							b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value))
						)
					);
					declarations.push(
						b.declarator(
							b.id(values_id),
							b.call(
								'$.derived',
								b.thunk(
									b.block([
										b.let(declarator.id, b.call('$.get', b.id(object_id))),
										b.return(b.array(bindings.map((binding) => binding.node)))
									])
								)
							)
						)
					);
					for (let i = 0; i < bindings.length; i++) {
						const binding = bindings[i];
						declarations.push(
							b.declarator(
								binding.node,
								b.call(
									'$.derived',
									b.thunk(b.member(b.call('$.get', b.id(values_id)), b.literal(i), true))
								)
							)
						);
					}
				}
				continue;
			}
		}
	} else {
		for (const declarator of node.declarations) {
			const bindings = /** @type {Binding[]} */ (context.state.scope.get_bindings(declarator));
			const has_state = bindings.some((binding) => binding.kind === 'state');
			const has_props = bindings.some((binding) => binding.kind === 'bindable_prop');
 
			if (!has_state && !has_props) {
				const init = declarator.init;
 
				if (init != null && is_hoistable_function(init)) {
					const hoistable_function = context.visit(init);

					context.state.hoisted.push(
						b.declaration('const', declarator.id, /** @type {Expression} */ (hoistable_function))
					);

					continue;
				}
 
				declarations.push(/** @type {VariableDeclarator} */ (context.visit(declarator)));
				continue;
			}
 
			if (has_props) {
				if (declarator.id.type !== 'Identifier') {
					// Turn export let into props. It's really really weird because export let { x: foo, z: [bar]} = ..
					// means that foo and bar are the props (i.e. the leafs are the prop names), not x and z.
					const tmp = context.state.scope.generate('tmp');
					const paths = extract_paths(declarator.id);
 
					declarations.push(
						b.declarator(
							b.id(tmp),
							/** @type {Expression} */ (context.visit(/** @type {Expression} */ (declarator.init)))
						)
					);
 
					for (const path of paths) {
						const name = /** @type {Identifier} */ (path.node).name;
						const binding = /** @type {Binding} */ (context.state.scope.get(name));
						const value = path.expression?.(b.id(tmp));
						declarations.push(
							b.declarator(
								path.node,
								binding.kind === 'bindable_prop'
									? get_prop_source(binding, context.state, binding.prop_alias ?? name, value)
									: value
							)
						);
					}
 
					continue;
				}
 
				const binding = /** @type {Binding} */ (context.state.scope.get(declarator.id.name));
 
				declarations.push(
					b.declarator(
						declarator.id,
						get_prop_source(
							binding,
							context.state,
							binding.prop_alias ?? declarator.id.name,
							declarator.init && /** @type {Expression} */ (context.visit(declarator.init))
						)
					)
				);
 
				continue;
			}
 
			declarations.push(
				...create_state_declarators(
					declarator,
					context.state.scope,
					/** @type {Expression} */ (declarator.init && context.visit(declarator.init))
				)
			);
		}
	}
 
	if (declarations.length === 0) {
		return b.empty;
	}
 
	return {
		...node,
		declarations
	};
}
 
/**
 * Creates the output for a state declaration.
 * @param {VariableDeclarator} declarator
 * @param {Scope} scope
 * @param {Expression} value
 */
function create_state_declarators(declarator, scope, value) {
	if (declarator.id.type === 'Identifier') {
		return [b.declarator(declarator.id, b.call('$.mutable_source', value))];
	}
 
	const tmp = scope.generate('tmp');
	const paths = extract_paths(declarator.id);
	return [
		b.declarator(b.id(tmp), value),
		...paths.map((path) => {
			const value = path.expression?.(b.id(tmp));
			const binding = scope.get(/** @type {Identifier} */ (path.node).name);
			return b.declarator(
				path.node,
				binding?.kind === 'state' ? b.call('$.mutable_source', value) : value
			);
		})
	];
}