Skip to content

Commit aa0c128

Browse files
committed
feat: 给组件返回值包裹用于层叠的函数
1 parent feb99c0 commit aa0c128

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

src/visitor.rs

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ use lightningcss::{
1515
};
1616
use swc_atoms::Atom;
1717
use swc_common::{Span, DUMMY_SP};
18-
use swc_ecma_ast::{
19-
AssignExpr, AssignOp, BindingIdent, BlockStmt, CallExpr, Callee, ComputedPropName, Decl, Expr, ExprOrSpread, ExprStmt, FnDecl, FnExpr, Function, Ident, IfStmt, ImportDecl, ImportNamedSpecifier, ImportSpecifier, JSXAttr, JSXAttrName, JSXAttrOrSpread, JSXAttrValue, JSXElement, JSXElementName, JSXExpr, JSXExprContainer, JSXFragment, KeyValueProp, Lit, MemberProp, Module, ModuleDecl, ModuleItem, Null, ObjectLit, Prop, PropName, PropOrSpread, ReturnStmt, Stmt, Str, VarDecl
20-
};
18+
use swc_ecma_ast::*;
19+
use swc_ecma_utils::quote_ident;
2120
use swc_ecma_visit::{
2221
noop_visit_mut_type, noop_visit_type, Visit, VisitAll, VisitAllWith, VisitMut, VisitMutWith,
2322
VisitWith,
@@ -424,12 +423,52 @@ pub fn insert_import_module_decl(module: &mut Module, last_import_index: usize,
424423

425424
pub struct ModuleMutVisitor {
426425
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
427-
pub platform: Platform
426+
pub platform: Platform,
427+
pub enable_cascading: bool,
428428
}
429429

430430
impl ModuleMutVisitor {
431431
pub fn new(all_style: Rc<RefCell<HashMap<String, StyleValue>>>, platform: Platform) -> Self {
432-
ModuleMutVisitor { all_style, platform }
432+
ModuleMutVisitor { all_style, platform, enable_cascading: true }
433+
}
434+
}
435+
436+
impl ModuleMutVisitor {
437+
fn get_cascading_visitor (&self) -> impl VisitMut {
438+
struct MyVisitor;
439+
impl VisitMut for MyVisitor {
440+
fn visit_mut_function(&mut self, _: &mut Function) {}
441+
fn visit_mut_arrow_expr(&mut self, _: &mut ArrowExpr) {}
442+
fn visit_mut_return_stmt(&mut self, stmt: &mut ReturnStmt) {
443+
let arg = &mut stmt.arg;
444+
if arg.is_some() {
445+
let expr = arg.take().unwrap();
446+
*arg = Some(Box::new(Expr::Call(CallExpr {
447+
span: DUMMY_SP,
448+
callee: Callee::Expr(Box::new(Expr::Ident(quote_ident!("MyMethod")))),
449+
args: vec![ExprOrSpread { expr, spread: None }],
450+
type_args: None,
451+
})))
452+
}
453+
}
454+
}
455+
MyVisitor {}
456+
}
457+
fn enable_cascading_for_class (&self, class: &mut Box<Class>) {
458+
let render_function = class.body.iter_mut().find(|item| {
459+
// Todo: support ClassProperty
460+
if let ClassMember::Method(ClassMethod { key, .. }) = item {
461+
return key.is_ident() && key.as_ident().unwrap().sym == "render";
462+
}
463+
return false;
464+
});
465+
if render_function.is_some() {
466+
let body = &mut render_function.unwrap().as_mut_method().unwrap().function;
467+
body.visit_mut_children_with(&mut self.get_cascading_visitor());
468+
};
469+
}
470+
fn enable_cascading_for_function (&self, body: &mut Box<Function>) {
471+
body.visit_mut_children_with(&mut &mut self.get_cascading_visitor());
433472
}
434473
}
435474

@@ -584,9 +623,35 @@ impl VisitMut for ModuleMutVisitor {
584623

585624
// 将 inner_style_stmt 插入到 module 的最后一条 import 语句之后
586625
let mut last_import_index = 0;
587-
for (index, stmt) in module.body.iter().enumerate() {
588-
if let ModuleItem::ModuleDecl(ModuleDecl::Import(_)) = stmt {
589-
last_import_index = index;
626+
for (index, stmt) in module.body.iter_mut().enumerate() {
627+
if stmt.is_module_decl() {
628+
let module_decl = stmt.as_mut_module_decl().unwrap();
629+
if let ModuleDecl::Import(_) = module_decl {
630+
last_import_index = index;
631+
}
632+
// 开启层叠功能
633+
if self.enable_cascading {
634+
match module_decl {
635+
ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { decl, .. }) => {
636+
match decl {
637+
// export defualt class {}
638+
DefaultDecl::Class(ClassExpr { class, .. }) => {
639+
self.enable_cascading_for_class(class);
640+
},
641+
// export defualt function () {}
642+
DefaultDecl::Fn(FnExpr { function, ..}) => {
643+
self.enable_cascading_for_function(function);
644+
}
645+
_ => ()
646+
}
647+
},
648+
// export default Index
649+
ModuleDecl::ExportDefaultExpr(ExportDefaultExpr { expr, .. }) => {
650+
// Todo: suport ExportDefaultExpr
651+
},
652+
_ => ()
653+
}
654+
}
590655
}
591656
}
592657
if last_import_index != 0 {

0 commit comments

Comments
 (0)