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
pull/12789/head
Rich Harris 3 months ago committed by GitHub
parent 19819d0477
commit 057316ccd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
chore: simpler fallback values

@ -2,7 +2,7 @@
/** @import { Binding } from '#compiler' */ /** @import { Binding } from '#compiler' */
/** @import { Context } from '../types.js' */ /** @import { Context } from '../types.js' */
/** @import { Scope } from '../../../scope.js' */ /** @import { Scope } from '../../../scope.js' */
import { extract_paths, is_expression_async } from '../../../../utils/ast.js'; import { build_fallback, extract_paths } from '../../../../utils/ast.js';
import * as b from '../../../../utils/builders.js'; import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js'; import { get_rune } from '../../../scope.js';
import { walk } from 'zimmerframe'; import { walk } from 'zimmerframe';
@ -96,9 +96,7 @@ export function VariableDeclaration(node, context) {
const name = /** @type {Identifier} */ (path.node).name; const name = /** @type {Identifier} */ (path.node).name;
const binding = /** @type {Binding} */ (context.state.scope.get(name)); const binding = /** @type {Binding} */ (context.state.scope.get(name));
const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true); const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true);
declarations.push( declarations.push(b.declarator(path.node, build_fallback(prop, value)));
b.declarator(path.node, b.call('$.value_or_fallback', prop, b.thunk(value)))
);
} }
continue; continue;
} }
@ -114,9 +112,7 @@ export function VariableDeclaration(node, context) {
let init = prop; let init = prop;
if (declarator.init) { if (declarator.init) {
const default_value = /** @type {Expression} */ (context.visit(declarator.init)); const default_value = /** @type {Expression} */ (context.visit(declarator.init));
init = is_expression_async(default_value) init = build_fallback(prop, default_value);
? b.await(b.call('$.value_or_fallback_async', prop, b.thunk(default_value, true)))
: b.call('$.value_or_fallback', prop, b.thunk(default_value));
} }
declarations.push(b.declarator(declarator.id, init)); declarations.push(b.declarator(declarator.id, init));

@ -368,12 +368,7 @@ function _extract_paths(assignments = [], param, expression, update_expression,
case 'AssignmentPattern': { case 'AssignmentPattern': {
/** @type {DestructuredAssignment['expression']} */ /** @type {DestructuredAssignment['expression']} */
const fallback_expression = (object) => const fallback_expression = (object) => build_fallback(expression(object), param.right);
is_expression_async(param.right)
? b.await(
b.call('$.value_or_fallback_async', expression(object), b.thunk(param.right, true))
)
: b.call('$.value_or_fallback', expression(object), b.thunk(param.right));
if (param.left.type === 'Identifier') { if (param.left.type === 'Identifier') {
assignments.push({ assignments.push({
@ -549,3 +544,22 @@ export function is_expression_async(expression) {
return false; return false;
} }
} }
/**
*
* @param {ESTree.Expression} expression
* @param {ESTree.Expression} fallback
*/
export function build_fallback(expression, fallback) {
if (is_simple_expression(fallback)) {
return b.call('$.fallback', expression, fallback);
}
if (fallback.type === 'AwaitExpression' && is_simple_expression(fallback.argument)) {
return b.await(b.call('$.fallback', expression, fallback.argument));
}
return is_expression_async(fallback)
? b.await(b.call('$.fallback', expression, b.thunk(fallback, true), b.true))
: b.call('$.fallback', expression, b.thunk(fallback), b.true);
}

@ -134,8 +134,6 @@ export {
untrack, untrack,
update, update,
update_pre, update_pre,
value_or_fallback,
value_or_fallback_async,
exclude_from_object, exclude_from_object,
pop, pop,
push, push,
@ -164,7 +162,7 @@ export {
$document as document $document as document
} from './dom/operations.js'; } from './dom/operations.js';
export { snapshot } from '../shared/clone.js'; export { snapshot } from '../shared/clone.js';
export { noop } from '../shared/utils.js'; export { noop, fallback } from '../shared/utils.js';
export { export {
invalid_default_snippet, invalid_default_snippet,
validate_dynamic_element_tag, validate_dynamic_element_tag,

@ -1003,26 +1003,6 @@ export function exclude_from_object(obj, keys) {
return result; return result;
} }
/**
* @template V
* @param {V} value
* @param {() => V} fallback lazy because could contain side effects
* @returns {V}
*/
export function value_or_fallback(value, fallback) {
return value === undefined ? fallback() : value;
}
/**
* @template V
* @param {V} value
* @param {() => Promise<V>} fallback lazy because could contain side effects
* @returns {Promise<V>}
*/
export async function value_or_fallback_async(value, fallback) {
return value === undefined ? fallback() : value;
}
/** /**
* @param {Record<string, unknown>} props * @param {Record<string, unknown>} props
* @param {any} runes * @param {any} runes

@ -383,26 +383,6 @@ export function unsubscribe_stores(store_values) {
} }
} }
/**
* @template V
* @param {V} value
* @param {() => V} fallback lazy because could contain side effects
* @returns {V}
*/
export function value_or_fallback(value, fallback) {
return value === undefined ? fallback() : value;
}
/**
* @template V
* @param {V} value
* @param {() => Promise<V>} fallback lazy because could contain side effects
* @returns {Promise<V>}
*/
export async function value_or_fallback_async(value, fallback) {
return value === undefined ? fallback() : value;
}
/** /**
* @param {Payload} payload * @param {Payload} payload
* @param {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn * @param {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn
@ -536,6 +516,8 @@ export { push_element, pop_element } from './dev.js';
export { snapshot } from '../shared/clone.js'; export { snapshot } from '../shared/clone.js';
export { fallback } from '../shared/utils.js';
export { export {
invalid_default_snippet, invalid_default_snippet,
validate_dynamic_element_tag, validate_dynamic_element_tag,

@ -46,3 +46,18 @@ export function run_all(arr) {
arr[i](); arr[i]();
} }
} }
/**
* @template V
* @param {V} value
* @param {V | (() => V)} fallback
* @param {boolean} [lazy]
* @returns {V}
*/
export function fallback(value, fallback, lazy = false) {
return value === undefined
? lazy
? /** @type {() => V} */ (fallback)()
: /** @type {V} */ (fallback)
: value;
}

Loading…
Cancel
Save