partial fix

pull/15844/head
Rich Harris 3 months ago
parent d96991a05b
commit 36947c5922

@ -3,15 +3,18 @@
/** @import { Context } from '../types' */
import { dev } from '../../../../state.js';
import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js';
/**
* @param {AwaitExpression} node
* @param {Context} context
*/
export function AwaitExpression(node, context) {
const tla = context.state.is_instance && context.state.scope.function_depth === 1;
const save = tla || !is_last_evaluated_expression(context.path, node);
const save =
// preserve context if this is a top-level await in `<script>`
(context.state.is_instance && context.state.scope.function_depth === 1) ||
// or if this is a derived/template expression
(is_reactive_expression(context) && !is_last_evaluated_expression(context.path, node));
if (dev || save) {
const expression = /** @type {Expression} */ (context.visit(node.argument));
@ -22,11 +25,40 @@ export function AwaitExpression(node, context) {
}
/**
*
* @param {Context} context
*/
function is_reactive_expression(context) {
let i = context.path.length;
while (i--) {
const parent = context.path[i];
if (
parent.type === 'ArrowFunctionExpression' ||
parent.type === 'FunctionExpression' ||
parent.type === 'FunctionDeclaration'
) {
return false;
}
if (parent.type === 'CallExpression' && get_rune(parent, context.state.scope) === '$derived') {
return true;
}
// @ts-expect-error we could probably use a neater/more robust mechanism
if (parent.metadata) {
return true;
}
}
return false;
}
/**
* @param {AST.SvelteNode[]} path
* @param {Expression | SpreadElement | Property} node
*/
export function is_last_evaluated_expression(path, node) {
function is_last_evaluated_expression(path, node) {
let i = path.length;
while (i--) {

Loading…
Cancel
Save