Skip to content

Commit d31dca1

Browse files
committed
feat: 增加对复杂的createElement支持
1 parent 7d1ae8c commit d31dca1

File tree

3 files changed

+179
-252
lines changed

3 files changed

+179
-252
lines changed

__test__/fixure/pesudo.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { View, Text } from '@tarojs/components'
2+
import React from 'react'
23
import './pesudo.scss'
34

45
export default function Pesudo() {
5-
return <View className='container'>
6-
<View className='box'></View>
7-
</View>
6+
return React.createElement('Text', _object_spread_porops(_object_spread({}, rest), {
7+
className: 'dasdsa'
8+
}))
89
}

src/utils.rs

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use html5ever::{namespace_url, ns, LocalName, QualName};
44
use pcre2::bytes::Regex;
55
// use lightningcss::values::number::CSSNumber;
6-
use swc_core::ecma::ast::{JSXMemberExpr, JSXObject, Expr, CallExpr, PropOrSpread, Prop, PropName};
6+
use swc_core::ecma::{ast::{ArrayLit, CallExpr, Expr, Function, JSXMemberExpr, JSXObject, ObjectLit, Prop, PropName, PropOrSpread}, visit::{Visit, VisitWith}};
77

88
use crate::style_propetries::unit::Platform;
99

@@ -71,25 +71,67 @@ pub fn prefix_style_key(s: String, platform: Platform) -> String {
7171
}
7272
}
7373

74-
pub fn get_callee_attributes (callee: &CallExpr) -> HashMap<String, Box<Expr>> {
75-
let mut attributes = HashMap::new();
74+
struct ObjectVisitor {
75+
pub attributes: HashMap<String, Box<Expr>>,
76+
}
7677

77-
if let Some(arg) = callee.args.get(1) {
78-
if let Expr::Object(object) = &*arg.expr {
79-
for prop in object.props.iter() {
80-
if let PropOrSpread::Prop(prop) = prop {
81-
if let Prop::KeyValue(key_value_prop) = &**prop {
82-
let name = match &key_value_prop.key {
83-
PropName::Ident(ident) => ident.sym.to_string(),
84-
PropName::Str(str) => str.value.to_string(),
85-
_ => "".to_string(),
86-
};
87-
88-
attributes.insert(name, key_value_prop.value.clone());
78+
impl ObjectVisitor {
79+
fn new() -> Self {
80+
ObjectVisitor {
81+
attributes: HashMap::new(),
82+
}
83+
}
84+
}
85+
86+
impl Visit for ObjectVisitor {
87+
fn visit_object_lit(&mut self, object: &ObjectLit) {
88+
for prop in &object.props {
89+
if let PropOrSpread::Prop(prop) = prop {
90+
if let Prop::KeyValue(kv) = &**prop {
91+
if let PropName::Ident(ref ident) = kv.key {
92+
let key = ident.sym.to_string();
93+
// Clone the value to store in the attributes map
94+
let value = kv.value.clone();
95+
self.attributes.insert(key, value);
96+
}
97+
}
8998
}
90-
}
9199
}
100+
// Continue traversing the AST
101+
object.visit_children_with(self);
102+
}
103+
104+
fn visit_call_expr(&mut self,n: &CallExpr) {
105+
// 可以在这里添加特定的逻辑
106+
n.args.iter().for_each(|arg| {
107+
arg.expr.visit_with(self);
108+
});
109+
}
110+
111+
// 确保所有其他类型的节点也被遍历
112+
fn visit_expr(&mut self, expr: &Expr) {
113+
// 判断expr的类型
114+
if let Expr::Object(obj) = &*expr {
115+
self.visit_object_lit(obj);
116+
} else {
117+
expr.visit_children_with(self);
92118
}
119+
120+
}
121+
122+
123+
}
124+
125+
pub fn get_callee_attributes (callee: &CallExpr) -> HashMap<String, Box<Expr>> {
126+
let mut attributes = HashMap::new();
127+
128+
if let Some(arg) = callee.args.get(1) {
129+
130+
let mut visitor = ObjectVisitor::new();
131+
(&*arg.expr).visit_children_with(&mut visitor);
132+
133+
134+
return visitor.attributes;
93135
}
94136

95137
attributes

0 commit comments

Comments
 (0)