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

100% Statements 226/226
94.91% Branches 56/59
100% Functions 5/5
100% Lines 220/220

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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 2212x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 185x 75x 75x 75x 75x 7x 75x 7x 7x 7x 7x 75x 68x 68x 75x 75x 183x 110x 110x 110x 110x 110x 110x 110x 185x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 122x 111x 111x 111x 111x 111x 10x 10x 10x 101x 101x 101x 101x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3962x 3962x 3962x 3962x 3962x 3962x 3962x 3962x 3962x 36x 36x 36x 36x 36x 36x 36x 36x 3x 3x 3x 3x 3x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 2x 34x 23x 33x 11x 11x 36x 3962x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3962x 3962x 3962x 3962x 3962x 3962x 3962x 27x 27x 27x 27x 4x 4x 4x 4x 4x 27x 27x 27x 27x 3x 27x 18x 22x 6x 6x 27x 3962x 2x 2x 2x 2x 2x 2x 2x 1039x 8x 8x 1031x 1039x 981x 981x 981x 251x 251x 730x 730x 730x 730x 730x 730x 730x 50x 50x 50x 2x 2x 2x 2x 2x 2x 1049x 942x 942x 107x 107x 107x  
/** @import { Expression, Identifier, ObjectExpression } from 'estree' */
/** @import { AST, Namespace } from '#compiler' */
/** @import { ComponentContext } from '../../types' */
import { normalize_attribute } from '../../../../../../utils.js';
import { is_ignored } from '../../../../../state.js';
import { get_attribute_expression, is_event_attribute } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';
import { build_getter, create_derived } from '../../utils.js';
import { build_template_literal, build_update } from './utils.js';
 
/**
 * @param {Array<AST.Attribute | AST.SpreadAttribute>} attributes
 * @param {ComponentContext} context
 * @param {AST.RegularElement | AST.SvelteElement} element
 * @param {Identifier} element_id
 * @param {Identifier} attributes_id
 * @param {false | Expression} preserve_attribute_case
 * @param {false | Expression} is_custom_element
 */
export function build_set_attributes(
	attributes,
	context,
	element,
	element_id,
	attributes_id,
	preserve_attribute_case,
	is_custom_element
) {
	let needs_isolation = false;
	let has_state = false;
 
	/** @type {ObjectExpression['properties']} */
	const values = [];
 
	for (const attribute of attributes) {
		if (attribute.type === 'Attribute') {
			const { value } = build_attribute_value(attribute.value, context);
 
			if (
				is_event_attribute(attribute) &&
				(value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression')
			) {
				// Give the event handler a stable ID so it isn't removed and readded on every update
				const id = context.state.scope.generate('event_handler');
				context.state.init.push(b.var(id, value));
				values.push(b.init(attribute.name, b.id(id)));
			} else {
				values.push(b.init(attribute.name, value));
			}
 
			has_state ||= attribute.metadata.expression.has_state;
		} else {
			values.push(b.spread(/** @type {Expression} */ (context.visit(attribute))));
 
			// objects could contain reactive getters -> play it safe and always assume spread attributes are reactive
			has_state = true;
 
			needs_isolation ||= attribute.metadata.expression.has_call;
		}
	}
 
	const call = b.call(
		'$.set_attributes',
		element_id,
		has_state ? attributes_id : b.literal(null),
		b.object(values),
		context.state.analysis.css.hash !== '' && b.literal(context.state.analysis.css.hash),
		preserve_attribute_case,
		is_custom_element,
		is_ignored(element, 'hydration_attribute_changed') && b.true
	);
 
	if (has_state) {
		context.state.init.push(b.let(attributes_id));
 
		const update = b.stmt(b.assignment('=', attributes_id, call));
 
		if (needs_isolation) {
			context.state.init.push(build_update(update));
			return false;
		}
 
		context.state.update.push(update);
		return true;
	}
 
	context.state.init.push(b.stmt(call));
	return false;
}
 
/**
 * Serializes each style directive into something like `$.set_style(element, style_property, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {AST.StyleDirective[]} style_directives
 * @param {Identifier} element_id
 * @param {ComponentContext} context
 * @param {boolean} is_attributes_reactive
 * @param {boolean} force_check Should be `true` if we can't rely on our cached value, because for example there's also a `style` attribute
 */
export function build_style_directives(
	style_directives,
	element_id,
	context,
	is_attributes_reactive,
	force_check
) {
	const state = context.state;
 
	for (const directive of style_directives) {
		const { has_state, has_call } = directive.metadata.expression;
 
		let value =
			directive.value === true
				? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
				: build_attribute_value(directive.value, context).value;
 
		if (has_call) {
			const id = b.id(state.scope.generate('style_directive'));
 
			state.init.push(b.const(id, create_derived(state, b.thunk(value))));
			value = b.call('$.get', id);
		}
 
		const update = b.stmt(
			b.call(
				'$.set_style',
				element_id,
				b.literal(directive.name),
				value,
				/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined),
				force_check ? b.true : undefined
			)
		);
 
		if (!is_attributes_reactive && has_call) {
			state.init.push(build_update(update));
		} else if (is_attributes_reactive || has_state || has_call) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * Serializes each class directive into something like `$.class_toogle(element, class_name, value)`
 * and adds it either to init or update, depending on whether or not the value or the attributes are dynamic.
 * @param {AST.ClassDirective[]} class_directives
 * @param {Identifier} element_id
 * @param {ComponentContext} context
 * @param {boolean} is_attributes_reactive
 */
export function build_class_directives(
	class_directives,
	element_id,
	context,
	is_attributes_reactive
) {
	const state = context.state;
	for (const directive of class_directives) {
		const { has_state, has_call } = directive.metadata.expression;
		let value = /** @type {Expression} */ (context.visit(directive.expression));
 
		if (has_call) {
			const id = b.id(state.scope.generate('class_directive'));
 
			state.init.push(b.const(id, create_derived(state, b.thunk(value))));
			value = b.call('$.get', id);
		}
 
		const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));
 
		if (!is_attributes_reactive && has_call) {
			state.init.push(build_update(update));
		} else if (is_attributes_reactive || has_state || has_call) {
			state.update.push(update);
		} else {
			state.init.push(update);
		}
	}
}
 
/**
 * @param {AST.Attribute['value']} value
 * @param {ComponentContext} context
 * @returns {{ value: Expression, has_state: boolean, has_call: boolean }}
 */
export function build_attribute_value(value, context) {
	if (value === true) {
		return { has_state: false, has_call: false, value: b.literal(true) };
	}
 
	if (!Array.isArray(value) || value.length === 1) {
		const chunk = Array.isArray(value) ? value[0] : value;
 
		if (chunk.type === 'Text') {
			return { has_state: false, has_call: false, value: b.literal(chunk.data) };
		}
 
		return {
			has_state: chunk.metadata.expression.has_state,
			has_call: chunk.metadata.expression.has_call,
			value: /** @type {Expression} */ (context.visit(chunk.expression))
		};
	}
 
	return build_template_literal(value, context.visit, context.state);
}
 
/**
 * @param {AST.RegularElement | AST.SvelteElement} element
 * @param {AST.Attribute} attribute
 */
export function get_attribute_name(element, attribute) {
	if (!element.metadata.svg && !element.metadata.mathml) {
		return normalize_attribute(attribute.name);
	}
 
	return attribute.name;
}