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

Fix imports used in member props #9710

Draft
wants to merge 2 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix imports used in member props
  • Loading branch information
mischnic committed May 9, 2024
commit 9c5f75e77ef40302592b6f7bf0b589a60574bb8a
22 changes: 16 additions & 6 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,30 +681,38 @@ impl Visit for Collect {
match &*node.obj {
Expr::Member(member) => {
if match_member_expr(member, vec!["module", "exports"], self.unresolved_mark) {
// module.exports[...]
handle_export!();
node.prop.visit_with(self);
return;
} else {
member.visit_with(self);
}
}
Expr::Ident(ident) => {
if &*ident.sym == "exports" && is_unresolved(&ident, self.unresolved_mark) {
if &*ident.sym == "exports" && is_unresolved(ident, self.unresolved_mark) {
// exports[...]
handle_export!();
} else if ident.sym == js_word!("module") && is_unresolved(&ident, self.unresolved_mark) {
node.prop.visit_with(self);
return;
} else if ident.sym == js_word!("module") && is_unresolved(ident, self.unresolved_mark) {
// module[...]
self.has_cjs_exports = true;
self.static_cjs_exports = false;
self.should_wrap = true;
self.add_bailout(node.span, BailoutReason::FreeModule);
node.prop.visit_with(self);
return;
} else if match_property_name(node).is_none() {
// something[some_var]
self
.non_static_access
.entry(id!(ident))
.or_default()
.push(node.span);
} else if self.imports.contains_key(&id!(ident)) {
// anImport[...]
self.used_imports.insert(id!(ident));
}
return;
// continue to visit children...
}
Expr::This(_this) => {
if self.in_module_this {
Expand All @@ -716,6 +724,7 @@ impl Visit for Collect {
self.this_exprs.insert(id!(prop), (prop.clone(), node.span));
}
}
node.prop.visit_with(self);
return;
}
_ => {}
Expand Down Expand Up @@ -797,7 +806,8 @@ impl Visit for Collect {
// This visitor helps us identify used imports in cases like:
//
// import { foo } from "bar";
// const baz = { foo };
// { foo }
// something[foo]
if self.imports.contains_key(&id!(node)) {
self.used_imports.insert(id!(node));
}
Expand Down
6 changes: 4 additions & 2 deletions packages/transformers/js/core/src/hoist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ mod tests {

let (collect, _code, _hoist) = parse(
r#"
import { a, b, c, d, e } from "other";
import { a, b, c, d, e, f } from "other";
import * as x from "other";
import * as y from "other";

Expand All @@ -1366,11 +1366,12 @@ mod tests {
log(x);
y.foo();
e.foo.bar();
global[f] = 1;
"#,
);
assert_eq_set!(
collect.used_imports,
set! { w!("a"), w!("b"), w!("c"), w!("e"), w!("x"), w!("y") }
set! { w!("a"), w!("b"), w!("c"), w!("e"), w!("f"), w!("x"), w!("y") }
);
assert_eq_imports!(
collect.imports,
Expand All @@ -1380,6 +1381,7 @@ mod tests {
w!("c") => (w!("other"), w!("c"), false),
w!("d") => (w!("other"), w!("d"), false),
w!("e") => (w!("other"), w!("e"), false),
w!("f") => (w!("other"), w!("f"), false),
w!("x") => (w!("other"), w!("*"), false),
w!("y") => (w!("other"), w!("*"), false)
}
Expand Down
Loading