Skip to content

fix: treat nullish expression as empty string #15901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-badgers-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: treat nullish expression as empty string
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function build_template_chunk(
// If we have a single expression, then pass that in directly to possibly avoid doing
// extra work in the template_effect (instead we do the work in set_text).
if (evaluated.is_known) {
value = b.literal(evaluated.value);
value = b.literal((evaluated.value ?? '') + '');
}

return { value, has_state };
Expand All @@ -96,7 +96,7 @@ export function build_template_chunk(
}

if (evaluated.is_known) {
quasi.value.cooked += evaluated.value + '';
quasi.value.cooked += (evaluated.value ?? '') + '';
} else {
if (!evaluated.is_defined) {
// add `?? ''` where necessary
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
html: '[]'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{undefined ?? null}]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Purity($$anchor) {
var fragment = root();
var p = $.first_child(fragment);

p.textContent = 0;
p.textContent = '0';

var p_1 = $.sibling(p, 2);

Expand Down