@@ -293,7 +293,7 @@ export function serialize_set_binding(node, context, fallback, options) {
293293 if (
294294 context . state . analysis . runes &&
295295 ! options ?. skip_proxy_and_freeze &&
296- should_proxy_or_freeze ( value )
296+ should_proxy_or_freeze ( value , context . state . scope )
297297 ) {
298298 const assignment = fallback ( ) ;
299299 if ( assignment . type === 'AssignmentExpression' ) {
@@ -310,7 +310,7 @@ export function serialize_set_binding(node, context, fallback, options) {
310310 left ,
311311 context . state . analysis . runes &&
312312 ! options ?. skip_proxy_and_freeze &&
313- should_proxy_or_freeze ( value )
313+ should_proxy_or_freeze ( value , context . state . scope )
314314 ? private_state . kind === 'frozen_state'
315315 ? b . call ( '$.freeze' , value )
316316 : b . call ( '$.proxy' , value )
@@ -330,7 +330,7 @@ export function serialize_set_binding(node, context, fallback, options) {
330330 context . state . analysis . runes &&
331331 public_state !== undefined &&
332332 ! options ?. skip_proxy_and_freeze &&
333- should_proxy_or_freeze ( value )
333+ should_proxy_or_freeze ( value , context . state . scope )
334334 ) {
335335 const assignment = fallback ( ) ;
336336 if ( assignment . type === 'AssignmentExpression' ) {
@@ -398,7 +398,7 @@ export function serialize_set_binding(node, context, fallback, options) {
398398 b . id ( left_name ) ,
399399 context . state . analysis . runes &&
400400 ! options ?. skip_proxy_and_freeze &&
401- should_proxy_or_freeze ( value )
401+ should_proxy_or_freeze ( value , context . state . scope )
402402 ? b . call ( '$.proxy' , value )
403403 : value
404404 ) ;
@@ -408,7 +408,7 @@ export function serialize_set_binding(node, context, fallback, options) {
408408 b . id ( left_name ) ,
409409 context . state . analysis . runes &&
410410 ! options ?. skip_proxy_and_freeze &&
411- should_proxy_or_freeze ( value )
411+ should_proxy_or_freeze ( value , context . state . scope )
412412 ? b . call ( '$.freeze' , value )
413413 : value
414414 ) ;
@@ -623,8 +623,11 @@ export function get_prop_source(binding, state, name, initial) {
623623 return b . call ( '$.prop' , ...args ) ;
624624}
625625
626- /** @param {import('estree').Expression } node */
627- export function should_proxy_or_freeze ( node ) {
626+ /**
627+ * @param {import('estree').Expression } node
628+ * @param {import("../../scope.js").Scope | null } scope
629+ */
630+ export function should_proxy_or_freeze ( node , scope ) {
628631 if (
629632 ! node ||
630633 node . type === 'Literal' ||
@@ -637,5 +640,20 @@ export function should_proxy_or_freeze(node) {
637640 ) {
638641 return false ;
639642 }
643+ if ( node . type === 'Identifier' && scope !== null ) {
644+ const binding = scope . get ( node . name ) ;
645+ // Let's see if the reference is something that can be proxied or frozen
646+ if (
647+ binding !== null &&
648+ ! binding . reassigned &&
649+ binding . initial !== null &&
650+ binding . initial . type !== 'FunctionDeclaration' &&
651+ binding . initial . type !== 'ClassDeclaration' &&
652+ binding . initial . type !== 'ImportDeclaration' &&
653+ binding . initial . type !== 'EachBlock'
654+ ) {
655+ return should_proxy_or_freeze ( binding . initial , null ) ;
656+ }
657+ }
640658 return true ;
641659}
0 commit comments