Skip to content

chore: simpler fallback values #12788

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 7 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/witty-frogs-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

chore: simpler fallback values
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** @import { Binding } from '#compiler' */
/** @import { Context } from '../types.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 { get_rune } from '../../../scope.js';
import { walk } from 'zimmerframe';
Expand Down Expand Up @@ -96,9 +96,7 @@ export function VariableDeclaration(node, context) {
const name = /** @type {Identifier} */ (path.node).name;
const binding = /** @type {Binding} */ (context.state.scope.get(name));
const prop = b.member(b.id('$$props'), b.literal(binding.prop_alias ?? name), true);
declarations.push(
b.declarator(path.node, b.call('$.value_or_fallback', prop, b.thunk(value)))
);
declarations.push(b.declarator(path.node, build_fallback(prop, value)));
}
continue;
}
Expand All @@ -114,9 +112,7 @@ export function VariableDeclaration(node, context) {
let init = prop;
if (declarator.init) {
const default_value = /** @type {Expression} */ (context.visit(declarator.init));
init = is_expression_async(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));
init = build_fallback(prop, default_value);
}

declarations.push(b.declarator(declarator.id, init));
Expand Down
26 changes: 20 additions & 6 deletions packages/svelte/src/compiler/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,7 @@ function _extract_paths(assignments = [], param, expression, update_expression,

case 'AssignmentPattern': {
/** @type {DestructuredAssignment['expression']} */
const fallback_expression = (object) =>
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));
const fallback_expression = (object) => build_fallback(expression(object), param.right);

if (param.left.type === 'Identifier') {
assignments.push({
Expand Down Expand Up @@ -549,3 +544,22 @@ export function is_expression_async(expression) {
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);
}
4 changes: 1 addition & 3 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ export {
untrack,
update,
update_pre,
value_or_fallback,
value_or_fallback_async,
exclude_from_object,
pop,
push,
Expand Down Expand Up @@ -164,7 +162,7 @@ export {
$document as document
} from './dom/operations.js';
export { snapshot } from '../shared/clone.js';
export { noop } from '../shared/utils.js';
export { noop, fallback } from '../shared/utils.js';
export {
invalid_default_snippet,
validate_dynamic_element_tag,
Expand Down
20 changes: 0 additions & 20 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,26 +1003,6 @@ export function exclude_from_object(obj, keys) {
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 {any} runes
Expand Down
22 changes: 2 additions & 20 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {void | ((payload: Payload, props: Record<string, unknown>) => void)} slot_fn
Expand Down Expand Up @@ -536,6 +516,8 @@ export { push_element, pop_element } from './dev.js';

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

export { fallback } from '../shared/utils.js';

export {
invalid_default_snippet,
validate_dynamic_element_tag,
Expand Down
15 changes: 15 additions & 0 deletions packages/svelte/src/internal/shared/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,18 @@ export function run_all(arr) {
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