|
|
use std::borrow::Cow; |
|
|
|
|
|
use swc_core::{ |
|
|
common::pass::AstKindPath, |
|
|
ecma::{ |
|
|
ast::*, |
|
|
visit::{AstParentKind, VisitMutAstPath, VisitMutWithAstPath}, |
|
|
}, |
|
|
}; |
|
|
|
|
|
use crate::code_gen::{AstModifier, ModifiableAst}; |
|
|
|
|
|
pub type AstPath = Vec<AstParentKind>; |
|
|
|
|
|
|
|
|
pub struct ApplyVisitors<'a, 'b> { |
|
|
|
|
|
|
|
|
visitors: Cow<'b, [(&'a AstPath, &'a dyn AstModifier)]>, |
|
|
|
|
|
index: usize, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn find_range<'a, 'b>( |
|
|
visitors: &'b [(&'a AstPath, &'a dyn AstModifier)], |
|
|
kind: &AstParentKind, |
|
|
index: usize, |
|
|
) -> Option<&'b [(&'a AstPath, &'a dyn AstModifier)]> { |
|
|
|
|
|
if visitors.first().unwrap().0[index] > *kind || visitors.last().unwrap().0[index] < *kind { |
|
|
|
|
|
|
|
|
return None; |
|
|
} |
|
|
|
|
|
let start = if visitors.first().unwrap().0[index] == *kind { |
|
|
|
|
|
0 |
|
|
} else { |
|
|
visitors.partition_point(|(path, _)| path[index] < *kind) |
|
|
}; |
|
|
|
|
|
if start >= visitors.len() { |
|
|
return None; |
|
|
} |
|
|
|
|
|
if visitors[start].0[index] > *kind { |
|
|
|
|
|
|
|
|
return None; |
|
|
} |
|
|
|
|
|
let end = if visitors.last().unwrap().0[index] == *kind { |
|
|
|
|
|
visitors.len() |
|
|
} else { |
|
|
visitors[start..].partition_point(|(path, _)| path[index] == *kind) + start |
|
|
}; |
|
|
if end == start { |
|
|
return None; |
|
|
} |
|
|
|
|
|
Some(&visitors[start..end]) |
|
|
} |
|
|
|
|
|
impl<'a> ApplyVisitors<'a, '_> { |
|
|
|
|
|
pub fn new(mut visitors: Vec<(&'a AstPath, &'a dyn AstModifier)>) -> Self { |
|
|
assert!(!visitors.is_empty()); |
|
|
visitors.sort_by_key(|(path, _)| *path); |
|
|
Self { |
|
|
visitors: Cow::Owned(visitors), |
|
|
index: 0, |
|
|
} |
|
|
} |
|
|
|
|
|
#[inline(never)] |
|
|
fn visit_if_required<N>(&mut self, n: &mut N, ast_path: &mut AstKindPath<AstParentKind>) |
|
|
where |
|
|
N: ModifiableAst + for<'aa, 'bb> VisitMutWithAstPath<ApplyVisitors<'aa, 'bb>>, |
|
|
{ |
|
|
let mut index = self.index; |
|
|
let mut current_visitors = self.visitors.as_ref(); |
|
|
while index < ast_path.len() { |
|
|
let current = index == ast_path.len() - 1; |
|
|
let kind = ast_path[index]; |
|
|
if let Some(visitors) = find_range(current_visitors, &kind, index) { |
|
|
|
|
|
|
|
|
|
|
|
index += 1; |
|
|
|
|
|
|
|
|
let nested_visitors_start = |
|
|
visitors.partition_point(|(path, _)| path.len() == index); |
|
|
if current { |
|
|
|
|
|
if nested_visitors_start < visitors.len() { |
|
|
n.visit_mut_children_with_ast_path( |
|
|
&mut ApplyVisitors { |
|
|
|
|
|
|
|
|
visitors: Cow::Borrowed(&visitors[nested_visitors_start..]), |
|
|
index, |
|
|
}, |
|
|
ast_path, |
|
|
); |
|
|
} |
|
|
for (_, visitor) in visitors[..nested_visitors_start].iter() { |
|
|
n.modify(&**visitor); |
|
|
} |
|
|
return; |
|
|
} else { |
|
|
|
|
|
|
|
|
current_visitors = &visitors[nested_visitors_start..]; |
|
|
if current_visitors.is_empty() { |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
n.visit_mut_children_with_ast_path(self, ast_path); |
|
|
} |
|
|
} |
|
|
|
|
|
macro_rules! method { |
|
|
($name:ident, $T:ty) => { |
|
|
fn $name(&mut self, n: &mut $T, ast_path: &mut AstKindPath<AstParentKind>) { |
|
|
self.visit_if_required(n, ast_path); |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
impl VisitMutAstPath for ApplyVisitors<'_, '_> { |
|
|
|
|
|
method!(visit_mut_prop, Prop); |
|
|
method!(visit_mut_simple_assign_target, SimpleAssignTarget); |
|
|
method!(visit_mut_expr, Expr); |
|
|
method!(visit_mut_member_expr, MemberExpr); |
|
|
method!(visit_mut_pat, Pat); |
|
|
method!(visit_mut_stmt, Stmt); |
|
|
method!(visit_mut_module_decl, ModuleDecl); |
|
|
method!(visit_mut_module_item, ModuleItem); |
|
|
method!(visit_mut_call_expr, CallExpr); |
|
|
method!(visit_mut_lit, Lit); |
|
|
method!(visit_mut_str, Str); |
|
|
method!(visit_mut_block_stmt, BlockStmt); |
|
|
method!(visit_mut_switch_case, SwitchCase); |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use std::sync::Arc; |
|
|
|
|
|
use swc_core::{ |
|
|
common::{FileName, Mark, SourceFile, SourceMap, errors::HANDLER}, |
|
|
ecma::{ |
|
|
ast::*, |
|
|
codegen::{Emitter, text_writer::JsWriter}, |
|
|
parser::parse_file_as_module, |
|
|
transforms::base::resolver, |
|
|
visit::{AstParentKind, VisitMutWith, VisitMutWithAstPath, fields::*}, |
|
|
}, |
|
|
testing::run_test, |
|
|
}; |
|
|
|
|
|
use super::{ApplyVisitors, AstModifier}; |
|
|
|
|
|
fn parse(fm: &SourceFile) -> Module { |
|
|
let mut m = parse_file_as_module( |
|
|
fm, |
|
|
Default::default(), |
|
|
EsVersion::latest(), |
|
|
None, |
|
|
&mut vec![], |
|
|
) |
|
|
.map_err(|err| HANDLER.with(|handler| err.into_diagnostic(handler).emit())) |
|
|
.unwrap(); |
|
|
|
|
|
let unresolved_mark = Mark::new(); |
|
|
let top_level_mark = Mark::new(); |
|
|
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); |
|
|
|
|
|
m |
|
|
} |
|
|
|
|
|
struct StrReplacer<'a> { |
|
|
from: &'a str, |
|
|
to: &'a str, |
|
|
} |
|
|
|
|
|
impl AstModifier for StrReplacer<'_> { |
|
|
fn visit_mut_str(&self, s: &mut Str) { |
|
|
s.value = s.value.replace(self.from, self.to).into(); |
|
|
s.raw = None; |
|
|
} |
|
|
} |
|
|
|
|
|
fn replacer(from: &'static str, to: &'static str) -> Box<dyn AstModifier + 'static> { |
|
|
Box::new(StrReplacer { from, to }) |
|
|
} |
|
|
|
|
|
fn to_js(m: &Module, cm: &Arc<SourceMap>) -> String { |
|
|
let mut bytes = Vec::new(); |
|
|
let mut emitter = Emitter { |
|
|
cfg: swc_core::ecma::codegen::Config::default().with_minify(true), |
|
|
cm: cm.clone(), |
|
|
comments: None, |
|
|
wr: JsWriter::new(cm.clone(), "\n", &mut bytes, None), |
|
|
}; |
|
|
|
|
|
emitter.emit_module(m).unwrap(); |
|
|
|
|
|
String::from_utf8(bytes).unwrap() |
|
|
} |
|
|
|
|
|
#[test] |
|
|
fn path_visitor() { |
|
|
run_test(false, |cm, _handler| { |
|
|
let fm = cm.new_source_file(FileName::Anon.into(), "('foo', 'bar', ['baz']);"); |
|
|
|
|
|
let m = parse(&fm); |
|
|
|
|
|
let module_kind = AstParentKind::Module(ModuleField::Body(0)); |
|
|
let module_item_kind = AstParentKind::ModuleItem(ModuleItemField::Stmt); |
|
|
let stmt_kind = AstParentKind::Stmt(StmtField::Expr); |
|
|
let expr_stmt_kind = AstParentKind::ExprStmt(ExprStmtField::Expr); |
|
|
let expr_kind = AstParentKind::Expr(ExprField::Paren); |
|
|
let paren_kind = AstParentKind::ParenExpr(ParenExprField::Expr); |
|
|
let expr2_kind = AstParentKind::Expr(ExprField::Seq); |
|
|
let seq_kind = AstParentKind::SeqExpr(SeqExprField::Exprs(1)); |
|
|
let expr3_kind = AstParentKind::Expr(ExprField::Lit); |
|
|
let lit_kind = AstParentKind::Lit(LitField::Str); |
|
|
|
|
|
{ |
|
|
let path = vec![ |
|
|
module_kind, |
|
|
module_item_kind, |
|
|
stmt_kind, |
|
|
expr_stmt_kind, |
|
|
expr_kind, |
|
|
paren_kind, |
|
|
expr2_kind, |
|
|
seq_kind, |
|
|
expr3_kind, |
|
|
lit_kind, |
|
|
]; |
|
|
let bar_replacer = replacer("bar", "bar-success"); |
|
|
|
|
|
let mut m = m.clone(); |
|
|
m.visit_mut_with_ast_path( |
|
|
&mut ApplyVisitors::new(vec![(&path, &*bar_replacer)]), |
|
|
&mut Default::default(), |
|
|
); |
|
|
|
|
|
let s = to_js(&m, &cm); |
|
|
assert_eq!(s, r#"("foo","bar-success",["baz"]);"#); |
|
|
} |
|
|
|
|
|
{ |
|
|
let wrong_path = vec![ |
|
|
module_kind, |
|
|
module_item_kind, |
|
|
stmt_kind, |
|
|
expr_stmt_kind, |
|
|
|
|
|
paren_kind, |
|
|
expr2_kind, |
|
|
seq_kind, |
|
|
expr3_kind, |
|
|
lit_kind, |
|
|
]; |
|
|
let bar_replacer = replacer("bar", "bar-success"); |
|
|
|
|
|
let mut m = m.clone(); |
|
|
m.visit_mut_with_ast_path( |
|
|
&mut ApplyVisitors::new(vec![(&wrong_path, &*bar_replacer)]), |
|
|
&mut Default::default(), |
|
|
); |
|
|
|
|
|
let s = to_js(&m, &cm); |
|
|
assert!(!s.contains("bar-success")); |
|
|
} |
|
|
|
|
|
drop(m); |
|
|
|
|
|
Ok(()) |
|
|
}) |
|
|
.unwrap(); |
|
|
} |
|
|
} |
|
|
|