|
|
@ -3,15 +3,18 @@
|
|
|
|
/** @import { Context } from '../types' */
|
|
|
|
/** @import { Context } from '../types' */
|
|
|
|
import { dev } from '../../../../state.js';
|
|
|
|
import { dev } from '../../../../state.js';
|
|
|
|
import * as b from '../../../../utils/builders.js';
|
|
|
|
import * as b from '../../../../utils/builders.js';
|
|
|
|
|
|
|
|
import { get_rune } from '../../../scope.js';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param {AwaitExpression} node
|
|
|
|
* @param {AwaitExpression} node
|
|
|
|
* @param {Context} context
|
|
|
|
* @param {Context} context
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function AwaitExpression(node, context) {
|
|
|
|
export function AwaitExpression(node, context) {
|
|
|
|
const tla = context.state.is_instance && context.state.scope.function_depth === 1;
|
|
|
|
const save =
|
|
|
|
|
|
|
|
// preserve context if this is a top-level await in `<script>`
|
|
|
|
const save = tla || !is_last_evaluated_expression(context.path, node);
|
|
|
|
(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) {
|
|
|
|
if (dev || save) {
|
|
|
|
const expression = /** @type {Expression} */ (context.visit(node.argument));
|
|
|
|
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 {AST.SvelteNode[]} path
|
|
|
|
* @param {Expression | SpreadElement | Property} node
|
|
|
|
* @param {Expression | SpreadElement | Property} node
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function is_last_evaluated_expression(path, node) {
|
|
|
|
function is_last_evaluated_expression(path, node) {
|
|
|
|
let i = path.length;
|
|
|
|
let i = path.length;
|
|
|
|
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
while (i--) {
|
|
|
|