From b2448dcc5c0bfef88e4640fbdd65ce6b695143b6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 5 Jul 2024 04:28:58 -0700 Subject: [PATCH] fix: ensure `bind:this` works with component with no return value (#12308) --- .changeset/warm-waves-reply.md | 2 +- .../compiler/phases/3-transform/client/transform-client.js | 3 --- .../svelte/src/internal/client/dom/elements/bindings/this.js | 4 ++-- .../bind-component-snippet/_expected/client/index.svelte.js | 1 - .../samples/bind-this/_expected/client/index.svelte.js | 1 - .../dynamic-attributes-casing/_expected/client/main.svelte.js | 1 - .../each-string-template/_expected/client/index.svelte.js | 1 - .../function-prop-no-getter/_expected/client/index.svelte.js | 2 -- .../samples/hello-world/_expected/client/index.svelte.js | 1 - .../snapshot/samples/hmr/_expected/client/index.svelte.js | 1 - .../state-proxy-literal/_expected/client/index.svelte.js | 1 - .../samples/svelte-element/_expected/client/index.svelte.js | 1 - 12 files changed, 3 insertions(+), 16 deletions(-) diff --git a/.changeset/warm-waves-reply.md b/.changeset/warm-waves-reply.md index 7f7f901aa3..2ddd4de7fa 100644 --- a/.changeset/warm-waves-reply.md +++ b/.changeset/warm-waves-reply.md @@ -2,4 +2,4 @@ 'svelte': patch --- -fix: ensure component always returns an object +fix: ensure `bind:this` works with component with no return value diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js index 222610e1e3..d0dd2315a5 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js @@ -339,9 +339,6 @@ export function client_component(source, analysis, options) { ? b.return(b.call('$.pop', b.object(component_returned_object))) : b.stmt(b.call('$.pop')) ); - } else { - // Always return an object, so that `bind:this` on this component will not be falsy - component_block.body.push(b.return(b.object(component_returned_object))); } if (analysis.uses_rest_props) { diff --git a/packages/svelte/src/internal/client/dom/elements/bindings/this.js b/packages/svelte/src/internal/client/dom/elements/bindings/this.js index de78858b60..a6bec253e5 100644 --- a/packages/svelte/src/internal/client/dom/elements/bindings/this.js +++ b/packages/svelte/src/internal/client/dom/elements/bindings/this.js @@ -15,14 +15,14 @@ function is_bound_this(bound_value, element_or_component) { } /** - * @param {Element} element_or_component + * @param {any} element_or_component * @param {(value: unknown, ...parts: unknown[]) => void} update * @param {(...parts: unknown[]) => unknown} get_value * @param {() => unknown[]} [get_parts] Set if the this binding is used inside an each block, * returns all the parts of the each block context that are used in the expression * @returns {void} */ -export function bind_this(element_or_component, update, get_value, get_parts) { +export function bind_this(element_or_component = {}, update, get_value, get_parts) { effect(() => { /** @type {unknown[]} */ var old_parts; diff --git a/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js index 2ffcbeffab..ba4352d57b 100644 --- a/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/bind-component-snippet/_expected/client/index.svelte.js @@ -30,5 +30,4 @@ export default function Bind_component_snippet($$anchor) { $.template_effect(() => $.set_text(text, ` value: ${$.get(value) ?? ""}`)); $.append($$anchor, fragment_1); - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/bind-this/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/bind-this/_expected/client/index.svelte.js index a2fc02644b..bd24eca962 100644 --- a/packages/svelte/tests/snapshot/samples/bind-this/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/bind-this/_expected/client/index.svelte.js @@ -3,5 +3,4 @@ import * as $ from "svelte/internal/client"; export default function Bind_this($$anchor) { $.bind_this(Foo($$anchor, { $$legacy: true }), ($$value) => foo = $$value, () => foo); - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js index eafec512f6..aa4d22d99a 100644 --- a/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js +++ b/packages/svelte/tests/snapshot/samples/dynamic-attributes-casing/_expected/client/main.svelte.js @@ -30,5 +30,4 @@ export default function Main($$anchor) { }); $.append($$anchor, fragment); - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js index 872fa30d8d..5d5e47faf2 100644 --- a/packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js @@ -13,5 +13,4 @@ export default function Each_string_template($$anchor) { }); $.append($$anchor, fragment); - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js index 512c2d9d8d..8fb6d075cd 100644 --- a/packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js @@ -22,6 +22,4 @@ export default function Function_prop_no_getter($$anchor) { }, $$slots: { default: true } }); - - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/hello-world/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/hello-world/_expected/client/index.svelte.js index da64ce05c5..92354d8f14 100644 --- a/packages/svelte/tests/snapshot/samples/hello-world/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/hello-world/_expected/client/index.svelte.js @@ -7,5 +7,4 @@ export default function Hello_world($$anchor) { var h1 = root(); $.append($$anchor, h1); - return {}; } \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js index 7358eefd30..6cb6845c1c 100644 --- a/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/hmr/_expected/client/index.svelte.js @@ -7,7 +7,6 @@ function Hmr($$anchor) { var h1 = root(); $.append($$anchor, h1); - return {}; } if (import.meta.hot) { diff --git a/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js index 45427aa6ff..0f2d6f4200 100644 --- a/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/state-proxy-literal/_expected/client/index.svelte.js @@ -28,7 +28,6 @@ export default function State_proxy_literal($$anchor) { $.bind_value(input, () => $.get(str), ($$value) => $.set(str, $$value)); $.bind_value(input_1, () => $.get(tpl), ($$value) => $.set(tpl, $$value)); $.append($$anchor, fragment); - return {}; } $.delegate(["click"]); \ No newline at end of file diff --git a/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js index 484d937327..a4bbea582b 100644 --- a/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js @@ -8,5 +8,4 @@ export default function Svelte_element($$anchor, $$props) { $.element(node, tag, false); $.append($$anchor, fragment); - return {}; } \ No newline at end of file