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

96.6% Statements 313/324
93.47% Branches 86/92
100% Functions 3/3
96.52% Lines 306/317

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 3182x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3426x 3426x 3426x 3426x 968x 969x 969x 969x 969x 969x 969x 969x 969x 969x 685x 969x 284x 8x 8x 8x 8x 8x 8x 276x 276x 276x 685x 969x 136x 136x 136x 136x     136x 136x 2x 2x 2x 2x       2x 2x 136x 134x 134x 134x 192x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 177x 7x 177x 4x 4x 177x 177x 70x 70x 70x 70x 192x 15x 15x 15x 15x 15x 1x 1x 1x 15x 15x 15x 192x 134x 136x 136x 136x 136x 549x 549x 549x 969x 969x 969x 473x 473x 473x 473x 473x 473x 473x 473x 473x 143x 143x 473x 354x 354x 473x 473x 473x 473x 471x 471x 471x 473x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 473x 473x 473x 76x 969x 76x 67x 67x 67x 67x 67x 67x 76x 9x 9x 9x 9x 9x 9x 9x 9x 9x 4x 9x 5x 5x 5x 5x 5x 5x 5x 9x 9x 19x 19x 19x 19x 19x 9x 76x 76x 969x 3426x 2458x 2798x 2798x 2798x 2798x 2798x 513x 513x 513x             513x 513x 513x 513x 2285x 2798x 1940x 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 1935x 1935x 1935x 1935x 1935x 1935x 1935x 1935x 1935x 1940x 1940x 1940x 1940x 1940x 1940x 1940x 1940x 345x 345x 345x 345x 345x 2798x 2798x 2798x 2798x 2458x 3426x 3426x 84x 84x 3342x 3342x 3342x 3342x 3342x 3342x 2x 2x 2x 2x 2x 2x 2x 345x 345x 343x 343x 343x 343x 343x 343x 343x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x  
/** @import { CallExpression, Expression, Identifier, Literal, VariableDeclaration, VariableDeclarator } from 'estree' */
/** @import { Binding } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
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 } from '../utils.js';
import { is_hoisted_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 === '$host'
			) {
				if (init != null && is_hoisted_function(init)) {
					context.state.hoisted.push(
						b.const(declarator.id, /** @type {Expression} */ (context.visit(init)))
					);
 
					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(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.raw') {
				/**
				 * @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 (rune === '$state' && should_proxy(value, context.state.scope)) {
						value = b.call('$.proxy', value);
					}
					if (is_state_source(binding, context.state.analysis)) {
						value = b.call('$.state', 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 === 'raw_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 = extract_paths(declarator.id);
 
					const init = /** @type {CallExpression} */ (declarator.init);
 
					/** @type {Identifier} */
					let id;
					let rhs = value;
 
					if (rune === '$derived' && init.arguments[0].type === 'Identifier') {
						id = init.arguments[0];
					} else {
						id = b.id(context.state.scope.generate('$$d'));
						rhs = b.call('$.get', id);
 
						declarations.push(
							b.declarator(id, b.call('$.derived', rune === '$derived.by' ? value : b.thunk(value)))
						);
					}
 
					for (let i = 0; i < bindings.length; i++) {
						const binding = bindings[i];
						declarations.push(
							b.declarator(binding.node, b.call('$.derived', b.thunk(binding.expression(rhs))))
						);
					}
				}
				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_hoisted_function(init)) {
					context.state.hoisted.push(
						b.const(declarator.id, /** @type {Expression} */ (context.visit(init)))
					);

					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,
					/** @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 in legacy mode.
 * @param {VariableDeclarator} declarator
 * @param {ComponentClientTransformState} scope
 * @param {Expression} value
 */
function create_state_declarators(declarator, { scope, analysis }, value) {
	if (declarator.id.type === 'Identifier') {
		return [
			b.declarator(
				declarator.id,
				b.call('$.mutable_state', value, analysis.immutable ? b.true : undefined)
			)
		];
	}
 
	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_state', value, analysis.immutable ? b.true : undefined)
					: value
			);
		})
	];
}