Skip to content

Commit 2703ac6

Browse files
committed
fix heuristic for transforming await expressions on server
1 parent bcdddc6 commit 2703ac6

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/AwaitExpression.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ import * as b from '../../../../utils/builders.js';
77
* @param {Context} context
88
*/
99
export function AwaitExpression(node, context) {
10-
if (context.state.scope.function_depth > 1) {
10+
// if `await` is inside a function, or inside `<script module>`,
11+
// allow it, otherwise error
12+
if (
13+
context.state.scope.function_depth === 0 ||
14+
context.path.some(
15+
(node) =>
16+
node.type === 'ArrowFunctionExpression' ||
17+
node.type === 'FunctionDeclaration' ||
18+
node.type === 'FunctionExpression'
19+
)
20+
) {
1121
return context.next();
1222
}
1323

0 commit comments

Comments
 (0)