Skip to content

Commit

Permalink
feat: 抽离写入逻辑的公共函数,支持 React.createElement 形式的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanzebin committed Nov 29, 2023
1 parent 29041fe commit d2731f2
Showing 2 changed files with 508 additions and 304 deletions.
30 changes: 28 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::HashMap;

use html5ever::{namespace_url, ns, LocalName, QualName};
use regex::Regex;
use swc_common::DUMMY_SP;
// use lightningcss::values::number::CSSNumber;
use swc_ecma_ast::{JSXMemberExpr, JSXObject, Callee, Expr, CallExpr, Ident, Lit, Number};
use swc_ecma_ast::{JSXMemberExpr, JSXObject, Callee, Expr, CallExpr, Ident, Lit, Number, PropOrSpread, Prop, PropName};

use crate::constants::{CONVERT_STYLE_PREFIX, CONVERT_STYLE_PX_FN};

@@ -91,4 +93,28 @@ pub fn convert_px_to_units(input: String) -> Expr {
}
// 如果没有匹配到,则返回原始字符串
Expr::Lit(Lit::Str(input.into()))
}
}

pub fn get_callee_attributes (callee: &CallExpr) -> HashMap<String, Box<Expr>> {
let mut attributes = HashMap::new();

if let Some(arg) = callee.args.get(1) {
if let Expr::Object(object) = &*arg.expr {
for prop in object.props.iter() {
if let PropOrSpread::Prop(prop) = prop {
if let Prop::KeyValue(key_value_prop) = &**prop {
let name = match &key_value_prop.key {
PropName::Ident(ident) => ident.sym.to_string(),
PropName::Str(str) => str.value.to_string(),
_ => "".to_string(),
};

attributes.insert(name, key_value_prop.value.clone());
}
}
}
}
}

attributes
}
Loading

0 comments on commit d2731f2

Please # to comment.