Skip to content

Commit 77f91fd

Browse files
committed
Add function boundary optimization
1 parent 9b01074 commit 77f91fd

File tree

1 file changed

+20
-8
lines changed
  • packages/svelte/src/compiler/phases/3-transform/client

1 file changed

+20
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,28 @@ export function serialize_set_binding(node, context, fallback) {
248248
return fallback();
249249
}
250250

251+
/** Potential optimization: find nearest function boundary and if it is not async, the assignment can't be async */
252+
const closest_function_boundary =
253+
/** @type {import('estree').BaseFunction | undefined} */
254+
(
255+
context.path.findLast(
256+
(node) =>
257+
node.type === 'FunctionDeclaration' ||
258+
node.type === 'FunctionExpression' ||
259+
node.type === 'ArrowFunctionExpression'
260+
)
261+
);
262+
const closest_function_boundary_not_async =
263+
closest_function_boundary === undefined || closest_function_boundary.async !== true;
264+
251265
const rhs_expression = /** @type {import('estree').Expression} */ (visit(node.right));
266+
252267
const iife_is_async =
268+
!closest_function_boundary_not_async ||
253269
is_expression_async(rhs_expression) ||
254270
assignments.some((assignment) => is_expression_async(assignment));
255271

256-
let iife = b.arrow(
272+
const iife = b.arrow(
257273
[],
258274
b.block([
259275
b.const(tmp_id, rhs_expression),
@@ -263,16 +279,12 @@ export function serialize_set_binding(node, context, fallback) {
263279
b.return(b.id(tmp_id))
264280
])
265281
);
266-
if (iife_is_async) {
267-
iife = b.async(iife);
268-
}
269282

270-
let iife_call = /** @type {import('estree').Expression} */ (b.call(iife));
271283
if (iife_is_async) {
272-
iife_call = b.await(iife_call);
284+
return b.await(b.call(b.async(iife)));
285+
} else {
286+
return b.call(iife);
273287
}
274-
275-
return iife_call;
276288
}
277289

278290
if (node.left.type !== 'Identifier' && node.left.type !== 'MemberExpression') {

0 commit comments

Comments
 (0)