Skip to content

fix: correctly transform {@const foo = await ...} #16463

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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/fluffy-ducks-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly transform `{@const foo = await ...}`
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ export function should_proxy(node, scope) {
* @param {ComponentClientTransformState} state
* @param {Expression} arg
*/
export function create_derived(state, arg) {
return b.call(state.analysis.runes ? '$.derived' : '$.derived_safe_equal', arg);
export function create_derived(state, arg, async = false) {
return b.call(
state.analysis.runes ? `$.${async ? 'async_' : ''}derived` : '$.derived_safe_equal',
arg
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import { dev } from '../../../../state.js';
import { extract_identifiers } from '../../../../utils/ast.js';
import { extract_identifiers, is_expression_async } from '../../../../utils/ast.js';
import * as b from '#compiler/builders';
import { create_derived } from '../utils.js';
import { get_value } from './shared/declarations.js';
Expand All @@ -17,7 +17,11 @@ export function ConstTag(node, context) {
// TODO we can almost certainly share some code with $derived(...)
if (declaration.id.type === 'Identifier') {
const init = build_expression(context, declaration.init, node.metadata.expression);
let expression = create_derived(context.state, b.thunk(init));
let expression = create_derived(
context.state,
is_expression_async(init) ? b.arrow([], init, true) : b.thunk(init),
is_expression_async(init)
);

if (dev) {
expression = b.call('$.tag', expression, b.literal(declaration.id.name));
Expand Down Expand Up @@ -58,10 +62,11 @@ export function ConstTag(node, context) {
b.block([
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
])
]),
is_expression_async(init)
);

let expression = create_derived(context.state, fn);
let expression = create_derived(context.state, fn, is_expression_async(init));

if (dev) {
expression = b.call('$.tag', expression, b.literal('[@const]'));
Expand Down
Loading