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

98.23% Statements 111/113
92.68% Branches 38/41
100% Functions 2/2
98.18% Lines 108/110

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 1112x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 186x 186x 186x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 27x 140x 124x 124x 124x 124x 124x 124x 124x 11x 11x 124x 113x 113x 113x 124x 124x 7x 7x 124x 124x 124x 16x 16x 16x 46x 46x 46x 186x 186x 1x 186x 1x 1x 1x 1x 1x 1x     1x 1x 1x 45x 45x 45x 45x 4x 4x 41x 41x 41x 45x 45x 45x 186x 186x 186x 186x 186x 186x 186x 186x 186x 186x 27x 27x 27x 18x 62x 15x 15x 3x 3x 3x 47x 1x 47x 2x 2x 2x 3x 3x 3x  
/** @import { Expression, Pattern, Statement, UpdateExpression } from 'estree' */
/** @import { Context } from '../types' */
import { is_ignored } from '../../../../state.js';
import * as b from '../../../../utils/builders.js';
import { build_getter, build_setter } from '../utils.js';
 
/**
 * @param {UpdateExpression} node
 * @param {Context} context
 */
export function UpdateExpression(node, context) {
	const argument = node.argument;
 
	if (argument.type === 'Identifier') {
		const binding = context.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 {Expression[]} */
			const args = [];
 
			let fn = '$.update';
			if (node.prefix) fn += '_pre';
 
			if (is_store) {
				fn += '_store';
				args.push(build_getter(b.id(name), context.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 context.next();
	}
 
	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 {Expression[]} */
		const args = [argument];
		if (node.operator === '--') {
			args.push(b.literal(-1));
		}
 
		return b.call(fn, ...args);
	}
 
	/** @param {any} serialized */
	function maybe_skip_ownership_validation(serialized) {
		if (is_ignored(node, 'ownership_invalid_mutation')) {
			return b.call('$.skip_ownership_validation', b.thunk(serialized));
		}
 
		return serialized;
	}
 
	// turn it into an IIFE assignment expression: i++ -> (() => { const $$value = i; i+=1; return $$value; })
	const assignment = b.assignment(
		node.operator === '++' ? '+=' : '-=',
		/** @type {Pattern} */ (argument),
		b.literal(1)
	);
 
	const serialized_assignment = build_setter(assignment, context, () => assignment, node.prefix);
 
	const value = /** @type {Expression} */ (context.visit(argument));
 
	if (serialized_assignment === assignment) {
		// No change to output -> nothing to transform -> we can keep the original update expression
		return maybe_skip_ownership_validation(context.next());
	}
 
	if (context.state.analysis.runes) {
		return maybe_skip_ownership_validation(serialized_assignment);
	}
 
	/** @type {Statement[]} */
	let statements;
	if (node.prefix) {
		statements = [b.stmt(serialized_assignment), b.return(value)];
	} else {
		const tmp_id = context.state.scope.generate('$$value');
		statements = [b.const(tmp_id, value), b.stmt(serialized_assignment), b.return(b.id(tmp_id))];
	}
 
	return maybe_skip_ownership_validation(b.call(b.thunk(b.block(statements))));
}