Repository: sveltejs/svelte
File: packages/svelte/src/internal/client/dom/blocks/snippet.js (JavaScript)

Code before:
 * @param {() => SnippetFn | null | undefined} get_snippet
 * @param {boolean} is_chain
 * @param {(() => any)[]} args
 * @returns {void}
 */
export function snippet(node, get_snippet, is_chain, ...args) {
	var anchor = node;

	/** @type {SnippetFn | null | undefined | typeof UNINITIALIZED} */
	var snippet = UNINITIALIZED;

	/** @type {Effect | null} */
	var snippet_effect;

	block(() => {
		if (snippet === (snippet = get_snippet())) return;

		if (snippet_effect) {
			destroy_effect(snippet_effect);
			snippet_effect = null;
		}

		if (snippet != null) {
			snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(anchor, ...args));
		} else if (!is_chain) {
			e.invalid_snippet();
		}
	}, EFFECT_TRANSPARENT);

	if (hydrating) {
		anchor = hydrate_node;
	}
}

/**
 * In development, wrap the snippet function so that it passes validation, and so that the
 * correct component context is set for ownership checks
 * @param {any} component
 * @param {(node: TemplateNode, ...args: any[]) => void} fn
 */
export function wrap_snippet(component, fn) {
	return (/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
		var previous_component_function = dev_current_component_function;
		set_dev_current_component_function(component);

		try {
			return fn(node, ...args);
		} finally {
			set_dev_current_component_function(previous_component_function);
		}
	};

Review comment (bug):
Is there any drawback to just remove the if block instead? In my local testing this fails with a somewhat readable error message
```suggestion
		snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(anchor, ...args));
```
...though the location may be harder to find. So maybe only have the more readable error message and extra argument at DEV time to save a few bytes? (possibly this means having a separate `snippet_dev` function)

Code after:
 * @param {() => SnippetFn | null | undefined} get_snippet
 * @param {(() => any)[]} args
 * @returns {void}
 */
export function snippet(node, get_snippet, ...args) {
	var anchor = node;

	/** @type {SnippetFn | null | undefined} */
	// @ts-ignore
	var snippet = noop;

	/** @type {Effect | null} */
	var snippet_effect;

	block(() => {
		if (snippet === (snippet = get_snippet())) return;

		if (snippet_effect) {
			destroy_effect(snippet_effect);
			snippet_effect = null;
		}

		if (DEV && snippet == null) {
			e.invalid_snippet();
		}

		snippet_effect = branch(() => /** @type {SnippetFn} */ (snippet)(anchor, ...args));
	}, EFFECT_TRANSPARENT);

	if (hydrating) {
		anchor = hydrate_node;
	}
}

/**
 * In development, wrap the snippet function so that it passes validation, and so that the
 * correct component context is set for ownership checks
 * @param {any} component
 * @param {(node: TemplateNode, ...args: any[]) => void} fn
 */
export function wrap_snippet(component, fn) {
	return (/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
		var previous_component_function = dev_current_component_function;
		set_dev_current_component_function(component);

		try {
			return fn(node, ...args);
		} finally {
			set_dev_current_component_function(previous_component_function);
		}
	};