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

100% Statements 128/128
97.43% Branches 38/39
100% Functions 4/4
100% Lines 124/124

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 1252x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3869x 3869x 3869x 3869x 3869x 3869x 3869x 3869x 3869x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 1x 33x 23x 33x 10x 10x 34x 3869x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3869x 3869x 3869x 3869x 3869x 3869x 3869x 24x 24x 24x 24x 24x 24x 2x 23x 18x 21x 4x 4x 24x 3869x 2x 2x 2x 2x 2x 2x 1023x 8x 8x 1015x 1023x 965x 965x 965x 248x 248x 717x 717x 717x 717x 717x 717x 717x 50x 50x 50x 2x 2x 2x 2x 2x 2x 2x 1087x 979x 979x 108x 108x 108x  
/** @import { Expression, Identifier } from 'estree' */
/** @import { Attribute, ClassDirective, ExpressionMetadata, Namespace, RegularElement, StyleDirective, SvelteElement } from '#compiler' */
/** @import { ComponentContext } from '../../types' */
import { normalize_attribute } from '../../../../../../utils.js';
import * as b from '../../../../../utils/builders.js';
import { build_getter } from '../../utils.js';
import { build_template_literal, build_update } from './utils.js';
 
/**
 * 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 {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) {
		let value =
			directive.value === true
				? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
				: build_attribute_value(directive.value, context).value;
 
		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
			)
		);
 
		const { has_state, has_call } = directive.metadata.expression;
 
		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 {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 value = /** @type {Expression} */ (context.visit(directive.expression));
		const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));
 
		const { has_state, has_call } = directive.metadata.expression;
 
		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 {Attribute['value']} value
 * @param {ComponentContext} context
 */
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 {RegularElement | SvelteElement} element
 * @param {Attribute} attribute
 * @param {{ state: { metadata: { namespace: Namespace }}}} context
 */
export function get_attribute_name(element, attribute, context) {
	if (!element.metadata.svg && !element.metadata.mathml) {
		return normalize_attribute(attribute.name);
	}
 
	return attribute.name;
}