Skip to content

Commit fca94e6

Browse files
committed
feat: 新增paser配置
1 parent 4c15e38 commit fca94e6

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33

44
/* auto-generated by NAPI-RS */
55

6-
export function parse(component: string, styles: Array<string>, platformString: string): string
6+
export interface ParseOptions {
7+
platformString: string
8+
isEnableNesting?: boolean
9+
}
10+
export function parse(component: string, styles: Array<string>, options: ParseOptions): string

src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@ mod parse_style_properties;
2525
// component: jsx的code string
2626
// styles: css的code string
2727
// platform_string: "ReactNative" | "Harmony"
28+
29+
#[napi(object)]
30+
pub struct ParseOptions {
31+
pub platform_string: String,
32+
pub is_enable_nesting: Option<bool>,
33+
}
34+
2835
#[napi]
29-
pub fn parse(component: String, styles: Vec<String>, platform_string: String) -> String {
36+
pub fn parse(component: String, styles: Vec<String>, options: ParseOptions) -> String {
3037

31-
let platform = match platform_string.as_str() {
38+
let platform = match options.platform_string.as_str() {
3239
"ReactNative" => Platform::ReactNative,
3340
"Harmony" => Platform::Harmony,
3441
_ => Platform::Harmony
3542
};
3643

44+
let is_enable_nesting = options.is_enable_nesting.map_or(false, |item| item);
45+
3746
// 解析组件文件
3847
let cm: Lrc<SourceMap> = Default::default();
3948
let comments = SingleThreadedComments::default();
@@ -54,6 +63,7 @@ pub fn parse(component: String, styles: Vec<String>, platform_string: String) ->
5463
style_data.style_record.clone(),
5564
style_data.pesudo_style_record.clone(),
5665
style_data.all_style.clone(),
66+
is_enable_nesting,
5767
);
5868
style_write.write(platform);
5969

@@ -72,4 +82,3 @@ pub fn parse(component: String, styles: Vec<String>, platform_string: String) ->
7282
let code = String::from_utf8(buf).unwrap().replace("\r\n", "\n");
7383
code
7484
}
75-

src/style_write.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct StyleWrite<'i> {
1414
pub style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Property<'i>)>>>>,
1515
pub pesudo_style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Vec<(String, Property<'i>)>)>>>>,
1616
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
17+
pub is_enable_nesting: bool,
1718
}
1819

1920
impl<'i> StyleWrite<'i> {
@@ -23,13 +24,15 @@ impl<'i> StyleWrite<'i> {
2324
style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Property<'i>)>>>>,
2425
pesudo_style_record: Rc<RefCell<HashMap<SpanKey, Vec<(String, Vec<(String, Property<'i>)>)>>>>,
2526
all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
27+
is_enable_nesting: bool
2628
) -> Self {
2729
StyleWrite {
2830
module,
2931
jsx_record,
3032
style_record,
3133
pesudo_style_record,
3234
all_style,
35+
is_enable_nesting,
3336
}
3437
}
3538

@@ -50,7 +53,7 @@ impl<'i> StyleWrite<'i> {
5053
}
5154
// 插入样式表
5255
{
53-
let mut insert_mut_visitor = ModuleMutVisitor::new(self.all_style.clone(), platform.clone());
56+
let mut insert_mut_visitor = ModuleMutVisitor::new(self.all_style.clone(), platform.clone(), self.is_enable_nesting);
5457
self
5558
.module
5659
.borrow_mut()

src/visitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ pub fn insert_import_module_decl(module: &mut Module, last_import_index: usize,
430430
pub struct ModuleMutVisitor {
431431
pub all_style: Rc<RefCell<HashMap<String, StyleValue>>>,
432432
pub platform: Platform,
433-
pub enable_cascading: bool,
433+
pub is_enable_nesting: bool,
434434
}
435435

436436
impl ModuleMutVisitor {
437-
pub fn new(all_style: Rc<RefCell<HashMap<String, StyleValue>>>, platform: Platform) -> Self {
438-
ModuleMutVisitor { all_style, platform, enable_cascading: true }
437+
pub fn new(all_style: Rc<RefCell<HashMap<String, StyleValue>>>, platform: Platform, is_enable_nesting: bool) -> Self {
438+
ModuleMutVisitor { all_style, platform, is_enable_nesting }
439439
}
440440
}
441441

@@ -568,7 +568,7 @@ impl VisitMut for ModuleMutVisitor {
568568
last_import_index = index;
569569
}
570570
// 开启层叠功能
571-
if self.enable_cascading {
571+
if self.is_enable_nesting {
572572
match module_decl {
573573
ModuleDecl::ExportDefaultDecl(ExportDefaultDecl { decl, .. }) => {
574574
match decl {

0 commit comments

Comments
 (0)