From 509ba568f3b16c82c9a2e0c55e5b3634edd46e85 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 17 Jan 2025 10:32:51 -0500 Subject: [PATCH] fix: create fewer deriveds for concatenated strings (#15041) * fix: create fewer deriveds for concatenated strings * stringified_text -> expression --- .changeset/rich-donkeys-wink.md | 5 +++++ .../phases/3-transform/client/visitors/shared/utils.js | 4 ++-- .../text-nodes-deriveds/_expected/client/index.svelte.js | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .changeset/rich-donkeys-wink.md diff --git a/.changeset/rich-donkeys-wink.md b/.changeset/rich-donkeys-wink.md new file mode 100644 index 000000000000..be8d02b67052 --- /dev/null +++ b/.changeset/rich-donkeys-wink.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: create fewer deriveds for concatenated strings diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js index 1854baa1e964..69fd8832143f 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/utils.js @@ -47,8 +47,8 @@ export function build_template_chunk(values, visit, state) { quasi.value.cooked += node.expression.value + ''; } } else { - if (contains_multiple_call_expression) { - const id = b.id(state.scope.generate('stringified_text')); + if (node.metadata.expression.has_call && contains_multiple_call_expression) { + const id = b.id(state.scope.generate('expression')); state.init.push( b.const( id, diff --git a/packages/svelte/tests/snapshot/samples/text-nodes-deriveds/_expected/client/index.svelte.js b/packages/svelte/tests/snapshot/samples/text-nodes-deriveds/_expected/client/index.svelte.js index 8e343fcf3602..2e0bd651da38 100644 --- a/packages/svelte/tests/snapshot/samples/text-nodes-deriveds/_expected/client/index.svelte.js +++ b/packages/svelte/tests/snapshot/samples/text-nodes-deriveds/_expected/client/index.svelte.js @@ -16,11 +16,11 @@ export default function Text_nodes_deriveds($$anchor) { } var p = root(); - const stringified_text = $.derived(() => text1() ?? ''); - const stringified_text_1 = $.derived(() => text2() ?? ''); + const expression = $.derived(() => text1() ?? ''); + const expression_1 = $.derived(() => text2() ?? ''); var text = $.child(p); - $.template_effect(() => $.set_text(text, `${$.get(stringified_text)}${$.get(stringified_text_1)}`)); + $.template_effect(() => $.set_text(text, `${$.get(expression)}${$.get(expression_1)}`)); $.reset(p); $.append($$anchor, p); } \ No newline at end of file