Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit non static import bailout warnings only for * #8136

Merged
merged 2 commits into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 52 additions & 21 deletions packages/transformers/js/core/src/hoist.rs
Expand Up @@ -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,
})
}
}
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2152,7 +2150,7 @@ mod tests {
collect_decls(&module),
Mark::fresh(Mark::root()),
global_mark,
false,
true,
);
module.visit_with(&mut collect);

Expand Down Expand Up @@ -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};
Expand All @@ -2747,6 +2745,8 @@ mod tests {
"#,
);

assert!(collect.bailouts.unwrap().is_empty());

assert_eq!(
code,
indoc! {r#"
Expand All @@ -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,
Expand All @@ -2776,14 +2777,44 @@ 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<_>>(),
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);
other();
"#,
);

assert!(collect.bailouts.unwrap().is_empty());

assert_eq!(
code,
indoc! {r#"
Expand Down
2 changes: 2 additions & 0 deletions packages/transformers/js/core/src/utils.rs
Expand Up @@ -275,6 +275,7 @@ pub enum SourceType {
Module,
}

#[derive(Debug)]
pub struct Bailout {
pub loc: SourceLocation,
pub reason: BailoutReason,
Expand All @@ -297,6 +298,7 @@ impl Bailout {
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum BailoutReason {
NonTopLevelRequire,
NonStaticDestructuring,
Expand Down