From c10cffc7093b9e2878ee59fdd1038ec774934713 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Tue, 24 Oct 2023 17:22:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BD=93=E7=94=A8=E5=88=B0=20=5F=5Finne?= =?UTF-8?q?r=5Fstyle=5F=5F=20=E6=97=B6=E6=89=8D=E6=8F=92=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/visitor.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/visitor.rs b/src/visitor.rs index 4b0a8ac..9d6fb85 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -16,6 +16,7 @@ use swc_ecma_ast::{ }; use swc_ecma_visit::{ noop_visit_mut_type, noop_visit_type, Visit, VisitAll, VisitAllWith, VisitMut, VisitMutWith, + VisitWith, }; use crate::{ @@ -47,6 +48,20 @@ impl Hash for SpanKey { pub type JSXRecord = HashMap; +struct VarChecker { + found: bool, +} + +impl Visit for VarChecker { + noop_visit_type!(); + + fn visit_ident(&mut self, ident: &Ident) { + if ident.sym == "__inner_style__" { + self.found = true; + } + } +} + pub struct CollectVisitor { pub taro_components: Vec, } @@ -276,9 +291,15 @@ impl VisitMut for ModuleMutVisitor { })), ); last_import_index += 1; - module - .body - .insert(last_import_index, ModuleItem::Stmt(inner_style_stmt)); + let mut var_checker = VarChecker { found: false }; + module.visit_with(&mut var_checker); + println!("var_checker.found: {}", var_checker.found); + if var_checker.found { + // 插入代码 const __inner_style__ = calcDynamicStyle(__inner_style__) + module + .body + .insert(last_import_index, ModuleItem::Stmt(inner_style_stmt)); + } } }