Skip to content

Commit d178106

Browse files
committed
fix: better optimization of await expressions
1 parent 523c85b commit d178106

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

.changeset/odd-plants-lead.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: better optimization of await expressions

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { walk } from 'zimmerframe';
66
import { parse } from '../1-parse/acorn.js';
77
import * as e from '../../errors.js';
88
import * as w from '../../warnings.js';
9-
import { extract_identifiers } from '../../utils/ast.js';
9+
import { extract_identifiers, has_await_expression } from '../../utils/ast.js';
1010
import * as b from '#compiler/builders';
1111
import { Scope, ScopeRoot, create_scopes, get_rune, set_scope } from '../scope.js';
1212
import check_graph_for_cycles from './utils/check_graph_for_cycles.js';

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import * as b from '#compiler/builders';
1313
import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
1414
import { regex_whitespaces_strict } from '../../../../patterns.js';
15-
import { has_await } from '../../../../../utils/ast.js';
15+
import { has_await_expression } from '../../../../../utils/ast.js';
1616

1717
/** Opens an if/each block, so that we can remove nodes in the case of a mismatch */
1818
export const block_open = b.literal(BLOCK_OPEN);
@@ -315,7 +315,7 @@ export class PromiseOptimiser {
315315

316316
const promises = b.array(
317317
this.expressions.map((expression) => {
318-
return expression.type === 'AwaitExpression' && !has_await(expression.argument)
318+
return expression.type === 'AwaitExpression' && !has_await_expression(expression.argument)
319319
? expression.argument
320320
: b.call(b.thunk(expression, true));
321321
})

packages/svelte/src/compiler/utils/ast.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,20 @@ export function build_assignment_value(operator, left, right) {
611611
}
612612

613613
/**
614-
* @param {ESTree.Expression} expression
614+
* @param {ESTree.Node} node
615615
*/
616-
export function has_await(expression) {
616+
export function has_await_expression(node) {
617617
let has_await = false;
618618

619-
walk(expression, null, {
619+
walk(node, null, {
620620
AwaitExpression(_node, context) {
621621
has_await = true;
622622
context.stop();
623-
}
623+
},
624+
// don't traverse into these
625+
FunctionDeclaration() {},
626+
FunctionExpression() {},
627+
ArrowFunctionExpression() {}
624628
});
625629

626630
return has_await;

packages/svelte/src/compiler/utils/builders.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { walk } from 'zimmerframe';
33
import { regex_is_valid_identifier } from '../phases/patterns.js';
44
import { sanitize_template_string } from './sanitize_template_string.js';
5-
import { has_await } from './ast.js';
5+
import { has_await_expression } from './ast.js';
66

77
/**
88
* @param {Array<ESTree.Expression | ESTree.SpreadElement | null>} elements
@@ -451,7 +451,7 @@ export function thunk(expression, async = false) {
451451
export function unthunk(expression) {
452452
// optimize `async () => await x()`, but not `async () => await x(await y)`
453453
if (expression.async && expression.body.type === 'AwaitExpression') {
454-
if (!has_await(expression.body.argument)) {
454+
if (!has_await_expression(expression.body.argument)) {
455455
return unthunk(arrow(expression.params, expression.body.argument));
456456
}
457457
}

0 commit comments

Comments
 (0)