Skip to content

fix: replace undefined with void 0 to avoid edge case #15511

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 5 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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/curvy-countries-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: replace `undefined` with `void 0` to avoid edge case
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export function VariableDeclaration(node, context) {
}

const args = /** @type {CallExpression} */ (init).arguments;
const value =
args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (context.visit(args[0]));
const value = args.length > 0 ? /** @type {Expression} */ (context.visit(args[0])) : b.void0;

if (rune === '$state' || rune === '$state.raw') {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function CallExpression(node, context) {
const rune = get_rune(node, context.state.scope);

if (rune === '$host') {
return b.id('undefined');
return b.void0;
}

if (rune === '$effect.tracking') {
return b.literal(false);
return b.false;
}

if (rune === '$effect.root') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function VariableDeclaration(node, context) {
) {
const right = node.right.arguments.length
? /** @type {Expression} */ (context.visit(node.right.arguments[0]))
: b.id('undefined');
: b.void0;
return b.assignment_pattern(node.left, right);
}
}
Expand Down Expand Up @@ -75,8 +75,7 @@ export function VariableDeclaration(node, context) {
}

const args = /** @type {CallExpression} */ (init).arguments;
const value =
args.length === 0 ? b.id('undefined') : /** @type {Expression} */ (context.visit(args[0]));
const value = args.length > 0 ? /** @type {Expression} */ (context.visit(args[0])) : b.void0;

if (rune === '$derived.by') {
declarations.push(
Expand Down
2 changes: 2 additions & 0 deletions packages/svelte/src/compiler/utils/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export function unary(operator, argument) {
return { type: 'UnaryExpression', argument, operator, prefix: true };
}

export const void0 = unary('void', b.literal(0));

/**
* @param {ESTree.Expression} test
* @param {ESTree.Expression} consequent
Expand Down
Loading