|
1 | 1 | /** @import { AwaitExpression, Expression, Property, SpreadElement } from 'estree' */ |
2 | 2 | /** @import { Context } from '../types' */ |
3 | | -import { dev } from '../../../../state.js'; |
| 3 | +import { dev, is_ignored } from '../../../../state.js'; |
4 | 4 | import * as b from '../../../../utils/builders.js'; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * @param {AwaitExpression} node |
8 | 8 | * @param {Context} context |
9 | 9 | */ |
10 | 10 | export function AwaitExpression(node, context) { |
11 | | - const save = |
12 | | - // preserve context if this is a top-level await in `<script>` |
13 | | - (context.state.is_instance && context.state.scope.function_depth === 1) || |
14 | | - // or if this is a derived/template expression |
15 | | - (is_reactive_expression(context) && !is_last_evaluated_expression(context, node)); |
16 | | - |
17 | | - if (dev || save) { |
18 | | - const expression = /** @type {Expression} */ (context.visit(node.argument)); |
19 | | - return b.call(b.await(b.call('$.save', expression, !save && b.false))); |
| 11 | + const argument = /** @type {Expression} */ (context.visit(node.argument)); |
| 12 | + |
| 13 | + const tla = context.state.is_instance && context.state.scope.function_depth === 1; |
| 14 | + |
| 15 | + // preserve context for |
| 16 | + // a) top-level await and |
| 17 | + // b) awaits that precede other expressions in template or `$derived(...)` |
| 18 | + if (tla || is_reactive_expression(context)) { |
| 19 | + if (tla || !is_last_evaluated_expression(context, node)) { |
| 20 | + return b.call(b.await(b.call('$.save', argument))); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // in dev, note which values are read inside a reactive expression, |
| 25 | + // but don't track them |
| 26 | + else if (dev && !is_ignored(node, 'await_reactivity_loss')) { |
| 27 | + return b.call(b.await(b.call('$.save', argument, b.false))); |
20 | 28 | } |
21 | 29 |
|
22 | | - return context.next(); |
| 30 | + return argument === node.argument ? node : { ...node, argument }; |
23 | 31 | } |
24 | 32 |
|
25 | 33 | /** |
|
0 commit comments