Skip to content

Commit c5f0f93

Browse files
committed
make lazy the exception
1 parent b91762a commit c5f0f93

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/** @import { Binding } from '#compiler' */
33
/** @import { Context } from '../types.js' */
44
/** @import { Scope } from '../../../scope.js' */
5-
import { build_fallback, extract_paths, is_expression_async } from '../../../../utils/ast.js';
5+
import { build_fallback, extract_paths } from '../../../../utils/ast.js';
66
import * as b from '../../../../utils/builders.js';
77
import { get_rune } from '../../../scope.js';
88
import { walk } from 'zimmerframe';

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,15 @@ export function is_expression_async(expression) {
551551
* @param {ESTree.Expression} fallback
552552
*/
553553
export function build_fallback(expression, fallback) {
554+
if (is_simple_expression(fallback)) {
555+
return b.call('$.fallback', expression, fallback);
556+
}
557+
558+
if (fallback.type === 'AwaitExpression' && is_simple_expression(fallback.argument)) {
559+
return b.await(b.call('$.fallback', expression, fallback.argument));
560+
}
561+
554562
return is_expression_async(fallback)
555-
? b.await(b.call('$.fallback', expression, b.thunk(fallback, true)))
556-
: b.call('$.fallback', expression, b.thunk(fallback));
563+
? b.await(b.call('$.fallback', expression, b.thunk(fallback, true), b.true))
564+
: b.call('$.fallback', expression, b.thunk(fallback), b.true);
557565
}

packages/svelte/src/internal/shared/utils.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ export function run_all(arr) {
5050
/**
5151
* @template V
5252
* @param {V} value
53-
* @param {() => V} fallback lazy because could contain side effects
53+
* @param {V | (() => V)} fallback
54+
* @param {boolean} [lazy]
5455
* @returns {V}
5556
*/
56-
export function fallback(value, fallback) {
57-
return value === undefined ? fallback() : value;
57+
export function fallback(value, fallback, lazy = false) {
58+
return value === undefined
59+
? lazy
60+
? /** @type {() => V} */ (fallback)()
61+
: /** @type {V} */ (fallback)
62+
: value;
5863
}

0 commit comments

Comments
 (0)