Skip to content

Commit 057316c

Browse files
authored
chore: simpler fallback values (#12788)
* DRY * value_or_fallback_async doesnt do anything * rename value_or_fallback to fallback * DRY * DRY * make lazy the exception * changeset
1 parent 19819d0 commit 057316c

File tree

7 files changed

+46
-56
lines changed

7 files changed

+46
-56
lines changed

.changeset/witty-frogs-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
chore: simpler fallback values

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

+3-7
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 { 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';
@@ -96,9 +96,7 @@ export function VariableDeclaration(node, context) {
9696
const name = /** @type {Identifier} */ (path.node).name;
9797
const binding = /** @type {Binding} */ (context.state.scope.get(name));
9898
const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true);
99-
declarations.push(
100-
b.declarator(path.node, b.call('$.value_or_fallback', prop, b.thunk(value)))
101-
);
99+
declarations.push(b.declarator(path.node, build_fallback(prop, value)));
102100
}
103101
continue;
104102
}
@@ -114,9 +112,7 @@ export function VariableDeclaration(node, context) {
114112
let init = prop;
115113
if (declarator.init) {
116114
const default_value = /** @type {Expression} */ (context.visit(declarator.init));
117-
init = is_expression_async(default_value)
118-
? b.await(b.call('$.value_or_fallback_async', prop, b.thunk(default_value, true)))
119-
: b.call('$.value_or_fallback', prop, b.thunk(default_value));
115+
init = build_fallback(prop, default_value);
120116
}
121117

122118
declarations.push(b.declarator(declarator.id, init));

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,7 @@ function _extract_paths(assignments = [], param, expression, update_expression,
368368

369369
case 'AssignmentPattern': {
370370
/** @type {DestructuredAssignment['expression']} */
371-
const fallback_expression = (object) =>
372-
is_expression_async(param.right)
373-
? b.await(
374-
b.call('$.value_or_fallback_async', expression(object), b.thunk(param.right, true))
375-
)
376-
: b.call('$.value_or_fallback', expression(object), b.thunk(param.right));
371+
const fallback_expression = (object) => build_fallback(expression(object), param.right);
377372

378373
if (param.left.type === 'Identifier') {
379374
assignments.push({
@@ -549,3 +544,22 @@ export function is_expression_async(expression) {
549544
return false;
550545
}
551546
}
547+
548+
/**
549+
*
550+
* @param {ESTree.Expression} expression
551+
* @param {ESTree.Expression} fallback
552+
*/
553+
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+
562+
return is_expression_async(fallback)
563+
? b.await(b.call('$.fallback', expression, b.thunk(fallback, true), b.true))
564+
: b.call('$.fallback', expression, b.thunk(fallback), b.true);
565+
}

packages/svelte/src/internal/client/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ export {
134134
untrack,
135135
update,
136136
update_pre,
137-
value_or_fallback,
138-
value_or_fallback_async,
139137
exclude_from_object,
140138
pop,
141139
push,
@@ -164,7 +162,7 @@ export {
164162
$document as document
165163
} from './dom/operations.js';
166164
export { snapshot } from '../shared/clone.js';
167-
export { noop } from '../shared/utils.js';
165+
export { noop, fallback } from '../shared/utils.js';
168166
export {
169167
invalid_default_snippet,
170168
validate_dynamic_element_tag,

packages/svelte/src/internal/client/runtime.js

-20
Original file line numberDiff line numberDiff line change
@@ -1003,26 +1003,6 @@ export function exclude_from_object(obj, keys) {
10031003
return result;
10041004
}
10051005

1006-
/**
1007-
* @template V
1008-
* @param {V} value
1009-
* @param {() => V} fallback lazy because could contain side effects
1010-
* @returns {V}
1011-
*/
1012-
export function value_or_fallback(value, fallback) {
1013-
return value === undefined ? fallback() : value;
1014-
}
1015-
1016-
/**
1017-
* @template V
1018-
* @param {V} value
1019-
* @param {() => Promise<V>} fallback lazy because could contain side effects
1020-
* @returns {Promise<V>}
1021-
*/
1022-
export async function value_or_fallback_async(value, fallback) {
1023-
return value === undefined ? fallback() : value;
1024-
}
1025-
10261006
/**
10271007
* @param {Record<string, unknown>} props
10281008
* @param {any} runes

packages/svelte/src/internal/server/index.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -383,26 +383,6 @@ export function unsubscribe_stores(store_values) {
383383
}
384384
}
385385

386-
/**
387-
* @template V
388-
* @param {V} value
389-
* @param {() => V} fallback lazy because could contain side effects
390-
* @returns {V}
391-
*/
392-
export function value_or_fallback(value, fallback) {
393-
return value === undefined ? fallback() : value;
394-
}
395-
396-
/**
397-
* @template V
398-
* @param {V} value
399-
* @param {() => Promise<V>} fallback lazy because could contain side effects
400-
* @returns {Promise<V>}
401-
*/
402-
export async function value_or_fallback_async(value, fallback) {
403-
return value === undefined ? fallback() : value;
404-
}
405-
406386
/**
407387
* @param {Payload} payload
408388
* @param {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn
@@ -536,6 +516,8 @@ export { push_element, pop_element } from './dev.js';
536516

537517
export { snapshot } from '../shared/clone.js';
538518

519+
export { fallback } from '../shared/utils.js';
520+
539521
export {
540522
invalid_default_snippet,
541523
validate_dynamic_element_tag,

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

+15
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,18 @@ export function run_all(arr) {
4646
arr[i]();
4747
}
4848
}
49+
50+
/**
51+
* @template V
52+
* @param {V} value
53+
* @param {V | (() => V)} fallback
54+
* @param {boolean} [lazy]
55+
* @returns {V}
56+
*/
57+
export function fallback(value, fallback, lazy = false) {
58+
return value === undefined
59+
? lazy
60+
? /** @type {() => V} */ (fallback)()
61+
: /** @type {V} */ (fallback)
62+
: value;
63+
}

0 commit comments

Comments
 (0)