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

97.57% Statements 201/206
94.44% Branches 68/72
100% Functions 3/3
97.52% Lines 197/202

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 2032x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1735x 1735x 1735x 1735x 1735x 1735x 1683x 1735x 52x 52x 52x 52x 52x 52x 52x 99x 99x 99x 99x 99x 99x 52x 52x 52x       52x 52x 52x 52x 10x 10x 10x 52x 52x 46x 46x 46x 46x 46x 46x 46x 46x 46x 6x 6x 6x 1683x 1735x     1683x 1683x 1735x 36x 1735x 1735x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1782x 1782x 1782x 1782x 214x 1782x 32x 14x 14x 14x 12x 12x 12x 12x 8x 8x 8x 8x 8x 8x 12x 12x 5x 4x 4x 12x 7x 7x 12x 31x 11x 11x 11x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 11x 32x 1762x 1762x 1762x 1782x 566x 566x 566x 1762x 1782x 31x 31x 1731x 1731x 1782x 1726x 1726x 1782x 1782x 1782x 1782x 1782x 1148x 1148x 1148x 1148x 1148x 1148x 1148x 1148x 1148x 1148x 302x 1148x 134x 134x 134x 134x 134x 1148x 1148x 1148x 578x 578x 578x 578x 578x 578x 578x 578x 578x 1782x 241x 241x 578x 578x 1779x 1782x 1782x 2x 2x 2x 2x 2x 2x 2x 2x 1160x 1160x 1160x 1160x 108x 108x 108x 108x 1160x 1160x  
/** @import { AssignmentExpression, AssignmentOperator, BinaryOperator, Expression, Node, Pattern } from 'estree' */
/** @import { SvelteNode } from '#compiler' */
/** @import { ClientTransformState, Context } from '../types.js' */
import * as b from '../../../../utils/builders.js';
import { extract_paths, is_expression_async } from '../../../../utils/ast.js';
import { is_ignored } from '../../../../state.js';
import { build_proxy_reassignment, should_proxy_or_freeze } from '../utils.js';
 
/**
 * @param {AssignmentExpression} node
 * @param {Context} context
 */
export function AssignmentExpression(node, context) {
	const parent = /** @type {Node} */ (context.path.at(-1));
	const is_standalone = parent.type.endsWith('Statement');
 
	if (
		node.left.type === 'ArrayPattern' ||
		node.left.type === 'ObjectPattern' ||
		node.left.type === 'RestElement'
	) {
		const value = /** @type {Expression} */ (context.visit(node.right));
		const should_cache = value.type !== 'Identifier';
		const rhs = should_cache ? b.id('$$value') : value;
 
		let changed = false;
 
		const assignments = extract_paths(node.left).map((path) => {
			const value = path.expression?.(rhs);
 
			let assignment = build_assignment('=', path.node, value, context);
			if (assignment !== null) changed = true;
 
			return assignment ?? /** @type {Expression} */ (context.next());
		});
 
		if (!changed) {
			// No change to output -> nothing to transform -> we can keep the original assignment
			return context.next();
		}
 
		const sequence = b.sequence(assignments);
 
		if (!is_standalone) {
			// this is part of an expression, we need the sequence to end with the value
			sequence.expressions.push(rhs);
		}
 
		if (should_cache) {
			// the right hand side is a complex expression, wrap in an IIFE to cache it
			const iife = b.arrow([rhs], sequence);
 
			const iife_is_async =
				is_expression_async(value) ||
				assignments.some((assignment) => is_expression_async(assignment));
 
			return iife_is_async ? b.await(b.call(b.async(iife), value)) : b.call(iife, value);
		}
 
		return sequence;
	}
 
	if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') {
		throw new Error(`Unexpected assignment type ${node.left.type}`);
	}
 
	return (
		build_assignment(node.operator, node.left, node.right, context) ??
		/** @type {Expression} */ (context.next())
	);
}
 
