Skip to content
Merged
Changes from all commits
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
34 changes: 17 additions & 17 deletions turbopack/crates/turbopack-ecmascript/src/analyzer/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,17 +1223,26 @@ impl Visit for Analyzer<'_> {
}

fn visit_member_expr(&mut self, node: &MemberExpr) {
if let MemberProp::Ident(..) | MemberProp::PrivateName(..) = &node.prop
&& node.obj.is_ident()
if matches!(
&node.prop,
MemberProp::Ident(..) | MemberProp::PrivateName(..)
) && let Expr::Ident(ident) = &*node.obj
{
// Skip traversing if obj is a Expr::Ident, so that it doesn't get added to
// Intentionally skipping over visit_expr(node.obj) here so that it doesn't get added to
// full_star_imports below in visit_expr.
ident.visit_with(self);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The visit_member_expr implementation skips node.prop when the condition is met. This means that identifiers or private names in the property position are not visited, which can lead to incorrect analysis (e.g., failing to mark them as used). You should explicitly visit node.prop.

Suggested change
ident.visit_with(self);
ident.visit_with(self);
node.prop.visit_with(self);

} else {
node.visit_children_with(self);
}
}

// TODO this currently doesn't properly mark the import in self.program_decl_usage, see
// todo in
// turbopack/crates/turbopack-tests/tests/execution/turbopack/remove-unused-imports/
// import-star/input/index.js
return;
fn visit_expr(&mut self, node: &Expr) {
// Careful about adding anything here, visit_member_expr might skip over this method for
// some Expr::Ident-s.
if let Expr::Ident(i) = node
&& let Some(module_path) = self.namespace_imports_to_specifier.get(&i.to_id())
{
self.data.full_star_imports.insert(module_path.clone());
}
node.visit_children_with(self);
}
Expand All @@ -1258,15 +1267,6 @@ impl Visit for Analyzer<'_> {
node.visit_children_with(self);
}

fn visit_expr(&mut self, node: &Expr) {
if let Expr::Ident(i) = node
&& let Some(module_path) = self.namespace_imports_to_specifier.get(&i.to_id())
{
self.data.full_star_imports.insert(module_path.clone());
}
node.visit_children_with(self);
}

fn visit_ident(&mut self, node: &Ident) {
let id = node.to_id();
if let Some((esm_reference_index, _)) = self.data.get_binding(&id) {
Expand Down
Loading