hoist-unmodified-var
Ben McCann 2 years ago
parent acb4455726
commit bc62554204

@ -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)
);

Loading…
Cancel
Save