Skip to content

Commit 489f463

Browse files
fix: replace undefined with void 0 to avoid edge case (#15511)
* replace 'undefined' with 'void 0' * lint * YALF * reuse expression * oops --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 5d3aa2b commit 489f463

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

.changeset/curvy-countries-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: replace `undefined` with `void 0` to avoid edge case

packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ export function VariableDeclaration(node, context) {
116116
}
117117

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

122121
if (rune === '$state' || rune === '$state.raw') {
123122
/**

packages/svelte/src/compiler/phases/3-transform/server/visitors/CallExpression.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export function CallExpression(node, context) {
1313
const rune = get_rune(node, context.state.scope);
1414

1515
if (rune === '$host') {
16-
return b.id('undefined');
16+
return b.void0;
1717
}
1818

1919
if (rune === '$effect.tracking') {
20-
return b.literal(false);
20+
return b.false;
2121
}
2222

2323
if (rune === '$effect.root') {

packages/svelte/src/compiler/phases/3-transform/server/visitors/VariableDeclaration.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function VariableDeclaration(node, context) {
4545
) {
4646
const right = node.right.arguments.length
4747
? /** @type {Expression} */ (context.visit(node.right.arguments[0]))
48-
: b.id('undefined');
48+
: b.void0;
4949
return b.assignment_pattern(node.left, right);
5050
}
5151
}
@@ -75,8 +75,7 @@ export function VariableDeclaration(node, context) {
7575
}
7676

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

8180
if (rune === '$derived.by') {
8281
declarations.push(

packages/svelte/src/compiler/utils/builders.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ export function unary(operator, argument) {
154154
return { type: 'UnaryExpression', argument, operator, prefix: true };
155155
}
156156

157+
export const void0 = unary('void', literal(0));
158+
157159
/**
158160
* @param {ESTree.Expression} test
159161
* @param {ESTree.Expression} consequent

0 commit comments

Comments
 (0)