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

98.7% Statements 152/154
94.91% Branches 56/59
100% Functions 4/4
98.67% Lines 149/151

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 1522x 2x 2x 2x 2x 2x 2x 16860x 12904x 25x 25x 12879x 12879x 12879x 12879x 12904x 12904x 12904x 2275x 12904x 21x 21x 21x 21x 21x 21x 6x 21x 6x 6x 21x 12873x 12873x 12873x 2x 2x 2882x 2882x 24x 24x 22x 22x 2882x 64x 64x 62x 62x 62x 13x 13x 62x 64x 2847x 2x 2x 1126x 2x 2x 180x 180x 180x 180x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 27x 138x 122x 122x 122x 122x 122x 122x 122x 11x 11x 122x 111x 111x 111x 122x 122x 7x 7x 122x 122x 122x 16x 16x 179x 42x 42x 42x 1x 42x 1x 1x 1x 1x 1x 1x     1x 1x 42x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 26x 26x 41x 12x 15x 3x 3x 3x 1x 3x 2x 2x 2x 2x 2x 2x 2x 3x 3x 41x 180x 2x  
import is_reference from 'is-reference';
import { serialize_get_binding, serialize_set_binding } from '../utils.js';
import * as b from '../../../../utils/builders.js';
 
/** @type {import('../types').Visitors} */
export const global_visitors = {
	Identifier(node, { path, state }) {
		if (is_reference(node, /** @type {import('estree').Node} */ (path.at(-1)))) {
			if (node.name === '$$props') {
				return b.id('$$sanitized_props');
			}
 
			// Optimize prop access: If it's a member read access, we can use the $$props object directly
			const binding = state.scope.get(node.name);
			if (
				state.analysis.runes && // can't do this in legacy mode because the proxy does more than just read/write
				binding !== null &&
				node !== binding.node &&
				binding.kind === 'rest_prop'
			) {
				const parent = path.at(-1);
				const grand_parent = path.at(-2);
				if (
					parent?.type === 'MemberExpression' &&
					!parent.computed &&
					grand_parent?.type !== 'AssignmentExpression' &&
					grand_parent?.type !== 'UpdateExpression'
				) {
					return b.id('$$props');
				}
			}
 
			return serialize_get_binding(node, state);
		}
	},
	MemberExpression(node, { state, next }) {
		// rewrite `this.#foo` as `this.#foo.v` inside a constructor
		if (node.property.type === 'PrivateIdentifier') {
			const field = state.private_state.get(node.property.name);
			if (field) {
				return state.in_constructor ? b.member(node, b.id('v')) : b.call('$.get', node);
			}
		} else if (node.object.type === 'ThisExpression') {
			// rewrite `this.foo` as `this.#foo.v` inside a constructor
			if (node.property.type === 'Identifier' && !node.computed) {
				const field = state.public_state.get(node.property.name);
 
				if (field && state.in_constructor) {
					return b.member(b.member(b.this, field.id), b.id('v'));
				}
			}
		}
		next();
	},
	AssignmentExpression(node, context) {
		return serialize_set_binding(node, context, context.next);
	},
	UpdateExpression(node, context) {
		const { state, next, visit } = context;
		const argument = node.argument;
 
		if (argument.type === 'Identifier') {
			const binding = state.scope.get(argument.name);
			const is_store = binding?.kind === 'store_sub';
			const name = is_store ? argument.name.slice(1) : argument.name;
 
			// use runtime functions for smaller output
			if (
				binding?.kind === 'state' ||
				binding?.kind === 'frozen_state' ||
				binding?.kind === 'each' ||
				binding?.kind === 'legacy_reactive' ||
				binding?.kind === 'prop' ||
				binding?.kind === 'bindable_prop' ||
				is_store
			) {
				/** @type {import('estree').Expression[]} */
				const args = [];
 
				let fn = '$.update';
				if (node.prefix) fn += '_pre';
 
				if (is_store) {
					fn += '_store';
					args.push(serialize_get_binding(b.id(name), state), b.call('$' + name));
				} else {
					if (binding.kind === 'prop' || binding.kind === 'bindable_prop') fn += '_prop';
					args.push(b.id(name));
				}
 
				if (node.operator === '--') {
					args.push(b.literal(-1));
				}
 
				return b.call(fn, ...args);
			}
 
			return next();
		} else if (
			argument.type === 'MemberExpression' &&
			argument.object.type === 'ThisExpression' &&
			argument.property.type === 'PrivateIdentifier' &&
			context.state.private_state.has(argument.property.name)
		) {
			let fn = '$.update';
			if (node.prefix) fn += '_pre';
 
			/** @type {import('estree').Expression[]} */
			const args = [argument];
			if (node.operator === '--') {
				args.push(b.literal(-1));
			}
 
			return b.call(fn, ...args);
		} else {
			// turn it into an IIFEE assignment expression: i++ -> (() => { const $$value = i; i+=1; return $$value; })
			const assignment = b.assignment(
				node.operator === '++' ? '+=' : '-=',
				/** @type {import('estree').Pattern} */ (argument),
				b.literal(1)
			);
			const serialized_assignment = serialize_set_binding(
				assignment,
				context,
				() => assignment,
				node.prefix
			);
			const value = /** @type {import('estree').Expression} */ (visit(argument));
			if (serialized_assignment === assignment) {
				// No change to output -> nothing to transform -> we can keep the original update expression
				return next();
			} else if (context.state.analysis.runes) {
				return serialized_assignment;
			} else {
				/** @type {import('estree').Statement[]} */
				let statements;
				if (node.prefix) {
					statements = [b.stmt(serialized_assignment), b.return(value)];
				} else {
					const tmp_id = state.scope.generate('$$value');
					statements = [
						b.const(tmp_id, value),
						b.stmt(serialized_assignment),
						b.return(b.id(tmp_id))
					];
				}
				return b.call(b.thunk(b.block(statements)));
			}
		}
	}
};