diff --git a/packages/transformers/js/core/src/hoist.rs b/packages/transformers/js/core/src/hoist.rs index 6e43a88910d..af55e1b3e61 100644 --- a/packages/transformers/js/core/src/hoist.rs +++ b/packages/transformers/js/core/src/hoist.rs @@ -1266,13 +1266,15 @@ impl Visit for Collect { self.in_module_this = false; if let Some(bailouts) = &mut self.bailouts { - for key in self.imports.keys() { - if let Some(spans) = self.non_static_access.get(key) { - for span in spans { - bailouts.push(Bailout { - loc: SourceLocation::from(&self.source_map, *span), - reason: BailoutReason::NonStaticAccess, - }) + for (key, Import { specifier, .. }) in &self.imports { + if specifier == "*" { + if let Some(spans) = self.non_static_access.get(key) { + for span in spans { + bailouts.push(Bailout { + loc: SourceLocation::from(&self.source_map, *span), + reason: BailoutReason::NonStaticAccess, + }) + } } } } @@ -1665,8 +1667,7 @@ impl Visit for Collect { self.add_bailout(node.span, BailoutReason::FreeModule); } - // `import` isn't really an identifier... - if match_property_name(node).is_none() && ident.sym != js_word!("import") { + if match_property_name(node).is_none() { self .non_static_access .entry(id!(ident)) @@ -1740,14 +1741,11 @@ impl Visit for Collect { } } - // `import` isn't really an identifier... - if ident.sym != js_word!("import") { - self - .non_static_access - .entry(id!(ident)) - .or_default() - .push(ident.span); - } + self + .non_static_access + .entry(id!(ident)) + .or_default() + .push(ident.span); } _ => { node.visit_children_with(self); @@ -2152,7 +2150,7 @@ mod tests { collect_decls(&module), Mark::fresh(Mark::root()), global_mark, - false, + true, ); module.visit_with(&mut collect); @@ -2738,7 +2736,7 @@ mod tests { #[test] fn fold_import() { - let (_collect, code, _hoist) = parse( + let (collect, code, _hoist) = parse( r#" import {foo as bar} from 'other'; let test = {bar: 3}; @@ -2747,6 +2745,8 @@ mod tests { "#, ); + assert!(collect.bailouts.unwrap().is_empty()); + assert_eq!( code, indoc! {r#" @@ -2759,13 +2759,14 @@ mod tests { "#} ); - let (_collect, code, _hoist) = parse( + let (collect, code, _hoist) = parse( r#" import * as foo from 'other'; console.log(foo.bar); foo.bar(); "#, ); + assert!(collect.bailouts.unwrap().is_empty()); assert_eq!( code, @@ -2776,7 +2777,35 @@ mod tests { "#} ); - let (_collect, code, _hoist) = parse( + let (collect, code, _hoist) = parse( + r#" + import * as foo from 'other'; + foo.bar(); + let y = "bar"; + foo[y](); + "#, + ); + assert_eq!( + collect + .bailouts + .unwrap() + .iter() + .map(|b| &b.reason) + .collect::>(), + vec![&BailoutReason::NonStaticAccess] + ); + + assert_eq!( + code, + indoc! {r#" + import "abc:other"; + $abc$import$70a00e0a8474f72a.bar(); + let $abc$var$y = "bar"; + $abc$import$70a00e0a8474f72a[$abc$var$y](); + "#} + ); + + let (collect, code, _hoist) = parse( r#" import other from 'other'; console.log(other, other.bar); @@ -2784,6 +2813,8 @@ mod tests { "#, ); + assert!(collect.bailouts.unwrap().is_empty()); + assert_eq!( code, indoc! {r#" diff --git a/packages/transformers/js/core/src/utils.rs b/packages/transformers/js/core/src/utils.rs index 113aa35c091..8d65d20f3e9 100644 --- a/packages/transformers/js/core/src/utils.rs +++ b/packages/transformers/js/core/src/utils.rs @@ -275,6 +275,7 @@ pub enum SourceType { Module, } +#[derive(Debug)] pub struct Bailout { pub loc: SourceLocation, pub reason: BailoutReason, @@ -297,6 +298,7 @@ impl Bailout { } } +#[derive(Debug, Eq, PartialEq)] pub enum BailoutReason { NonTopLevelRequire, NonStaticDestructuring,