From bc6255420418d7f46871d70e86b7b97d674f071f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 6 Jan 2024 15:07:51 -0800 Subject: [PATCH] notes --- .../src/compiler/phases/3-transform/client/utils.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 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 c08c135183..86aa472dcd 100644 --- a/packages/svelte/src/compiler/phases/3-transform/client/utils.js +++ b/packages/svelte/src/compiler/phases/3-transform/client/utils.js @@ -543,15 +543,19 @@ export function should_proxy_or_freeze(node) { * @returns {boolean} */ export function can_inline_variable(binding, name) { - // TODO: allow object expressions that are not passed to functions or components as props - // and expressions as long as they do not reference non-hoistable variables return ( !!binding && binding.kind === 'normal' && binding.scope.is_top_level && + // TODO: allow object expressions that are not passed to functions or components as props + // and expressions as long as they do not reference non-hoistable variables + 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 + // E.g. if you have `let x = 0; x++` you could hoist both statements !binding.mutated && !binding.reassigned && - binding.initial?.type === 'Literal' && + // Avoid conflicts. It would be nice to rename the variable, but keeping it simple for now !binding.scope.declared_in_outer_scope(name) && !GlobalBindings.has(name) );