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

87.39% Statements 215/246
64.28% Branches 45/70
100% Functions 1/1
87.08% Lines 209/240

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 2412x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 312x 312x 312x 312x 312x 312x 312x 312x 312x 8x 2x 2x 4x 312x 7x 312x 6x 6x 6x 6x 6x 6x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 312x 6x 6x 6x 6x 6x 6x 6x 6x 312x 306x 306x 306x 306x     306x 306x 306x 3x 3x 3x 3x 3x 3x 3x 306x 306x 306x 306x 306x 4x 4x 306x 306x 306x 1x 1x 306x 306x 306x     306x     306x     306x     306x     306x     306x     306x     306x     306x     306x     306x 306x 306x 306x 306x 306x     306x 306x 306x 306x 306x     306x 306x 306x 134x 33x 134x 101x 101x 134x 134x 306x 306x     306x 306x 74x 74x 306x 306x 306x 306x 7x 7x 7x 7x 7x 7x 7x 7x 306x 306x 306x 19x 19x 306x 306x 2x 2x 306x 306x 62x 18x 18x 18x 18x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 204x 204x 204x 31x 62x 62x 62x 62x 62x 31x 31x 31x 31x 31x 31x 31x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 306x 306x   306x 306x 312x 312x 312x 312x 74x 310x 238x 238x 238x 238x 238x 238x 238x 312x  
/** @import { CallExpression, Expression, MemberExpression } from 'estree' */
/** @import { Attribute, BindDirective } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import { dev, is_ignored } from '../../../../state.js';
import { is_text_attribute } from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js';
import { binding_properties } from '../../../bindings.js';
import { build_attribute_value } from './shared/element.js';
import { build_bind_this, validate_binding } from './shared/utils.js';
 
/**
 * @param {BindDirective} node
 * @param {ComponentContext} context
 */
