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

86.04% Statements 222/258
64.86% Branches 48/74
100% Functions 1/1
85.71% Lines 216/252

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 241 242 243 244 245 246 247 248 249 250 251 252 2532x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 314x 314x 314x 314x 314x 314x 314x 314x 314x 9x 2x 2x 4x 314x 8x 314x 7x 7x 7x 7x 7x 7x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 314x 116x 116x 314x 314x 314x 314x 314x 6x 6x 6x 6x 6x 6x 6x 6x 314x 308x 308x 308x 308x     308x 308x 308x 3x 3x 3x 3x 3x 3x 3x 308x 308x 308x 308x 308x 4x 4x 308x 308x 308x 1x 1x 308x 308x 308x     308x     308x     308x     308x     308x     308x     308x     308x     308x     308x     308x 308x 308x 308x 308x 308x               308x 308x 308x 308x 308x     308x 308x 308x 136x 33x 136x 103x 103x 136x 136x 308x 308x     308x 308x 73x 73x 308x 308x 308x 308x 7x 7x 7x 7x 7x 7x 7x 7x 308x 308x 308x 20x 20x 308x 308x 2x 2x 308x 308x 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 308x 308x   308x 308x 314x 314x 314x 314x 73x 313x 241x 241x 241x 241x 241x 241x 241x 314x  
/** @import { CallExpression, Expression, MemberExpression } from 'estree' */
/** @import { AST, SvelteNode } 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 {AST.BindDirective} node
 * @param {ComponentContext} context
 */
export function BindDirective(node, context) {
	const expression = node.expression;
	const property = binding_properties[node.name];
 
	const parent = /** @type {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 get = b.thunk(/** @type {Expression} */ (context.visit(expression)));
 
	/** @type {Expression | undefined} */
	let set = b.unthunk(
		b.arrow(
			[b.id('$$value')],
			/** @type {Expression} */ (context.visit(b.assignment('=', expression, b.id('$$value'))))
		)
	);
 
	if (get === set) {
		set = undefined;
	}
 
	/** @type {CallExpression} */
	let call;
 
	if (property?.event) {
		call = b.call(
			'$.bind_property',
			b.literal(node.name),
			b.literal(property.event),
			context.state.node,
			set ?? get,
			property.bidirectional && get
		);
	} else {
		// special cases
		switch (node.name) {
			// window
			case 'online':
				call = b.call(`$.bind_online`, set ?? get);
				break;
 
			case 'scrollX':
			case 'scrollY':
				call = b.call(
					'$.bind_window_scroll',
					b.literal(node.name === 'scrollX' ? 'x' : 'y'),
					get,
					set
				);
				break;
 
			case 'innerWidth':
			case 'innerHeight':
			case 'outerWidth':
			case 'outerHeight':
				call = b.call('$.bind_window_size', b.literal(node.name), set ?? get);
				break;
 
			// document
			case 'activeElement':
				call = b.call('$.bind_active_element', set ?? get);
				break;
 
			// media
			case 'muted':
				call = b.call(`$.bind_muted`, context.state.node, get, set);
				break;
			case 'paused':
				call = b.call(`$.bind_paused`, context.state.node, get, set);
				break;
			case 'volume':
				call = b.call(`$.bind_volume`, context.state.node, get, set);
				break;
			case 'playbackRate':
				call = b.call(`$.bind_playback_rate`, context.state.node, get, set);
				break;
			case 'currentTime':
				call = b.call(`$.bind_current_time`, context.state.node, get, set);
				break;
			case 'buffered':
				call = b.call(`$.bind_buffered`, context.state.node, set ?? get);
				break;
			case 'played':
				call = b.call(`$.bind_played`, context.state.node, set ?? get);
				break;
			case 'seekable':
				call = b.call(`$.bind_seekable`, context.state.node, set ?? get);
				break;
			case 'seeking':
				call = b.call(`$.bind_seeking`, context.state.node, set ?? get);
				break;
			case 'ended':
				call = b.call(`$.bind_ended`, context.state.node, set ?? get);
				break;
			case 'readyState':
				call = b.call(`$.bind_ready_state`, context.state.node, set ?? get);
				break;
 
			// dimensions
			case 'contentRect':
			case 'contentBoxSize':
			case 'borderBoxSize':
			case 'devicePixelContentBoxSize':
				call = b.call(
					'$.bind_resize_observer',
					context.state.node,
					b.literal(node.name),
					set ?? get
				);
				break;
 
			case 'clientWidth':
			case 'clientHeight':
			case 'offsetWidth':
			case 'offsetHeight':
				call = b.call('$.bind_element_size', context.state.node, b.literal(node.name), set ?? get);
				break;
 
			// various
			case 'value': {
				if (parent?.type === 'RegularElement' && parent.name === 'select') {
					call = b.call(`$.bind_select_value`, context.state.node, get, set);
				} else {
					call = b.call(`$.bind_value`, context.state.node, get, set);
				}
				break;
			}
 
			case 'files':
				call = b.call(`$.bind_files`, context.state.node, get, set);
				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,
					get,
					set
				);
				break;
 
			// checkbox/radio
			case 'checked':
				call = b.call(`$.bind_checked`, context.state.node, get, set);
				break;
 
			case 'focused':
				call = b.call(`$.bind_focused`, context.state.node, set ?? get);
				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 = get;
 
				if (parent?.type === 'RegularElement') {
					const value = /** @type {any[]} */ (
						/** @type {AST.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,
					set ?? get
				);
				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)
		);
	}
}