Skip to content

Commit d92bd8a

Browse files
committed
handle async functions
1 parent e6bb46a commit d92bd8a

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/BlockStatement.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { BlockStatement, Expression, Statement } from 'estree' */
1+
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression, Expression, FunctionDeclaration, FunctionExpression, Statement } from 'estree' */
22
/** @import { ComponentContext } from '../types' */
33
import { add_state_transformers } from './shared/declarations.js';
44
import * as b from '../../../../utils/builders.js';
@@ -12,15 +12,20 @@ export function BlockStatement(node, context) {
1212
const tracing = context.state.scope.tracing;
1313

1414
if (tracing !== null) {
15-
return b.block([
16-
b.return(
17-
b.call(
18-
'$.trace',
19-
/** @type {Expression} */ (context.visit(tracing)),
20-
b.thunk(b.block(node.body.map((n) => /** @type {Statement} */ (context.visit(n)))))
21-
)
22-
)
23-
]);
15+
const parent =
16+
/** @type {ArrowFunctionExpression | FunctionDeclaration | FunctionExpression} */ (
17+
context.path.at(-1)
18+
);
19+
20+
const is_async = parent.async;
21+
22+
const call = b.call(
23+
'$.trace',
24+
/** @type {Expression} */ (tracing),
25+
b.thunk(b.block(node.body.map((n) => /** @type {Statement} */ (context.visit(n)))), is_async)
26+
);
27+
28+
return b.block([b.return(is_async ? b.await(call) : call)]);
2429
}
2530

2631
context.next();

0 commit comments

Comments
 (0)