Skip to content

Commit d2731f2

Browse files
author
xuanzebin
committed
feat: 抽离写入逻辑的公共函数,支持 React.createElement 形式的代码
1 parent 29041fe commit d2731f2

File tree

2 files changed

+508
-304
lines changed

2 files changed

+508
-304
lines changed

src/utils.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use std::collections::HashMap;
2+
13
use html5ever::{namespace_url, ns, LocalName, QualName};
24
use regex::Regex;
35
use swc_common::DUMMY_SP;
46
// use lightningcss::values::number::CSSNumber;
5-
use swc_ecma_ast::{JSXMemberExpr, JSXObject, Callee, Expr, CallExpr, Ident, Lit, Number};
7+
use swc_ecma_ast::{JSXMemberExpr, JSXObject, Callee, Expr, CallExpr, Ident, Lit, Number, PropOrSpread, Prop, PropName};
68

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

@@ -91,4 +93,28 @@ pub fn convert_px_to_units(input: String) -> Expr {
9193
}
9294
// 如果没有匹配到,则返回原始字符串
9395
Expr::Lit(Lit::Str(input.into()))
94-
}
96+
}
97+
98+
pub fn get_callee_attributes (callee: &CallExpr) -> HashMap<String, Box<Expr>> {
99+
let mut attributes = HashMap::new();
100+
101+
if let Some(arg) = callee.args.get(1) {
102+
if let Expr::Object(object) = &*arg.expr {
103+
for prop in object.props.iter() {
104+
if let PropOrSpread::Prop(prop) = prop {
105+
if let Prop::KeyValue(key_value_prop) = &**prop {
106+
let name = match &key_value_prop.key {
107+
PropName::Ident(ident) => ident.sym.to_string(),
108+
PropName::Str(str) => str.value.to_string(),
109+
_ => "".to_string(),
110+
};
111+
112+
attributes.insert(name, key_value_prop.value.clone());
113+
}
114+
}
115+
}
116+
}
117+
}
118+
119+
attributes
120+
}

0 commit comments

Comments
 (0)