@@ -293,7 +293,7 @@ export function serialize_set_binding(node, context, fallback, options) {
293
293
if (
294
294
context . state . analysis . runes &&
295
295
! options ?. skip_proxy_and_freeze &&
296
- should_proxy_or_freeze ( value )
296
+ should_proxy_or_freeze ( value , context . state . scope )
297
297
) {
298
298
const assignment = fallback ( ) ;
299
299
if ( assignment . type === 'AssignmentExpression' ) {
@@ -310,7 +310,7 @@ export function serialize_set_binding(node, context, fallback, options) {
310
310
left ,
311
311
context . state . analysis . runes &&
312
312
! options ?. skip_proxy_and_freeze &&
313
- should_proxy_or_freeze ( value )
313
+ should_proxy_or_freeze ( value , context . state . scope )
314
314
? private_state . kind === 'frozen_state'
315
315
? b . call ( '$.freeze' , value )
316
316
: b . call ( '$.proxy' , value )
@@ -330,7 +330,7 @@ export function serialize_set_binding(node, context, fallback, options) {
330
330
context . state . analysis . runes &&
331
331
public_state !== undefined &&
332
332
! options ?. skip_proxy_and_freeze &&
333
- should_proxy_or_freeze ( value )
333
+ should_proxy_or_freeze ( value , context . state . scope )
334
334
) {
335
335
const assignment = fallback ( ) ;
336
336
if ( assignment . type === 'AssignmentExpression' ) {
@@ -398,7 +398,7 @@ export function serialize_set_binding(node, context, fallback, options) {
398
398
b . id ( left_name ) ,
399
399
context . state . analysis . runes &&
400
400
! options ?. skip_proxy_and_freeze &&
401
- should_proxy_or_freeze ( value )
401
+ should_proxy_or_freeze ( value , context . state . scope )
402
402
? b . call ( '$.proxy' , value )
403
403
: value
404
404
) ;
@@ -408,7 +408,7 @@ export function serialize_set_binding(node, context, fallback, options) {
408
408
b . id ( left_name ) ,
409
409
context . state . analysis . runes &&
410
410
! options ?. skip_proxy_and_freeze &&
411
- should_proxy_or_freeze ( value )
411
+ should_proxy_or_freeze ( value , context . state . scope )
412
412
? b . call ( '$.freeze' , value )
413
413
: value
414
414
) ;
@@ -623,8 +623,11 @@ export function get_prop_source(binding, state, name, initial) {
623
623
return b . call ( '$.prop' , ...args ) ;
624
624
}
625
625
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 ) {
628
631
if (
629
632
! node ||
630
633
node . type === 'Literal' ||
@@ -637,5 +640,20 @@ export function should_proxy_or_freeze(node) {
637
640
) {
638
641
return false ;
639
642
}
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
+ }
640
658
return true ;
641
659
}
0 commit comments