Unverified Commit 43b4364f authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Generalize `ImportedBinding` effect creation to avoid as much…

Turbopack: Generalize `ImportedBinding` effect creation to avoid as much special-casing of SimpleAssignTarget (#81653)

There was some logic here that checked for the existence of `SimpleAssignTarget`, and using that to bail out of `visit_ident`. That doesn't scale well to pattern assignments, but it turns out that we don't need to bail out, we just need to fix the codegen.

Without this, https://github.com/vercel/next.js/pull/81654 breaks the test cases.
parent 8659d47c
...@@ -1312,6 +1312,14 @@ impl Analyzer<'_> { ...@@ -1312,6 +1312,14 @@ impl Analyzer<'_> {
} }
impl VisitAstPath for Analyzer<'_> { impl VisitAstPath for Analyzer<'_> {
fn visit_import_specifier<'ast: 'r, 'r>(
&mut self,
_import_specifier: &'ast ImportSpecifier,
_ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
// Skip these nodes entirely: We gather imports in a separate pass
}
fn visit_assign_expr<'ast: 'r, 'r>( fn visit_assign_expr<'ast: 'r, 'r>(
&mut self, &mut self,
n: &'ast AssignExpr, n: &'ast AssignExpr,
...@@ -1837,71 +1845,55 @@ impl VisitAstPath for Analyzer<'_> { ...@@ -1837,71 +1845,55 @@ impl VisitAstPath for Analyzer<'_> {
ident: &'ast Ident, ident: &'ast Ident,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>, ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) { ) {
if !(matches!( // Note: `Ident` is (generally) only used for nodes referencing a variable, as it has scope
ast_path.last(), // information. In other cases (e.g. object literals, properties of member expressions),
Some(AstParentNodeRef::Expr(_, ExprField::Ident)) // `IdentName` is used instead.
| Some(AstParentNodeRef::Prop(_, PropField::Shorthand))
) || matches!(
ast_path.get(ast_path.len() - 2),
Some(AstParentNodeRef::SimpleAssignTarget(
_,
SimpleAssignTargetField::Ident,
))
)) {
return;
}
// Note: The `Ident` children of `ImportSpecifier` are not visited because
// `visit_import_specifier` bails out.
// Attempt to add import effects.
if let Some((esm_reference_index, export)) = if let Some((esm_reference_index, export)) =
self.eval_context.imports.get_binding(&ident.to_id()) self.eval_context.imports.get_binding(&ident.to_id())
{ {
// Optimization: Look for a MemberExpr to see if we only access a few members from the
// module, add those specific effects instead of depending on the entire module.
//
// export.is_none() checks for a namespace import (*).
if export.is_none() if export.is_none()
&& !self && !self
.eval_context .eval_context
.imports .imports
.should_import_all(esm_reference_index) .should_import_all(esm_reference_index)
{ && let Some(AstParentNodeRef::MemberExpr(member, MemberExprField::Obj)) =
// export.is_none() checks for a namespace import.
// Note: This is optimization that can be applied if we don't need to
// import all bindings
if let Some(AstParentNodeRef::MemberExpr(member, MemberExprField::Obj)) =
ast_path.get(ast_path.len() - 2) ast_path.get(ast_path.len() - 2)
{ && let Some(prop) = self.eval_context.eval_member_prop(&member.prop)
// Skip if it's on the LHS of assignment && let Some(prop_str) = prop.as_str()
let is_lhs = matches!( {
ast_path.get(ast_path.len() - 3), // a namespace member access like
Some(AstParentNodeRef::SimpleAssignTarget( // `import * as ns from "..."; ns.exportName`
_, self.add_effect(Effect::ImportedBinding {
SimpleAssignTargetField::Member esm_reference_index,
)) export: Some(prop_str.into()),
); // point to the MemberExpression instead
ast_path: as_parent_path_skip(ast_path, 1),
if !is_lhs span: member.span(),
&& let Some(prop) = self.eval_context.eval_member_prop(&member.prop) in_try: is_in_try(ast_path),
&& let Some(prop_str) = prop.as_str() });
{ } else {
// a namespace member access like self.add_effect(Effect::ImportedBinding {
// `import * as ns from "..."; ns.exportName` esm_reference_index,
self.add_effect(Effect::ImportedBinding { export,
esm_reference_index, ast_path: as_parent_path(ast_path),
export: Some(prop_str.into()), span: ident.span(),
ast_path: as_parent_path_skip(ast_path, 1), in_try: is_in_try(ast_path),
span: member.span(), })
in_try: is_in_try(ast_path),
});
return;
}
}
} }
return;
}
self.add_effect(Effect::ImportedBinding { // If this variable is unresolved, track it as a free (unbound) variable
esm_reference_index, if is_unresolved(ident, self.eval_context.unresolved_mark)
export,
ast_path: as_parent_path(ast_path),
span: ident.span(),
in_try: is_in_try(ast_path),
})
} else if is_unresolved(ident, self.eval_context.unresolved_mark)
|| self.eval_context.force_free_values.contains(&ident.to_id()) || self.eval_context.force_free_values.contains(&ident.to_id())
{ {
self.add_effect(Effect::FreeVar { self.add_effect(Effect::FreeVar {
......
...@@ -149,46 +149,37 @@ impl EsmBinding { ...@@ -149,46 +149,37 @@ impl EsmBinding {
)); ));
break; break;
} }
Some(swc_core::ecma::visit::AstParentKind::BindingIdent( // We need to handle LHS because of code like
swc_core::ecma::visit::fields::BindingIdentField::Id, // (function (RouteKind1){})(RouteKind || RouteKind = {})
)) => { Some(swc_core::ecma::visit::AstParentKind::SimpleAssignTarget(_)) => {
ast_path.pop(); ast_path.pop();
// We need to handle LHS because of code like visitors.push(create_visitor!(
// (function (RouteKind1){})(RouteKind || RouteKind = {}) exact,
if let Some(swc_core::ecma::visit::AstParentKind::SimpleAssignTarget( ast_path,
swc_core::ecma::visit::fields::SimpleAssignTargetField::Ident, visit_mut_simple_assign_target,
)) = ast_path.last() |l: &mut SimpleAssignTarget| {
{ use swc_core::common::Spanned;
ast_path.pop(); match &imported_ident {
ImportedIdent::Module(imported_ident) => {
visitors.push(create_visitor!( *l = imported_ident
exact, .as_expr_individual(l.span())
ast_path, .map_either(
visit_mut_simple_assign_target, |i| SimpleAssignTarget::Ident(i.into()),
|l: &mut SimpleAssignTarget| { SimpleAssignTarget::Member,
use swc_core::common::Spanned; )
match &imported_ident { .into_inner();
ImportedIdent::Module(imported_ident) => { }
*l = imported_ident ImportedIdent::None => {
.as_expr_individual(l.span()) // Do nothing, cannot assign to `undefined`
.map_either( }
|i| SimpleAssignTarget::Ident(i.into()), ImportedIdent::Unresolvable => {
SimpleAssignTarget::Member, // Do nothing, the reference will insert a throw
)
.into_inner();
}
ImportedIdent::None => {
// Do nothing, cannot assign to `undefined`
}
ImportedIdent::Unresolvable => {
// Do nothing, the reference will insert a throw
}
} }
} }
)); }
break; ));
} break;
} }
Some(_) => { Some(_) => {
ast_path.pop(); ast_path.pop();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment