@@ -2,6 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
22use swc_atoms:: atom;
33use swc_common:: { util:: take:: Take , EqIgnoreSpan , Mark , NodeId } ;
44use swc_ecma_ast:: * ;
5+ use swc_ecma_transforms_base:: resolve:: RefTo ;
56use swc_ecma_usage_analyzer:: alias:: { collect_infects_from, AliasConfig } ;
67use swc_ecma_utils:: {
78 class_has_side_effect, collect_decls, contains_this_expr, find_pat_ids, ExprExt , Remapper ,
@@ -212,17 +213,21 @@ impl Optimizer<'_> {
212213 Expr :: Ident ( Ident { sym, .. } ) if & * * sym == "eval" => false ,
213214
214215 Expr :: Ident ( id) if !id. eq_ignore_span ( ident) => {
215- let node_id = self . r . find_binding_by_ident ( id) ;
216- debug_assert ! ( id. node_id != node_id) ;
216+ let node_id = match self . r . find_binding_by_ident ( id) {
217+ RefTo :: Binding ( node_id) => Some ( node_id) ,
218+ RefTo :: Unresolved => None ,
219+ RefTo :: Itself => unreachable ! ( ) ,
220+ } ;
217221
218222 if !usage. flags . contains ( VarUsageInfoFlags :: ASSIGNED_FN_LOCAL ) {
219223 false
220- } else if let Some ( u) = self . data . vars . get ( & node_id) {
221- let mut should_inline =
222- !u. flags . contains ( VarUsageInfoFlags :: REASSIGNED )
223- && u. flags . contains ( VarUsageInfoFlags :: DECLARED ) ;
224+ } else if let Some ( node_id) = node_id {
225+ if let Some ( u) = self . data . vars . get ( & node_id) {
226+ let mut should_inline =
227+ !u. flags . contains ( VarUsageInfoFlags :: REASSIGNED )
228+ && u. flags . contains ( VarUsageInfoFlags :: DECLARED ) ;
224229
225- should_inline &=
230+ should_inline &=
226231 // Function declarations are hoisted
227232 //
228233 // As we copy expressions, this can cause a problem.
@@ -233,33 +238,36 @@ impl Optimizer<'_> {
233238 || !u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_DECL )
234239 || usage. callee_count == 0 ;
235240
236- if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FOR_INIT )
237- && !usage. flags . contains ( VarUsageInfoFlags :: IS_FN_LOCAL )
238- {
239- should_inline &= !matches ! (
240- u. var_kind,
241- Some ( VarDeclKind :: Let | VarDeclKind :: Const )
242- )
243- }
244-
245- if u. flags . intersects (
246- VarUsageInfoFlags :: DECLARED_AS_FN_DECL
247- . union ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) ,
248- ) {
249- if self . options . keep_fnames
250- || self . mangle_options . is_some_and ( |v| v. keep_fn_names )
241+ if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FOR_INIT )
242+ && !usage. flags . contains ( VarUsageInfoFlags :: IS_FN_LOCAL )
251243 {
252- should_inline = false
244+ should_inline &= !matches ! (
245+ u. var_kind,
246+ Some ( VarDeclKind :: Let | VarDeclKind :: Const )
247+ )
253248 }
254- }
255249
256- if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) {
257- if self . options . inline != 3 {
258- return ;
250+ if u. flags . intersects (
251+ VarUsageInfoFlags :: DECLARED_AS_FN_DECL
252+ . union ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) ,
253+ ) {
254+ if self . options . keep_fnames
255+ || self . mangle_options . is_some_and ( |v| v. keep_fn_names )
256+ {
257+ should_inline = false
258+ }
259+ }
260+
261+ if u. flags . contains ( VarUsageInfoFlags :: DECLARED_AS_FN_EXPR ) {
262+ if self . options . inline != 3 {
263+ return ;
264+ }
259265 }
260- }
261266
262- should_inline
267+ should_inline
268+ } else {
269+ false
270+ }
263271 } else {
264272 false
265273 }
@@ -319,7 +327,11 @@ impl Optimizer<'_> {
319327 } = * * usage;
320328 let mut inc_usage = || {
321329 if let Expr :: Ident ( i) = & * init {
322- let node_id = self . r . find_binding_by_ident ( i) ;
330+ let node_id = match self . r . find_binding_by_ident ( i) {
331+ RefTo :: Binding ( node_id) => node_id,
332+ RefTo :: Unresolved => return ,
333+ RefTo :: Itself => unreachable ! ( ) ,
334+ } ;
323335 debug_assert ! ( i. node_id != node_id) ;
324336 if let Some ( u) = self . data . vars . get_mut ( & node_id) {
325337 u. flags |= flags & VarUsageInfoFlags :: USED_AS_ARG ;
@@ -470,8 +482,11 @@ impl Optimizer<'_> {
470482
471483 Expr :: Object ( ..) if self . options . pristine_globals => {
472484 for id in idents_used_by_ignoring_nested ( init) {
473- let node_id = self . r . find_binding_by_node_id ( id) ;
474- debug_assert ! ( node_id != id) ;
485+ let node_id = match self . r . find_binding_by_node_id ( id) {
486+ RefTo :: Binding ( node_id) => node_id,
487+ RefTo :: Unresolved => continue ,
488+ RefTo :: Itself => unreachable ! ( ) ,
489+ } ;
475490 if let Some ( v_usage) = self . data . vars . get ( & id) {
476491 if v_usage. flags . contains ( VarUsageInfoFlags :: REASSIGNED ) {
477492 return ;
@@ -518,8 +533,11 @@ impl Optimizer<'_> {
518533
519534 _ => {
520535 for id in idents_used_by ( init) {
521- let node_id = self . r . find_binding_by_node_id ( id) ;
522- debug_assert ! ( node_id != id) ;
536+ let node_id = match self . r . find_binding_by_node_id ( id) {
537+ RefTo :: Binding ( node_id) => node_id,
538+ RefTo :: Unresolved => continue ,
539+ RefTo :: Itself => unreachable ! ( ) ,
540+ } ;
523541 if let Some ( v_usage) = self . data . vars . get ( & node_id) {
524542 if v_usage. property_mutation_count > usage. property_mutation_count
525543 || v_usage. flags . intersects (
@@ -901,8 +919,11 @@ impl Optimizer<'_> {
901919 }
902920 }
903921 Expr :: Ident ( i) => {
904- let node_id = self . r . find_binding_by_ident ( i) ;
905- debug_assert ! ( i. node_id != node_id) ;
922+ let node_id = match self . r . find_binding_by_ident ( i) {
923+ RefTo :: Binding ( node_id) => node_id,
924+ RefTo :: Unresolved => return ,
925+ RefTo :: Itself => unreachable ! ( ) ,
926+ } ;
906927
907928 if let Some ( mut value) = self
908929 . vars
0 commit comments