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

90.58% Statements 558/616
93.18% Branches 82/88
70.58% Functions 12/17
90.41% Lines 547/605

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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 6062x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6372x 6372x 6372x 6372x 6372x 25488x 151474x     151474x 151474x 151474x 151474x 25488x 6372x 6372x 6372x 2x 2x 2x 2x 2x 2x 2x 2x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 4264x 4264x 4264x 4264x 4264x 2122x 2122x 4264x 4264x 4264x 4264x 2122x 2122x 4264x 4264x 4264x 4264x 4264x 2122x 2122x 4264x 4264x 4264x 4264x 4264x 2122x 2122x 4264x 4264x 4264x 4264x 4264x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 786x 786x 2122x 2122x 1788x 1769x 1769x 19x 19x 1788x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 1758x 656x 2x 2x 2x 2x 656x 1758x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 7574x 62x 62x 7574x 129x 84x 84x 84x 84x 84x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 5x 5x 5x 5x 5x 129x 129x 129x 129x 7574x 2122x 2122x 135x 135x     135x 135x 2122x 2122x 101x 101x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 34x 34x 2122x 2122x 2122x 136x 136x 136x 136x 136x 136x 136x 136x 2122x 2122x 2122x 136x 136x 136x 124x 124x 12x 136x 2122x 2122x 2122x 2122x 7574x 2122x 2122x 2122x 47x 47x 15x 6x 6x 15x 47x 47x 47x 47x 2122x 2122x 1939x 2026x 2026x 2026x 2026x 2026x 2026x 2026x 2026x 2026x 2026x 32x 32x 32x 32x 32x 32x 32x 2026x 2026x 2026x 1939x 2122x 2122x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2122x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 6x 6x 27x 27x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2122x 2122x 19x 19x     19x 19x 19x 19x 2122x 2122x 3x 3x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 2122x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2122x 2122x 2122x 2122x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 2122x 2122x 2122x 2122x 2122x 2122x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2122x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 2122x 2122x 97x 97x 97x 97x 2122x 2122x                                                                                                         2122x 2122x 2122x 2122x 2122x 2122x 2122x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x  
import { walk } from 'zimmerframe';
import { error } from '../../../errors.js';
import * as b from '../../../utils/builders.js';
import { set_scope } from '../../scope.js';
import { template_visitors } from './visitors/template.js';
import { global_visitors } from './visitors/global.js';
import { javascript_visitors } from './visitors/javascript.js';
import { javascript_visitors_runes } from './visitors/javascript-runes.js';
import { javascript_visitors_legacy } from './visitors/javascript-legacy.js';
import { is_state_source, serialize_get_binding } from './utils.js';
import { render_stylesheet } from '../css/index.js';
 
/**
 * This function ensures visitor sets don't accidentally clobber each other
 * @param  {...import('./types').Visitors} array
 * @returns {import('./types').Visitors}
 */
function combine_visitors(...array) {
	/** @type {Record<string, any>} */
	const visitors = {};
 
	for (const member of array) {
		for (const key in member) {
			if (visitors[key]) {
				throw new Error(`Duplicate visitor: ${key}`);
			}
 
			// @ts-ignore
			visitors[key] = member[key];
		}
	}
 
	return visitors;
}
 
/**
 * @param {string} source
 * @param {import('../../types').ComponentAnalysis} analysis
 * @param {import('#compiler').ValidatedCompileOptions} options
 * @returns {import('estree').Program}
 */