/**
 * @template {ClientTransformState} State
 * @param {AssignmentOperator} operator
 * @param {Pattern} left
 * @param {Expression} right
 * @param {import('zimmerframe').Context<SvelteNode, State>} context
 * @returns {Expression | null}
 */
export function build_assignment(operator, left, right, context) {
	// Handle class private/public state assignment cases
	if (
		context.state.analysis.runes &&
		left.type === 'MemberExpression' &&
		left.object.type === 'ThisExpression'
	) {
		if (left.property.type === 'PrivateIdentifier') {
			const private_state = context.state.private_state.get(left.property.name);
 
			if (private_state !== undefined) {
				let value = get_assignment_value(operator, left, right, context);
				let transformed = false;
 
				if (should_proxy_or_freeze(value, context.state.scope)) {
					transformed = true;
					value =
						private_state.kind === 'frozen_state'
							? b.call('$.freeze', value)
							: build_proxy_reassignment(value, private_state.id);
				}
 
				if (context.state.in_constructor) {
					if (transformed) {
						return b.assignment(operator, /** @type {Pattern} */ (context.visit(left)), value);
					}
				} else {
					return b.call('$.set', left, value);
				}
			}
		} else if (left.property.type === 'Identifier' && context.state.in_constructor) {
			const public_state = context.state.public_state.get(left.property.name);
 
			if (public_state !== undefined && should_proxy_or_freeze(right, context.state.scope)) {
				const value = /** @type {Expression} */ (context.visit(right));
 
				return b.assignment(
					operator,
					/** @type {Pattern} */ (context.visit(left)),
					public_state.kind === 'frozen_state'
						? b.call('$.freeze', value)
						: build_proxy_reassignment(value, public_state.id)
				);
			}
		}
	}
 
	let object = left;
 
	while (object.type === 'MemberExpression') {
		// @ts-expect-error
		object = object.object;
	}
 
	if (object.type !== 'Identifier') {
		return null;
	}
 
	const binding = context.state.scope.get(object.name);
	if (!binding) return null;
 
	const transform = Object.hasOwn(context.state.transform, object.name)
		? context.state.transform[object.name]
		: null;
 
	// reassignment
	if (object === left && transform?.assign) {
		let value = get_assignment_value(operator, left, right, context);
 
		// special case — if an element binding, we know it's a primitive
		const path = context.path.map((node) => node.type);
		const is_primitive = path.at(-1) === 'BindDirective' && path.at(-2) === 'RegularElement';
 
		if (
			!is_primitive &&
			binding.kind !== 'prop' &&
			context.state.analysis.runes &&
			should_proxy_or_freeze(value, context.state.scope)
		) {
			value =
				binding.kind === 'frozen_state'
					? b.call('$.freeze', value)
					: build_proxy_reassignment(value, object.name);
		}
 
		return transform.assign(object, value);
	}
 
	/** @type {Expression} */
	let mutation = b.assignment(
		operator,
		/** @type {Pattern} */ (context.visit(left)),
		/** @type {Expression} */ (context.visit(right))
	);
 
	// mutation
	if (transform?.mutate) {
		mutation = transform.mutate(object, mutation);
	}
 
	return is_ignored(left, 'ownership_invalid_mutation')
		? b.call('$.skip_ownership_validation', b.thunk(mutation))
		: mutation;
}
 
/**
 * @template {ClientTransformState} State
 * @param {AssignmentOperator} operator
 * @param {Pattern} left
 * @param {Expression} right
 * @param {import('zimmerframe').Context<SvelteNode, State>} context
 */
function get_assignment_value(operator, left, right, { visit }) {
	return operator === '='
		? /** @type {Expression} */ (visit(right))
		: // turn something like x += 1 into x = x + 1
			b.binary(
				/** @type {BinaryOperator} */ (operator.slice(0, -1)),
				/** @type {Expression} */ (visit(left)),
				/** @type {Expression} */ (visit(right))
			);
}