export function BindDirective(node, context) {
	const expression = node.expression;
	const property = binding_properties[node.name];
 
	const parent = /** @type {import('#compiler').SvelteNode} */ (context.path.at(-1));
 
	if (
		dev &&
		context.state.analysis.runes &&
		expression.type === 'MemberExpression' &&
		(node.name !== 'this' ||
			context.path.some(
				({ type }) =>
					type === 'IfBlock' || type === 'EachBlock' || type === 'AwaitBlock' || type === 'KeyBlock'
			)) &&
		!is_ignored(node, 'binding_property_non_reactive')
	) {
		validate_binding(
			context.state,
			node,
			/**@type {MemberExpression} */ (context.visit(expression))
		);
	}
 
	const getter = b.thunk(/** @type {Expression} */ (context.visit(expression)));
 
	const setter = b.arrow(
		[b.id('$$value')],
		/** @type {Expression} */ (context.visit(b.assignment('=', expression, b.id('$$value'))))
	);
 
	/** @type {CallExpression} */
	let call;
 
	if (property?.event) {
		call = b.call(
			'$.bind_property',
			b.literal(node.name),
			b.literal(property.event),
			context.state.node,
			setter,
			property.bidirectional && getter
		);
	} else {
		// special cases
		switch (node.name) {
			// window
			case 'online':
				call = b.call(`$.bind_online`, setter);
				break;
 
			case 'scrollX':
			case 'scrollY':
				call = b.call(
					'$.bind_window_scroll',
					b.literal(node.name === 'scrollX' ? 'x' : 'y'),
					getter,
					setter
				);
				break;
 
			case 'innerWidth':
			case 'innerHeight':
			case 'outerWidth':
			case 'outerHeight':
				call = b.call('$.bind_window_size', b.literal(node.name), setter);
				break;
 
			// document
			case 'activeElement':
				call = b.call('$.bind_active_element', setter);
				break;
 
			// media
			case 'muted':
				call = b.call(`$.bind_muted`, context.state.node, getter, setter);
				break;
			case 'paused':
				call = b.call(`$.bind_paused`, context.state.node, getter, setter);
				break;
			case 'volume':
				call = b.call(`$.bind_volume`, context.state.node, getter, setter);
				break;
			case 'playbackRate':
				call = b.call(`$.bind_playback_rate`, context.state.node, getter, setter);
				break;
			case 'currentTime':
				call = b.call(`$.bind_current_time`, context.state.node, getter, setter);
				break;
			case 'buffered':
				call = b.call(`$.bind_buffered`, context.state.node, setter);
				break;
			case 'played':
				call = b.call(`$.bind_played`, context.state.node, setter);
				break;
			case 'seekable':
				call = b.call(`$.bind_seekable`, context.state.node, setter);
				break;
			case 'seeking':
				call = b.call(`$.bind_seeking`, context.state.node, setter);
				break;
			case 'ended':
				call = b.call(`$.bind_ended`, context.state.node, setter);
				break;
			case 'readyState':
				call = b.call(`$.bind_ready_state`, context.state.node, setter);
				break;
 
			// dimensions
			case 'contentRect':
			case 'contentBoxSize':
			case 'borderBoxSize':
			case 'devicePixelContentBoxSize':
				call = b.call('$.bind_resize_observer', context.state.node, b.literal(node.name), setter);
				break;
 
			case 'clientWidth':
			case 'clientHeight':
			case 'offsetWidth':
			case 'offsetHeight':
				call = b.call('$.bind_element_size', context.state.node, b.literal(node.name), setter);
				break;
 
			// various
			case 'value': {
				if (parent?.type === 'RegularElement' && parent.name === 'select') {
					call = b.call(`$.bind_select_value`, context.state.node, getter, setter);
				} else {
					call = b.call(`$.bind_value`, context.state.node, getter, setter);
				}
				break;
			}
 
			case 'files':
				call = b.call(`$.bind_files`, context.state.node, getter, setter);
				break;
 
			case 'this':
				call = build_bind_this(expression, context.state.node, context);
				break;
 
			case 'textContent':
			case 'innerHTML':
			case 'innerText':
				call = b.call(
					'$.bind_content_editable',
					b.literal(node.name),
					context.state.node,
					getter,
					setter
				);
				break;
 
			// checkbox/radio
			case 'checked':
				call = b.call(`$.bind_checked`, context.state.node, getter, setter);
				break;
 
			case 'focused':
				call = b.call(`$.bind_focused`, context.state.node, setter);
				break;
 
			case 'group': {
				const indexes = node.metadata.parent_each_blocks.map((each) => {
					// if we have a keyed block with an index, the index is wrapped in a source
					return each.metadata.keyed && each.index
						? b.call('$.get', each.metadata.index)
						: each.metadata.index;
				});
 
				// We need to additionally invoke the value attribute signal to register it as a dependency,
				// so that when the value is updated, the group binding is updated
				let group_getter = getter;
 
				if (parent?.type === 'RegularElement') {
					const value = /** @type {any[]} */ (
						/** @type {Attribute} */ (
							parent.attributes.find(
								(a) =>
									a.type === 'Attribute' &&
									a.name === 'value' &&
									!is_text_attribute(a) &&
									a.value !== true
							)
						)?.value
					);
 
					if (value !== undefined) {
						group_getter = b.thunk(
							b.block([
								b.stmt(build_attribute_value(value, context).value),
								b.return(/** @type {Expression} */ (context.visit(expression)))
							])
						);
					}
				}
 
				call = b.call(
					'$.bind_group',
					node.metadata.binding_group_name,
					b.array(indexes),
					context.state.node,
					group_getter,
					setter
				);
				break;
			}
 
			default:
				throw new Error('unknown binding ' + node.name);
		}
	}
 
	// Bindings need to happen after attribute updates, therefore after the render effect, and in order with events/actions.
	// bind:this is a special case as it's one-way and could influence the render effect.
	if (node.name === 'this') {
		context.state.init.push(b.stmt(call));
	} else {
		const has_action_directive =
			parent.type === 'RegularElement' && parent.attributes.find((a) => a.type === 'UseDirective');
 
		context.state.after_update.push(
			b.stmt(has_action_directive ? b.call('$.effect', b.thunk(call)) : call)
		);
	}
}