@@ -7,11 +7,11 @@ use oxc_ecmascript::{
77 } ,
88 side_effects:: { MayHaveSideEffects , PropertyReadSideEffects } ,
99} ;
10- use oxc_semantic:: { IsGlobalReference , Scoping } ;
10+ use oxc_semantic:: { IsGlobalReference , Scoping , SymbolId } ;
1111use oxc_span:: format_atom;
1212use oxc_syntax:: reference:: ReferenceId ;
1313
14- use crate :: { options:: CompressOptions , state:: MinifierState } ;
14+ use crate :: { options:: CompressOptions , state:: MinifierState , symbol_value :: SymbolValue } ;
1515
1616pub type TraverseCtx < ' a > = oxc_traverse:: TraverseCtx < ' a , MinifierState < ' a > > ;
1717
@@ -50,7 +50,7 @@ impl<'a> oxc_ecmascript::is_global_reference::IsGlobalReference<'a> for Ctx<'a,
5050 self . scoping ( )
5151 . get_reference ( reference_id)
5252 . symbol_id ( )
53- . and_then ( |symbol_id| self . state . constant_values . get ( & symbol_id) )
53+ . and_then ( |symbol_id| self . state . symbol_values . get_constant_value ( symbol_id) )
5454 . cloned ( )
5555 }
5656}
@@ -164,6 +164,46 @@ impl<'a> Ctx<'a, '_> {
164164 false
165165 }
166166
167+ pub fn init_value ( & mut self , symbol_id : SymbolId , constant : Option < ConstantValue < ' a > > ) {
168+ let mut exported = false ;
169+ if self . scoping . current_scope_id ( ) == self . scoping ( ) . root_scope_id ( ) {
170+ for ancestor in self . ancestors ( ) {
171+ if ancestor. is_export_named_declaration ( )
172+ || ancestor. is_export_all_declaration ( )
173+ || ancestor. is_export_default_declaration ( )
174+ {
175+ exported = true ;
176+ }
177+ }
178+ }
179+
180+ let for_statement_init = self . ancestors ( ) . nth ( 1 ) . is_some_and ( |ancestor| {
181+ ancestor. is_parent_of_for_statement_init ( ) || ancestor. is_parent_of_for_statement_left ( )
182+ } ) ;
183+
184+ let mut read_references_count = 0 ;
185+ let mut write_references_count = 0 ;
186+ for r in self . scoping ( ) . get_resolved_references ( symbol_id) {
187+ if r. is_read ( ) {
188+ read_references_count += 1 ;
189+ }
190+ if r. is_write ( ) {
191+ write_references_count += 1 ;
192+ }
193+ }
194+
195+ let scope_id = self . scoping . current_scope_id ( ) ;
196+ let symbol_value = SymbolValue {
197+ constant,
198+ exported,
199+ for_statement_init,
200+ read_references_count,
201+ write_references_count,
202+ scope_id,
203+ } ;
204+ self . state . symbol_values . init_value ( symbol_id, symbol_value) ;
205+ }
206+
167207 /// If two expressions are equal.
168208 /// Special case `undefined` == `void 0`
169209 pub fn expr_eq ( & self , a : & Expression < ' a > , b : & Expression < ' a > ) -> bool {
0 commit comments