Unverified Commit dcd5ed0a authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor(turbopack): Remove `PassFactory` (#79555)

### What?

Remove `PassFactory`. Instead, merge it into `AstModifier`.

### Why?

`PassFactory` was a temporary solution required only for step-by-step refactoring. Now the refactoring is done, we don't need it anymore.
parent 5f3868b2
...@@ -2,7 +2,7 @@ use anyhow::Result; ...@@ -2,7 +2,7 @@ use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use swc_core::ecma::{ use swc_core::ecma::{
ast::{ ast::{
BlockStmt, CallExpr, Expr, Lit, MemberExpr, ModuleDecl, ModuleItem, Pass, Pat, Prop, BlockStmt, CallExpr, Expr, Lit, MemberExpr, ModuleDecl, ModuleItem, Pat, Program, Prop,
SimpleAssignTarget, Stmt, Str, SwitchCase, SimpleAssignTarget, Stmt, Str, SwitchCase,
}, },
visit::AstParentKind, visit::AstParentKind,
...@@ -38,7 +38,6 @@ use crate::references::{ ...@@ -38,7 +38,6 @@ use crate::references::{
#[derive(Default)] #[derive(Default)]
pub struct CodeGeneration { pub struct CodeGeneration {
/// ast nodes matching the span will be visitor by the visitor /// ast nodes matching the span will be visitor by the visitor
pub root_visitors: Vec<Box<dyn PassFactory>>,
pub visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>, pub visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>,
pub hoisted_stmts: Vec<CodeGenerationHoistedStmt>, pub hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
pub early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>, pub early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
...@@ -52,34 +51,18 @@ impl CodeGeneration { ...@@ -52,34 +51,18 @@ impl CodeGeneration {
} }
pub fn new( pub fn new(
root_visitors: Vec<Box<dyn PassFactory>>,
visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>, visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>,
hoisted_stmts: Vec<CodeGenerationHoistedStmt>, hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>, early_hoisted_stmts: Vec<CodeGenerationHoistedStmt>,
) -> Self { ) -> Self {
CodeGeneration { CodeGeneration {
root_visitors,
visitors, visitors,
hoisted_stmts, hoisted_stmts,
early_hoisted_stmts, early_hoisted_stmts,
} }
} }
pub fn root_visitors(root_visitors: Vec<Box<dyn PassFactory>>) -> Self {
CodeGeneration {
root_visitors,
..Default::default()
}
}
pub fn visitors(visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>) -> Self { pub fn visitors(visitors: Vec<(Vec<AstParentKind>, Box<dyn AstModifier>)>) -> Self {
#[cfg(debug_assertions)]
for (path, _) in visitors.iter() {
if path.is_empty() {
unreachable!("if the path is empty, the visitor should be a root visitor");
}
}
CodeGeneration { CodeGeneration {
visitors, visitors,
..Default::default() ..Default::default()
...@@ -113,10 +96,6 @@ impl CodeGenerationHoistedStmt { ...@@ -113,10 +96,6 @@ impl CodeGenerationHoistedStmt {
} }
} }
pub trait PassFactory: Send + Sync {
fn create<'a>(&'a self) -> Box<dyn Pass + Send + Sync + 'a>;
}
macro_rules! method { macro_rules! method {
($name:ident, $T:ty) => { ($name:ident, $T:ty) => {
fn $name(&self, _node: &mut $T) {} fn $name(&self, _node: &mut $T) {}
...@@ -137,6 +116,7 @@ pub trait AstModifier: Send + Sync { ...@@ -137,6 +116,7 @@ pub trait AstModifier: Send + Sync {
method!(visit_mut_str, Str); method!(visit_mut_str, Str);
method!(visit_mut_block_stmt, BlockStmt); method!(visit_mut_block_stmt, BlockStmt);
method!(visit_mut_switch_case, SwitchCase); method!(visit_mut_switch_case, SwitchCase);
method!(visit_mut_program, Program);
} }
pub trait ModifiableAst { pub trait ModifiableAst {
...@@ -166,6 +146,7 @@ impl_modify!(visit_mut_lit, Lit); ...@@ -166,6 +146,7 @@ impl_modify!(visit_mut_lit, Lit);
impl_modify!(visit_mut_str, Str); impl_modify!(visit_mut_str, Str);
impl_modify!(visit_mut_block_stmt, BlockStmt); impl_modify!(visit_mut_block_stmt, BlockStmt);
impl_modify!(visit_mut_switch_case, SwitchCase); impl_modify!(visit_mut_switch_case, SwitchCase);
impl_modify!(visit_mut_program, Program);
#[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)] #[derive(PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue)]
pub enum CodeGen { pub enum CodeGen {
......
...@@ -94,7 +94,7 @@ pub use turbopack_resolve::ecmascript as resolve; ...@@ -94,7 +94,7 @@ pub use turbopack_resolve::ecmascript as resolve;
use self::chunk::{EcmascriptChunkItemContent, EcmascriptChunkType, EcmascriptExports}; use self::chunk::{EcmascriptChunkItemContent, EcmascriptChunkType, EcmascriptExports};
use crate::{ use crate::{
chunk::{EcmascriptChunkPlaceable, placeable::is_marked_as_side_effect_free}, chunk::{EcmascriptChunkPlaceable, placeable::is_marked_as_side_effect_free},
code_gen::CodeGens, code_gen::{CodeGens, ModifiableAst},
parse::generate_js_source_map, parse::generate_js_source_map,
references::{ references::{
analyse_ecmascript_module, async_module::OptionAsyncModule, esm::base::EsmAssetReferences, analyse_ecmascript_module, async_module::OptionAsyncModule, esm::base::EsmAssetReferences,
...@@ -1172,13 +1172,10 @@ fn process_content_with_code_gens( ...@@ -1172,13 +1172,10 @@ fn process_content_with_code_gens(
for CodeGenerationHoistedStmt { key, stmt } in code_gen.early_hoisted_stmts.drain(..) { for CodeGenerationHoistedStmt { key, stmt } in code_gen.early_hoisted_stmts.drain(..) {
early_hoisted_stmts.insert(key.clone(), stmt); early_hoisted_stmts.insert(key.clone(), stmt);
} }
for visitor in &code_gen.root_visitors {
root_visitors.push(visitor.create());
}
for (path, visitor) in &code_gen.visitors { for (path, visitor) in &code_gen.visitors {
if path.is_empty() { if path.is_empty() {
unreachable!("if the path is empty, the visitor should be a root visitor"); root_visitors.push(&**visitor);
} else { } else {
visitors.push((path, &**visitor)); visitors.push((path, &**visitor));
} }
...@@ -1193,7 +1190,7 @@ fn process_content_with_code_gens( ...@@ -1193,7 +1190,7 @@ fn process_content_with_code_gens(
); );
} }
for pass in root_visitors { for pass in root_visitors {
program.mutate(pass); program.modify(pass);
} }
program.visit_mut_with( program.visit_mut_with(
&mut swc_core::ecma::transforms::base::hygiene::hygiene_with_config( &mut swc_core::ecma::transforms::base::hygiene::hygiene_with_config(
......
...@@ -678,7 +678,6 @@ impl EsmExports { ...@@ -678,7 +678,6 @@ impl EsmExports {
}; };
Ok(CodeGeneration::new( Ok(CodeGeneration::new(
vec![],
vec![], vec![],
[dynamic_stmt [dynamic_stmt
.map(|stmt| CodeGenerationHoistedStmt::new("__turbopack_dynamic__".into(), stmt))] .map(|stmt| CodeGenerationHoistedStmt::new("__turbopack_dynamic__".into(), stmt))]
......
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