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

96.82% Statements 122/126
96.96% Branches 32/33
92.3% Functions 12/13
96.74% Lines 119/123

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 1242x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4779x 3672x 3672x 3672x 3672x 3672x 7723x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7723x 3672x 4779x 4779x 9071x 143x 143x 143x 143x 143x 143x 28x 28x 28x 28x 28x 28x 28x 28x 28x 66x 38x 38x 38x 38x 38x 38x 28x 28x 66x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 143x 143x 11x 11x 11x 11x 11x 11x 11x 143x 143x 9071x 9071x 2125x 2027x 2027x 2027x 2027x 77x 70x 70x 70x 7x 7x 2027x 2027x 14x 14x 14x 14x 14x 14x 2027x 2125x 98x 98x 98x 98x 98x 98x         2125x 9071x 4779x 4779x 4779x 4779x 4779x  
/** @import { Expression, MemberExpression, Program } from 'estree' */
/** @import { ComponentContext } from '../types' */
import { build_getter, is_prop_source } from '../utils.js';
import * as b from '../../../../utils/builders.js';
import { add_state_transformers } from './shared/declarations.js';
 
/**
 * @param {Program} _
 * @param {ComponentContext} context
 */
export function Program(_, context) {
	if (!context.state.analysis.runes) {
		context.state.transform['$$props'] = {
			read: (node) => ({ ...node, name: '$$sanitized_props' })
		};
 
		for (const [name, binding] of context.state.scope.declarations) {
			if (binding.declaration_kind === 'import' && binding.mutated) {
				const id = b.id('$$_import_' + name);
 
				context.state.transform[name] = {
					read: (_) => b.call(id),
					mutate: (_, mutation) => b.call(id, mutation)
				};
 
				context.state.legacy_reactive_imports.push(
					b.var(id, b.call('$.reactive_import', b.thunk(b.id(name))))
				);
			}
		}
	}
 
	for (const [name, binding] of context.state.scope.declarations) {
		if (binding.kind === 'store_sub') {
			const store = /** @type {Expression} */ (context.visit(b.id(name.slice(1))));
 
			context.state.transform[name] = {
				read: b.call,
				assign: (_, value) => b.call('$.store_set', store, value),
				mutate: (node, mutation) => {
					// We need to untrack the store read, for consistency with Svelte 4
					const untracked = b.call('$.untrack', node);
 
					/**
					 *
					 * @param {Expression} n
					 * @returns {Expression}
					 */
					function replace(n) {
						if (n.type === 'MemberExpression') {
							return {
								...n,
								object: replace(/** @type {Expression} */ (n.object)),
								property: n.property
							};
						}
 
						return untracked;
					}
 
					return b.call(
						'$.store_mutate',
						store,
						b.assignment(
							mutation.operator,
							/** @type {MemberExpression} */ (
								replace(/** @type {MemberExpression} */ (mutation.left))
							),
							mutation.right
						),
						untracked
					);
				},
				update: (node) => {
					return b.call(
						node.prefix ? '$.update_pre_store' : '$.update_store',
						build_getter(b.id(name.slice(1)), context.state),
						b.call(node.argument),
						node.operator === '--' && b.literal(-1)
					);
				}
			};
		}
 
		if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
			if (is_prop_source(binding, context.state)) {
				context.state.transform[name] = {
					read: b.call,
					assign: (node, value) => b.call(node, value),
					mutate: (node, value) => {
						if (binding.kind === 'bindable_prop') {
							// only necessary for interop with legacy parent bindings
							return b.call(node, value, b.true);
						}
 
						return value;
					},
					update: (node) => {
						return b.call(
							node.prefix ? '$.update_pre_prop' : '$.update_prop',
							node.argument,
							node.operator === '--' && b.literal(-1)
						);
					}
				};
			} else if (binding.prop_alias) {
				const key = b.key(binding.prop_alias);
 
				context.state.transform[name] = {
					read: (_) => b.member(b.id('$$props'), key, key.type === 'Literal')
				};
			} else {
				context.state.transform[name] = {
					read: (node) => b.member(b.id('$$props'), node)
				};
			}
		}
	}
 
	add_state_transformers(context);
 
	context.next();
}