From 623c7c7b4679c820974e49d2f2bc1159c1e7c4e0 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sun, 7 Jan 2024 09:37:22 -0800 Subject: [PATCH] tweak comment --- .../src/compiler/phases/3-transform/client/utils.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/svelte/src/compiler/phases/3-transform/client/utils.js b/packages/svelte/src/compiler/phases/3-transform/client/utils.js index cebd784f26..26cc1cadd2 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -552,17 +552,17 @@ export function can_inline_variable(binding, name) { * @returns {boolean} */ export function can_hoist_declaration(binding, name) { + // We cannot hoist functions or constructors because they could be non-deterministic + // (i.e. `Math.random()`) or have side-effects (e.g. `increment()`). We also cannot hoist + // anything that references a non-hoistable variable. return ( !!binding && binding.kind === 'normal' && binding.scope.is_top_level && binding.scope.has_parent() && // i.e. not when context="module" - // For now we just allow primitives for simplicity. We could allow object expressions that are - // not passed to functions or components as props and expressions as long as they do not - // reference functions, constructors, non-hoistable variables, etc. + // For now we just allow primitives for simplicity binding.initial?.type === 'Literal' && - // Checking that it's not mutated or reassigned is a bit simplistic - // If it's not state and thus not reactive, we could hoist the variable and mutation + // For now, we just check that it's not mutated or reassigned for simplicity // E.g. if you have `let x = 0; x++` you could hoist both statements !binding.mutated && !binding.reassigned &&