@@ -4,6 +4,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
44use swc_atoms:: atom;
55use swc_common:: { util:: take:: Take , EqIgnoreSpan , Mark , NodeId } ;
66use swc_ecma_ast:: * ;
7+ use swc_ecma_transforms_base:: resolve:: RefTo ;
78use swc_ecma_usage_analyzer:: alias:: { collect_infects_from, AliasConfig } ;
89use swc_ecma_utils:: {
910 class_has_side_effect, collect_decls, contains_this_expr, find_pat_ids, ExprExt , Remapper ,
@@ -214,17 +215,21 @@ impl Optimizer<'_> {
214215 Expr :: Ident ( Ident { sym, .. } ) if & * * sym == "eval" => false ,
215216
216217 Expr :: Ident ( id) if !id. eq_ignore_span ( ident) => {
217- let node_id = self . r . find_binding_by_ident ( id) ;
218- debug_assert ! ( id. node_id != node_id) ;
218+ let node_id = match self . r . find_binding_by_ident ( id) {
219+ RefTo :: Binding ( node_id) => Some ( node_id) ,
220+ RefTo :: Unresolved => None ,
221+ RefTo :: Itself => unreachable ! ( ) ,
222+ } ;
219223
220224 if !usage. flags . contains ( VarUsageInfoFlags :: ASSIGNED_FN_LOCAL ) {
221225 false
222- } else if let Some ( u) = self . data . vars . get ( & node_id) {
223- let mut should_inline =
224- !u. flags . contains ( VarUsageInfoFlags :: REASSIGNED )
225- && u. flags . contains ( VarUsageInfoFlags :: DECLARED ) ;
226+ } else if let Some ( node_id) = node_id {
227+ if let Some ( u) = self . data . vars . get ( & node_id) {
228+ let mut should_inline =
229+ !u. flags . contains ( VarUsageInfoFlags :: REASSIGNED )
230+ && u. flags . contains ( VarUsageInfoFlags :: DECLARED ) ;
226231
227- should_inline &=
232+ should_inline &=
228233 // Function declarations are hoisted
229234 //
230235 // As we copy expressions, this can cause a problem.
@@ -235,33 +240,36 @@ impl Optimizer<'_> {
235240 || !u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_DECL )
236241 || usage. callee_count == 0 ;
237242
238- if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FOR_INIT )
239- && !usage. flags . contains ( VarUsageInfoFlags :: IS_FN_LOCAL )
240- {
241- should_inline &= !matches ! (
242- u. var_kind,
243- Some ( VarDeclKind :: Let | VarDeclKind :: Const )
244- )
245- }
246-
247- if u. flags . intersects (
248- VarUsageInfoFlags :: DECLARED_AS_FN_DECL
249- . union ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) ,
250- ) {
251- if self . options . keep_fnames
252- || self . mangle_options . is_some_and ( |v| v. keep_fn_names )
243+ if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FOR_INIT )
244+ && !usage. flags . contains ( VarUsageInfoFlags :: IS_FN_LOCAL )
253245 {
254- should_inline = false
246+ should_inline &= !matches ! (
247+ u. var_kind,
248+ Some ( VarDeclKind :: Let | VarDeclKind :: Const )
249+ )
255250 }
256- }
257251
258- if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) {
259- if self . options . inline != 3 {
260- return ;
252+ if u. flags . intersects (
253+ VarUsageInfoFlags :: DECLARED_AS_FN_DECL
254+ . union ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) ,
255+ ) {
256+ if self . options . keep_fnames
257+ || self . mangle_options . is_some_and ( |v| v. keep_fn_names )
258+ {
259+ should_inline = false
260+ }
261+ }
262+
263+ if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) {
264+ if self . options . inline != 3 {
265+ return ;
266+ }
261267 }
262- }
263268
264- should_inline
269+ should_inline
270+ } else {
271+ false
272+ }
265273 } else {
266274 false
267275 }
@@ -321,7 +329,11 @@ impl Optimizer<'_> {
321329 } = * * usage;
322330 let mut inc_usage = || {
323331 if let Expr :: Ident ( i) = & * init {
324- let node_id = self . r . find_binding_by_ident ( i) ;
332+ let node_id = match self . r . find_binding_by_ident ( i) {
333+ RefTo :: Binding ( node_id) => node_id,
334+ RefTo :: Unresolved => return ,
335+ RefTo :: Itself => unreachable ! ( ) ,
336+ } ;
325337 debug_assert ! ( i. node_id != node_id) ;
326338 if let Some ( u) = self . data . vars . get_mut ( & node_id) {
327339 u. flags |= flags & VarUsageInfoFlags :: USED_AS_ARG ;
@@ -472,8 +484,11 @@ impl Optimizer<'_> {
472484
473485 Expr :: Object ( ..) if self . options . pristine_globals => {
474486 for id in idents_used_by_ignoring_nested ( init) {
475- let node_id = self . r . find_binding_by_node_id ( id) ;
476- debug_assert ! ( node_id != id) ;
487+ let node_id = match self . r . find_binding_by_node_id ( id) {
488+ RefTo :: Binding ( node_id) => node_id,
489+ RefTo :: Unresolved => continue ,
490+ RefTo :: Itself => unreachable ! ( ) ,
491+ } ;
477492 if let Some ( v_usage) = self . data . vars . get ( & id) {
478493 if v_usage. flags . contains ( VarUsageInfoFlags :: REASSIGNED ) {
479494 return ;
@@ -520,8 +535,11 @@ impl Optimizer<'_> {
520535
521536 _ => {
522537 for id in idents_used_by ( init) {
523- let node_id = self . r . find_binding_by_node_id ( id) ;
524- debug_assert ! ( node_id != id) ;
538+ let node_id = match self . r . find_binding_by_node_id ( id) {
539+ RefTo :: Binding ( node_id) => node_id,
540+ RefTo :: Unresolved => continue ,
541+ RefTo :: Itself => unreachable ! ( ) ,
542+ } ;
525543 if let Some ( v_usage) = self . data . vars . get ( & node_id) {
526544 if v_usage. property_mutation_count > usage. property_mutation_count
527545 || v_usage. flags . intersects (
@@ -905,8 +923,11 @@ impl Optimizer<'_> {
905923 }
906924 }
907925 Expr :: Ident ( i) => {
908- let node_id = self . r . find_binding_by_ident ( i) ;
909- debug_assert ! ( i. node_id != node_id) ;
926+ let node_id = match self . r . find_binding_by_ident ( i) {
927+ RefTo :: Binding ( node_id) => node_id,
928+ RefTo :: Unresolved => return ,
929+ RefTo :: Itself => unreachable ! ( ) ,
930+ } ;
910931
911932 if let Some ( mut value) = self
912933 . vars
0 commit comments