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

97.05% Statements 132/136
97.05% Branches 33/34
92.3% Functions 12/13
96.99% Lines 129/133

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 1342x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4789x 3680x 3680x 3680x 3680x 3680x 7740x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 9x 7740x 3680x 4789x 4789x 9091x 144x 144x 144x 144x 144x 144x 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 144x 144x 11x 11x 11x 11x 11x 11x 11x 144x 144x 9091x 9091x 2128x 2028x 2028x 2028x 2028x 77x 70x 70x 70x 7x 7x 2028x 2028x 14x 14x 14x 14x 14x 14x 2028x 2128x 100x 100x 100x 100x 100x 100x         2128x 9091x 4789x 4789x 4789x 4789x 4789x  
/** @import { Expression, ImportDeclaration, 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) {
				// the declaration itself is hoisted to the module scope, so we need
				// to resort to cruder measures to differentiate instance/module imports
				const { start, end } = context.state.analysis.instance.ast;
				const node = /** @type {ImportDeclaration} */ (binding.initial);
				const is_instance_import =
					/** @type {number} */ (node.start) > /** @type {number} */ (start) &&
					/** @type {number} */ (node.end) < /** @type {number} */ (end);
 
				if (is_instance_import) {
					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();
}