export function client_component(source, analysis, options) {
	/** @type {import('./types').ComponentClientTransformState} */
	const state = {
		analysis,
		options,
		scope: analysis.module.scope,
		scopes: analysis.template.scopes,
		hoisted: [b.import_all('$', 'svelte/internal/client')],
		node: /** @type {any} */ (null), // populated by the root node
		// these should be set by create_block - if they're called outside, it's a bug
		get before_init() {
			/** @type {any[]} */
			const a = [];
			a.push = () =>
				error(null, 'INTERNAL', 'before_init.push should not be called outside create_block');
			return a;
		},
		get init() {
			/** @type {any[]} */
			const a = [];
			a.push = () => error(null, 'INTERNAL', 'init.push should not be called outside create_block');
			return a;
		},
		get update() {
			/** @type {any[]} */
			const a = [];
			a.push = () =>
				error(null, 'INTERNAL', 'update.push should not be called outside create_block');
			return a;
		},
		get after_update() {
			/** @type {any[]} */
			const a = [];
			a.push = () =>
				error(null, 'INTERNAL', 'after_update.push should not be called outside create_block');
			return a;
		},
		get template() {
			/** @type {any[]} */
			const a = [];
			a.push = () =>
				error(null, 'INTERNAL', 'template.push should not be called outside create_block');
			return a;
		},
		legacy_reactive_statements: new Map(),
		metadata: {
			context: {
				template_needs_import_node: false,
				template_contains_script_tag: false
			},
			namespace: options.namespace,
			bound_contenteditable: false
		},
		events: new Set(),
		preserve_whitespace: options.preserveWhitespace,
		public_state: new Map(),
		private_state: new Map(),
		in_constructor: false
	};
 
	const module = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.module.ast),
			state,
			combine_visitors(
				set_scope(analysis.module.scopes),
				global_visitors,
				// @ts-expect-error TODO
				javascript_visitors,
				analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy
			)
		)
	);
 
	const instance_state = { ...state, scope: analysis.instance.scope };
	const instance = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.instance.ast),
			instance_state,
			combine_visitors(
				set_scope(analysis.instance.scopes),
				global_visitors,
				// @ts-expect-error TODO
				javascript_visitors,
				analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy,
				{
					ImportDeclaration(node) {
						state.hoisted.push(node);
						return b.empty;
					},
					ExportNamedDeclaration(node, context) {
						if (node.declaration) {
							return context.visit(node.declaration);
						}
 
						return b.empty;
					}
				}
			)
		)
	);
 
	const template = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.template.ast),
			{ ...state, scope: analysis.instance.scope },
			combine_visitors(
				set_scope(analysis.template.scopes),
				global_visitors,
				// @ts-expect-error TODO
				template_visitors
			)
		)
	);
 
	// Very very dirty way of making import statements reactive in legacy mode if needed
	if (!analysis.runes) {
		for (const [name, binding] of analysis.module.scope.declarations) {
			if (binding.kind === 'legacy_reactive_import') {
				instance.body.unshift(
					b.var('$$_import_' + name, b.call('$.reactive_import', b.thunk(b.id(name))))
				);
			}
		}
	}
 
	/** @type {import('estree').Statement[]} */
	const store_setup = [];
 
	/** @type {import('estree').VariableDeclaration[]} */
	const legacy_reactive_declarations = [];
 
	for (const [name, binding] of analysis.instance.scope.declarations) {
		if (binding.kind === 'legacy_reactive') {
			legacy_reactive_declarations.push(b.const(name, b.call('$.mutable_source')));
		}
		if (binding.kind === 'store_sub') {
			if (store_setup.length === 0) {
				store_setup.push(
					b.const('$$subscriptions', b.object([])),
					b.stmt(b.call('$.unsubscribe_on_destroy', b.id('$$subscriptions')))
				);
			}
			// We're creating an arrow function that gets the store value which minifies better for two or more references
			const store_reference = serialize_get_binding(b.id(name.slice(1)), instance_state);
			const store_get = b.call(
				'$.store_get',
				store_reference,
				b.literal(name),
				b.id('$$subscriptions')
			);
			store_setup.push(
				b.const(
					binding.node,
					options.dev
						? b.thunk(
								b.sequence([
									b.call('$.validate_store', store_reference, b.literal(name.slice(1))),
									store_get
								])
							)
						: b.thunk(store_get)
				)
			);
		}
	}
 
	for (const [node] of analysis.reactive_statements) {
		const statement = [...state.legacy_reactive_statements].find(([n]) => n === node);
		if (statement === undefined) {
			error(node, 'INTERNAL', 'Could not find reactive statement');
		}
		instance.body.push(statement[1]);
	}
 
	if (analysis.reactive_statements.size > 0) {
		instance.body.push(b.stmt(b.call('$.legacy_pre_effect_reset')));
	}
 
	/**
	 * Used to store the group nodes
	 * @type {import('estree').VariableDeclaration[]}
	 */
	const group_binding_declarations = [];
	for (const group of analysis.binding_groups.values()) {
		group_binding_declarations.push(b.const(group.name, b.array([])));
	}
 
	// Bind static exports to props so that people can access them with bind:x
	const static_bindings = analysis.exports.map(({ name, alias }) => {
		return b.stmt(
			b.call(
				'$.bind_prop',
				b.id('$$props'),
				b.literal(alias ?? name),
				serialize_get_binding(b.id(name), instance_state)
			)
		);
	});
 
	const component_returned_object = analysis.exports.map(({ name, alias }) => {
		const expression = serialize_get_binding(b.id(name), instance_state);
 
		if (expression.type === 'Identifier' && !options.dev) {
			return b.init(alias ?? name, expression);
		}
 
		return b.get(alias ?? name, [b.return(expression)]);
	});
 
	const properties = [...analysis.instance.scope.declarations].filter(
		([name, binding]) =>
			(binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$')
	);
 
	if (analysis.runes && options.dev) {
		const bindable = analysis.exports.map(({ name, alias }) => b.literal(alias ?? name));
		for (const [name, binding] of properties) {
			if (binding.kind === 'bindable_prop') {
				bindable.push(b.literal(binding.prop_alias ?? name));
			}
		}
		instance.body.unshift(
			b.stmt(b.call('$.validate_prop_bindings', b.id('$$props'), b.array(bindable)))
		);
	}
 
	if (analysis.accessors) {
		for (const [name, binding] of properties) {
			const key = binding.prop_alias ?? name;
 
			const getter = b.get(key, [b.return(b.call(b.id(name)))]);
 
			const setter = b.set(key, [
				b.stmt(b.call(b.id(name), b.id('$$value'))),
				b.stmt(b.call('$.flush_sync'))
			]);
 
			if (analysis.runes && binding.initial) {
				// turn `set foo($$value)` into `set foo($$value = expression)`
				setter.value.params[0] = {
					type: 'AssignmentPattern',
					left: b.id('$$value'),
					right: /** @type {import('estree').Expression} */ (binding.initial)
				};
			}
 
			component_returned_object.push(getter, setter);
		}
	}
 
	if (options.legacy.componentApi) {
		component_returned_object.push(
			b.init('$set', b.id('$.update_legacy_props')),
			b.init(
				'$on',
				b.arrow(
					[b.id('$$event_name'), b.id('$$event_cb')],
					b.call(
						'$.add_legacy_event_listener',
						b.id('$$props'),
						b.id('$$event_name'),
						b.id('$$event_cb')
					)
				)
			)
		);
	} else if (options.dev) {
		component_returned_object.push(
			b.init(
				'$set',
				b.thunk(
					b.block([
						b.throw_error(
							`The component shape you get when doing bind:this changed. Updating its properties via $set is no longer valid in Svelte 5. ` +
								'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information'
						)
					])
				)
			),
			b.init(
				'$on',
				b.thunk(
					b.block([
						b.throw_error(
							`The component shape you get when doing bind:this changed. Listening to events via $on is no longer valid in Svelte 5. ` +
								'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information'
						)
					])
				)
			),
			b.init(
				'$destroy',
				b.thunk(
					b.block([
						b.throw_error(
							`The component shape you get when doing bind:this changed. Destroying such a component via $destroy is no longer valid in Svelte 5. ` +
								'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information'
						)
					])
				)
			)
		);
	}
 
	const push_args = [b.id('$$props'), b.literal(analysis.runes)];
	if (options.dev) push_args.push(b.id(analysis.name));
 
	const component_block = b.block([
		b.stmt(b.call('$.push', ...push_args)),
		...store_setup,
		...legacy_reactive_declarations,
		...group_binding_declarations,
		.../** @type {import('estree').Statement[]} */ (instance.body),
		analysis.runes ? b.empty : b.stmt(b.call('$.init')),
		.../** @type {import('estree').Statement[]} */ (template.body),
		...static_bindings
	]);
 
	const append_styles =
		analysis.inject_styles && analysis.css.ast
			? () =>
					component_block.body.push(
						b.stmt(
							b.call(
								'$.append_styles',
								b.id('$$anchor'),
								b.literal(analysis.css.hash),
								b.literal(render_stylesheet(source, analysis, options).code)
							)
						)
					)
			: () => {};
 
	append_styles();
	component_block.body.push(
		component_returned_object.length > 0
			? b.return(b.call('$.pop', b.object(component_returned_object)))
			: b.stmt(b.call('$.pop'))
	);
 
	if (analysis.uses_rest_props) {
		const named_props = analysis.exports.map(({ name, alias }) => alias ?? name);
		for (const [name, binding] of analysis.instance.scope.declarations) {
			if (binding.kind === 'bindable_prop') named_props.push(binding.prop_alias ?? name);
		}
 
		component_block.body.unshift(
			b.const(
				'$$restProps',
				b.call(
					'$.rest_props',
					b.id('$$sanitized_props'),
					b.array(named_props.map((name) => b.literal(name)))
				)
			)
		);
	}
 
	if (analysis.uses_props || analysis.uses_rest_props) {
		const to_remove = [b.literal('children'), b.literal('$$slots'), b.literal('$$events')];
		if (analysis.custom_element) {
			to_remove.push(b.literal('$$host'));
		}
		component_block.body.unshift(
			b.const('$$sanitized_props', b.call('$.rest_props', b.id('$$props'), b.array(to_remove)))
		);
	}
 
	if (analysis.uses_slots) {
		component_block.body.unshift(b.const('$$slots', b.call('$.sanitize_slots', b.id('$$props'))));
	}
 
	const body = [
		...state.hoisted,
		...module.body,
		b.function_declaration(
			b.id(analysis.name),
			[b.id('$$anchor'), b.id('$$props')],
			component_block
		)
	];
 
	if (options.hmr) {
		body.push(
			b.if(
				b.id('import.meta.hot'),
				b.block([
					b.const(b.id('s'), b.call('$.source', b.id(analysis.name))),
					b.stmt(b.assignment('=', b.id(analysis.name), b.call('$.hmr', b.id('s')))),
					b.stmt(
						b.call(
							'import.meta.hot.accept',
							b.arrow(
								[b.id('module')],
								b.block([
									b.stmt(b.call('$.set', b.id('s'), b.member(b.id('module'), b.id('default'))))
								])
							)
						)
					)
				])
			)
		);
	}
 
	body.push(b.export_default(b.id(analysis.name)));
 
	if (options.dev) {
		if (options.filename) {
			let filename = options.filename;
			if (/(\/|\w:)/.test(options.filename)) {
				// filename is absolute — truncate it
				const parts = filename.split(/[/\\]/);
				filename = parts.length > 3 ? ['...', ...parts.slice(-3)].join('/') : filename;
			}
 
			// add `App.filename = 'App.svelte'` so that we can print useful messages later
			body.push(
				b.stmt(
					b.assignment('=', b.member(b.id(analysis.name), b.id('filename')), b.literal(filename))
				)
			);
		}
 
		body.unshift(b.stmt(b.call(b.id('$.mark_module_start'))));
		body.push(b.stmt(b.call(b.id('$.mark_module_end'), b.id(analysis.name))));
	}
 
	if (options.discloseVersion) {
		body.unshift(b.imports([], 'svelte/internal/disclose-version'));
	}
 
	if (options.legacy.componentApi) {
		body.unshift(b.imports([['createClassComponent', '$$_createClassComponent']], 'svelte/legacy'));
		component_block.body.unshift(
			b.if(
				b.binary('===', b.id('new.target'), b.id(analysis.name)),
				b.return(
					b.call(
						'$$_createClassComponent',
						// When called with new, the first argument is the constructor options
						b.object([b.init('component', b.id(analysis.name)), b.spread(b.id('$$anchor'))])
					)
				)
			)
		);
	} else if (options.dev) {
		component_block.body.unshift(
			b.if(
				b.binary('===', b.id('new.target'), b.id(analysis.name)),
				b.throw_error(
					`Instantiating a component with \`new\` is no longer valid in Svelte 5. ` +
						'See https://svelte-5-preview.vercel.app/docs/breaking-changes#components-are-no-longer-classes for more information'
				)
			)
		);
	}
 
	if (state.events.size > 0) {
		body.push(
			b.stmt(b.call('$.delegate', b.array(Array.from(state.events).map((name) => b.literal(name)))))
		);
	}
 
	if (analysis.custom_element) {
		const ce = analysis.custom_element;

		/** @type {import('estree').Property[]} */
		const props_str = [];

		for (const [name, binding] of properties) {
			const key = binding.prop_alias ?? name;
			const prop_def = typeof ce === 'boolean' ? {} : ce.props?.[key] || {};
			if (
				!prop_def.type &&
				binding.initial?.type === 'Literal' &&
				typeof binding.initial.value === 'boolean'
			) {
				prop_def.type = 'Boolean';
			}

			const value = b.object(
				/** @type {import('estree').Property[]} */ (
					[
						prop_def.attribute ? b.init('attribute', b.literal(prop_def.attribute)) : undefined,
						prop_def.reflect ? b.init('reflect', b.literal(true)) : undefined,
						prop_def.type ? b.init('type', b.literal(prop_def.type)) : undefined
					].filter(Boolean)
				)
			);
			props_str.push(b.init(key, value));
		}

		const slots_str = b.array([...analysis.slot_names.keys()].map((name) => b.literal(name)));
		const accessors_str = b.array(
			analysis.exports.map(({ name, alias }) => b.literal(alias ?? name))
		);
		const use_shadow_dom = typeof ce === 'boolean' || ce.shadow !== 'none' ? true : false;

		const create_ce = b.call(
			'$.create_custom_element',
			b.id(analysis.name),
			b.object(props_str),
			slots_str,
			accessors_str,
			b.literal(use_shadow_dom),
			/** @type {any} */ (typeof ce !== 'boolean' ? ce.extend : undefined)
		);

		// If customElement option is set, we define the custom element directly. Else we still create
		// the custom element class so that the user may instantiate a custom element themselves later.
		if (typeof ce !== 'boolean') {
			body.push(b.stmt(b.call('customElements.define', b.literal(ce.tag), create_ce)));
		} else {
			body.push(b.stmt(create_ce));
		}
	}
 
	return {
		type: 'Program',
		sourceType: 'module',
		body
	};
}
 
/**
 * @param {import('../../types').Analysis} analysis
 * @param {import('#compiler').ValidatedModuleCompileOptions} options
 * @returns {import('estree').Program}
 */
export function client_module(analysis, options) {
	/** @type {import('./types').ClientTransformState} */
	const state = {
		analysis,
		options,
		scope: analysis.module.scope,
		scopes: analysis.module.scopes,
		legacy_reactive_statements: new Map(),
		public_state: new Map(),
		private_state: new Map(),
		in_constructor: false
	};
 
	const module = /** @type {import('estree').Program} */ (
		walk(
			/** @type {import('#compiler').SvelteNode} */ (analysis.module.ast),
			state,
			combine_visitors(
				set_scope(analysis.module.scopes),
				global_visitors,
				// @ts-expect-error
				javascript_visitors,
				javascript_visitors_runes
			)
		)
	);
 
	return {
		type: 'Program',
		sourceType: 'module',
		body: [b.import_all('$', 'svelte/internal/client'), ...module.body]
	};
}