diff --git a/turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs b/turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs index 8680671b6c37c..1bc3ed1ebb98f 100644 --- a/turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs +++ b/turbopack/crates/turbopack-ecmascript/src/analyzer/graph.rs @@ -43,31 +43,31 @@ impl EffectsBlock { #[derive(Debug, Clone)] pub enum ConditionalKind { /// The blocks of an `if` statement without an `else` block. - If { then: EffectsBlock }, + If { then: Box }, /// The blocks of an `if ... else` or `if { ... return ... } ...` statement. IfElse { - then: EffectsBlock, - r#else: EffectsBlock, + then: Box, + r#else: Box, }, /// The blocks of an `if ... else` statement. - Else { r#else: EffectsBlock }, + Else { r#else: Box }, /// The blocks of an `if { ... return ... } else { ... } ...` or `if { ... } /// else { ... return ... } ...` statement. IfElseMultiple { - then: Vec, - r#else: Vec, + then: Vec>, + r#else: Vec>, }, /// The expressions on the right side of the `?:` operator. Ternary { - then: EffectsBlock, - r#else: EffectsBlock, + then: Box, + r#else: Box, }, /// The expression on the right side of the `&&` operator. - And { expr: EffectsBlock }, + And { expr: Box }, /// The expression on the right side of the `||` operator. - Or { expr: EffectsBlock }, + Or { expr: Box }, /// The expression on the right side of the `??` operator. - NullishCoalescing { expr: EffectsBlock }, + NullishCoalescing { expr: Box }, } impl ConditionalKind { @@ -132,7 +132,7 @@ pub enum Effect { /// condition evaluates to some compile-time constant, we can use that /// to determine which effects are executed and remove the others. Conditional { - condition: JsValue, + condition: Box, kind: Box, /// The ast path to the condition. ast_path: Vec, @@ -141,7 +141,7 @@ pub enum Effect { }, /// A function call or a new call of a function. Call { - func: JsValue, + func: Box, args: Vec, ast_path: Vec, span: Span, @@ -150,8 +150,8 @@ pub enum Effect { }, /// A function call or a new call of a property of an object. MemberCall { - obj: JsValue, - prop: JsValue, + obj: Box, + prop: Box, args: Vec, ast_path: Vec, span: Span, @@ -160,8 +160,8 @@ pub enum Effect { }, /// A property access. Member { - obj: JsValue, - prop: JsValue, + obj: Box, + prop: Box, ast_path: Vec, span: Span, in_try: bool, @@ -176,14 +176,14 @@ pub enum Effect { }, /// A reference to a free var access. FreeVar { - var: JsValue, + var: Box, ast_path: Vec, span: Span, in_try: bool, }, /// A typeof expression TypeOf { - arg: JsValue, + arg: Box, ast_path: Vec, span: Span, }, @@ -720,9 +720,9 @@ enum EarlyReturn { prev_effects: Vec, start_ast_path: Vec, - condition: JsValue, - then: Option, - r#else: Option, + condition: Box, + then: Option>, + r#else: Option>, /// The ast path to the condition. condition_ast_path: Vec, span: Span, @@ -1127,7 +1127,7 @@ impl Analyzer<'_> { match callee { Callee::Import(_) => { self.add_effect(Effect::Call { - func: JsValue::FreeVar(js_word!("import")), + func: Box::new(JsValue::FreeVar(js_word!("import"))), args, ast_path: as_parent_path(ast_path), span, @@ -1137,15 +1137,15 @@ impl Analyzer<'_> { } Callee::Expr(box expr) => { if let Expr::Member(MemberExpr { obj, prop, .. }) = unparen(expr) { - let obj_value = self.eval_context.eval(obj); + let obj_value = Box::new(self.eval_context.eval(obj)); let prop_value = match prop { // TODO avoid clone - MemberProp::Ident(i) => i.sym.clone().into(), + MemberProp::Ident(i) => Box::new(i.sym.clone().into()), MemberProp::PrivateName(_) => { return; } MemberProp::Computed(ComputedPropName { expr, .. }) => { - self.eval_context.eval(expr) + Box::new(self.eval_context.eval(expr)) } }; self.add_effect(Effect::MemberCall { @@ -1158,7 +1158,7 @@ impl Analyzer<'_> { new, }); } else { - let fn_value = self.eval_context.eval(expr); + let fn_value = Box::new(self.eval_context.eval(expr)); self.add_effect(Effect::Call { func: fn_value, args, @@ -1170,10 +1170,11 @@ impl Analyzer<'_> { } } Callee::Super(_) => self.add_effect(Effect::Call { - func: self - .eval_context - // Unwrap because `new super(..)` isn't valid anyway - .eval(&Expr::Call(n.as_call().unwrap().clone())), + func: Box::new( + self.eval_context + // Unwrap because `new super(..)` isn't valid anyway + .eval(&Expr::Call(n.as_call().unwrap().clone())), + ), args, ast_path: as_parent_path(ast_path), span, @@ -1188,14 +1189,16 @@ impl Analyzer<'_> { member_expr: &'ast MemberExpr, ast_path: &AstNodePath>, ) { - let obj_value = self.eval_context.eval(&member_expr.obj); + let obj_value = Box::new(self.eval_context.eval(&member_expr.obj)); let prop_value = match &member_expr.prop { // TODO avoid clone - MemberProp::Ident(i) => i.sym.clone().into(), + MemberProp::Ident(i) => Box::new(i.sym.clone().into()), MemberProp::PrivateName(_) => { return; } - MemberProp::Computed(ComputedPropName { expr, .. }) => self.eval_context.eval(expr), + MemberProp::Computed(ComputedPropName { expr, .. }) => { + Box::new(self.eval_context.eval(expr)) + } }; self.add_effect(Effect::Member { obj: obj_value, @@ -1241,10 +1244,10 @@ impl Analyzer<'_> { in_try, early_return_condition_value, } => { - let block = EffectsBlock { + let block = Box::new(EffectsBlock { effects: take(&mut self.effects), range: AstPathRange::StartAfter(start_ast_path), - }; + }); self.effects = prev_effects; let kind = match (then, r#else, early_return_condition_value) { (None, None, false) => ConditionalKind::If { then: block }, @@ -1873,7 +1876,7 @@ impl VisitAstPath for Analyzer<'_> { }) } else if is_unresolved(ident, self.eval_context.unresolved_mark) { self.add_effect(Effect::FreeVar { - var: JsValue::FreeVar(ident.sym.clone()), + var: Box::new(JsValue::FreeVar(ident.sym.clone())), ast_path: as_parent_path(ast_path), span: ident.span(), in_try: is_in_try(ast_path), @@ -1925,19 +1928,19 @@ impl VisitAstPath for Analyzer<'_> { let mut ast_path = ast_path.with_guard(AstParentNodeRef::CondExpr(expr, CondExprField::Cons)); expr.cons.visit_with_ast_path(self, &mut ast_path); - EffectsBlock { + Box::new(EffectsBlock { effects: take(&mut self.effects), range: AstPathRange::Exact(as_parent_path(&ast_path)), - } + }) }; let r#else = { let mut ast_path = ast_path.with_guard(AstParentNodeRef::CondExpr(expr, CondExprField::Alt)); expr.alt.visit_with_ast_path(self, &mut ast_path); - EffectsBlock { + Box::new(EffectsBlock { effects: take(&mut self.effects), range: AstPathRange::Exact(as_parent_path(&ast_path)), - } + }) }; self.effects = prev_effects; @@ -1968,10 +1971,10 @@ impl VisitAstPath for Analyzer<'_> { ast_path.with_guard(AstParentNodeRef::IfStmt(stmt, IfStmtField::Cons)); stmt.cons.visit_with_ast_path(self, &mut ast_path); then_returning = self.end_early_return_block(); - EffectsBlock { + Box::new(EffectsBlock { effects: take(&mut self.effects), range: AstPathRange::Exact(as_parent_path(&ast_path)), - } + }) }; let mut else_returning = false; let r#else = stmt.alt.as_ref().map(|alt| { @@ -1979,10 +1982,10 @@ impl VisitAstPath for Analyzer<'_> { ast_path.with_guard(AstParentNodeRef::IfStmt(stmt, IfStmtField::Alt)); alt.visit_with_ast_path(self, &mut ast_path); else_returning = self.end_early_return_block(); - EffectsBlock { + Box::new(EffectsBlock { effects: take(&mut self.effects), range: AstPathRange::Exact(as_parent_path(&ast_path)), - } + }) }); self.early_return_stack = prev_early_return_stack; self.effects = prev_effects; @@ -2118,7 +2121,7 @@ impl VisitAstPath for Analyzer<'_> { ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>, ) { if n.op == UnaryOp::TypeOf { - let arg_value = self.eval_context.eval(&n.arg); + let arg_value = Box::new(self.eval_context.eval(&n.arg)); self.add_effect(Effect::TypeOf { arg: arg_value, ast_path: as_parent_path(ast_path), @@ -2137,8 +2140,8 @@ impl Analyzer<'_> { ast_path: &AstNodePath>, condition_ast_kind: AstParentKind, span: Span, - then: Option, - r#else: Option, + then: Option>, + r#else: Option>, early_return_when_true: bool, early_return_when_false: bool, ) { @@ -2146,7 +2149,7 @@ impl Analyzer<'_> { { return; } - let condition = self.eval_context.eval(test); + let condition = Box::new(self.eval_context.eval(test)); if condition.is_unknown() { if let Some(mut then) = then { self.effects.append(&mut then.effects); @@ -2215,7 +2218,7 @@ impl Analyzer<'_> { span: Span, mut cond_kind: ConditionalKind, ) { - let condition = self.eval_context.eval(test); + let condition = Box::new(self.eval_context.eval(test)); if condition.is_unknown() { match &mut cond_kind { ConditionalKind::If { then } => { diff --git a/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs b/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs index e4509104866d6..7b59c11aed890 100644 --- a/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs @@ -4178,7 +4178,7 @@ mod tests { condition, kind, .. } => { let condition = - resolve(&var_graph, condition, ImportAttributes::empty_ref()) + resolve(&var_graph, *condition, ImportAttributes::empty_ref()) .await; resolved.push((format!("{parent} -> {i} conditional"), condition)); match *kind { @@ -4228,7 +4228,7 @@ mod tests { } => { let func = resolve( &var_graph, - func, + *func, eval_context.imports.get_attributes(span), ) .await; @@ -4243,11 +4243,11 @@ mod tests { )); } Effect::FreeVar { var, .. } => { - resolved.push((format!("{parent} -> {i} free var"), var)); + resolved.push((format!("{parent} -> {i} free var"), *var)); } Effect::TypeOf { arg, .. } => { let arg = - resolve(&var_graph, arg, ImportAttributes::empty_ref()).await; + resolve(&var_graph, *arg, ImportAttributes::empty_ref()).await; resolved.push(( format!("{parent} -> {i} typeof"), JsValue::type_of(Box::new(arg)), @@ -4257,9 +4257,9 @@ mod tests { obj, prop, args, .. } => { let obj = - resolve(&var_graph, obj, ImportAttributes::empty_ref()).await; + resolve(&var_graph, *obj, ImportAttributes::empty_ref()).await; let prop = - resolve(&var_graph, prop, ImportAttributes::empty_ref()).await; + resolve(&var_graph, *prop, ImportAttributes::empty_ref()).await; let new_args = handle_args(args, &mut queue, &var_graph, i).await; resolved.push(( format!("{parent} -> {i} member call"), diff --git a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs index 3fd7ceb147056..c405173123c7d 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/mod.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/mod.rs @@ -363,7 +363,7 @@ impl AnalysisState<'_> { let fun_args_values = self.fun_args_values.lock().clone(); link( self.var_graph, - value.clone(), + value, &early_value_visitor, &|value| { value_visitor( @@ -916,7 +916,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( let condition_has_side_effects = condition.has_side_effects(); let condition = analysis_state - .link_value(condition, ImportAttributes::empty_ref()) + .link_value(*condition, ImportAttributes::empty_ref()) .await?; macro_rules! inactive { @@ -1069,7 +1069,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( } let func = analysis_state - .link_value(func, eval_context.imports.get_attributes(span)) + .link_value(*func, eval_context.imports.get_attributes(span)) .await?; handle_call( @@ -1101,10 +1101,10 @@ pub(crate) async fn analyse_ecmascript_module_internal( } } let mut obj = analysis_state - .link_value(obj, ImportAttributes::empty_ref()) + .link_value(*obj, ImportAttributes::empty_ref()) .await?; let prop = analysis_state - .link_value(prop, ImportAttributes::empty_ref()) + .link_value(*prop, ImportAttributes::empty_ref()) .await?; if !new { @@ -1171,11 +1171,11 @@ pub(crate) async fn analyse_ecmascript_module_internal( } => { // FreeVar("require") might be turbopackIgnore-d if !analysis_state - .link_value(var.clone(), eval_context.imports.get_attributes(span)) + .link_value(*var.clone(), eval_context.imports.get_attributes(span)) .await? .is_unknown() { - handle_free_var(&ast_path, var, span, &analysis_state, &mut analysis).await?; + handle_free_var(&ast_path, *var, span, &analysis_state, &mut analysis).await?; } } Effect::Member { @@ -1186,10 +1186,10 @@ pub(crate) async fn analyse_ecmascript_module_internal( in_try: _, } => { let obj = analysis_state - .link_value(obj, ImportAttributes::empty_ref()) + .link_value(*obj, ImportAttributes::empty_ref()) .await?; let prop = analysis_state - .link_value(prop, ImportAttributes::empty_ref()) + .link_value(*prop, ImportAttributes::empty_ref()) .await?; handle_member(&ast_path, obj, prop, span, &analysis_state, &mut analysis).await?; @@ -1247,7 +1247,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( span, } => { let arg = analysis_state - .link_value(arg, ImportAttributes::empty_ref()) + .link_value(*arg, ImportAttributes::empty_ref()) .await?; handle_typeof(&ast_path, arg, span, &analysis_state, &mut analysis).await?; }