File size: 10,258 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
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>;
// Invariant: Each [AstPath] in `visitors` contains a value at position `index`.
pub struct ApplyVisitors<'a, 'b> {
/// `VisitMut` should be shallow. In other words, it should not visit
/// children of the node.
visitors: Cow<'b, [(&'a AstPath, &'a dyn AstModifier)]>,
index: usize,
}
/// Do two binary searches to find the sub-slice that has `path[index] == kind`.
/// Returns None if no item matches that. `visitors` need to be sorted by path.
fn find_range<'a, 'b>(
visitors: &'b [(&'a AstPath, &'a dyn AstModifier)],
kind: &AstParentKind,
index: usize,
) -> Option<&'b [(&'a AstPath, &'a dyn AstModifier)]> {
// Precondition: visitors is never empty
if visitors.first().unwrap().0[index] > *kind || visitors.last().unwrap().0[index] < *kind {
// Fast path: If ast path of the first visitor is already out of range, then we
// can skip the whole visit.
return None;
}
let start = if visitors.first().unwrap().0[index] == *kind {
// Fast path: It looks like the whole range is selected
0
} else {
visitors.partition_point(|(path, _)| path[index] < *kind)
};
if start >= visitors.len() {
return None;
}
if visitors[start].0[index] > *kind {
// Fast path: If the starting point is greater than the given kind, it's
// meaningless to visit later.
return None;
}
let end = if visitors.last().unwrap().0[index] == *kind {
// Fast path: It's likely that the whole range is selected
visitors.len()
} else {
visitors[start..].partition_point(|(path, _)| path[index] == *kind) + start
};
if end == start {
return None;
}
// Postcondition: return value is never empty
Some(&visitors[start..end])
}
impl<'a> ApplyVisitors<'a, '_> {
/// `visitors` must have an non-empty [AstPath].
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) {
// visitors contains all items that match kind at index. Some of them terminate
// here, some need furth visiting. The terminating items are at the start due to
// sorting of the list.
index += 1;
// skip items that terminate here
let nested_visitors_start =
visitors.partition_point(|(path, _)| path.len() == index);
if current {
// Potentially skip visiting this sub tree
if nested_visitors_start < visitors.len() {
n.visit_mut_children_with_ast_path(
&mut ApplyVisitors {
// We only select visitors starting from `nested_visitors_start`
// which maintains the invariant.
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` has the invariant that is must not be empty.
// When it becomes empty, we must early exit
current_visitors = &visitors[nested_visitors_start..];
if current_visitors.is_empty() {
// Nothing to do in this subtree, skip it
return;
}
}
} else {
// Skip visiting this sub tree
return;
}
}
// Ast path is unchanged, just keep visiting
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<'_, '_> {
// TODO: we need a macro to apply that for all methods
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,
// 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![(&wrong_path, &*bar_replacer)]),
&mut Default::default(),
);
let s = to_js(&m, &cm);
assert!(!s.contains("bar-success"));
}
drop(m);
Ok(())
})
.unwrap();
}
}
|