Skip to content

Commit a842ecb

Browse files
committed
feat: 增加对规则对env的提前识别
1 parent c129577 commit a842ecb

File tree

7 files changed

+201
-125
lines changed

7 files changed

+201
-125
lines changed

__test__/fixure/style.bin

1.16 KB
Binary file not shown.

__test__/index.spec.mjs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Generated by [AVA](https://avajs.dev).
88

99
> Snapshot 1
1010
11-
'{"fonts":[],"keyframes":[],"medias":[],"styles":[{"declarations":[[66,100],[69,"100px"],[67,"100px"],[68,"100px"]],"media":0,"selector":["px"]}]}'
11+
'{"keyframes":[],"medias":[],"styles":[{"declarations":[[66,100],[69,"100px"],[67,"100px"],[68,"100px"]],"has_env":false,"media":0,"selector":["px"]}]}'

__test__/index.spec.mjs.snap

8 Bytes
Binary file not shown.

pnpm-lock.yaml

+175-116
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/json_writer.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use swc_core::common::DUMMY_SP;
44
use swc_core::ecma::ast::*;
55

66
use crate::constants::{Pseudo, SUPPORT_PSEUDO_KEYS};
7+
use crate::parse_style_properties::DeclsAndVars;
78
use crate::style_propetries::style_value_type::StyleValueType;
89

910
use crate::style_parser::{FontFaceItem, KeyFrameItem};
@@ -13,7 +14,7 @@ use crate::visitor::parse_style_values;
1314
use crate::{generate_expr_lit_num, generate_expr_lit_str, utils};
1415

1516
pub struct JsonWriter {
16-
styles: IndexMap<(u32, String), Vec<StyleValueType>>,
17+
styles: IndexMap<(u32, String), DeclsAndVars>,
1718
keyframes: IndexMap<(u32, String), Vec<KeyFrameItem>>,
1819
medias: Vec<StyleMedia>,
1920
fonts: Vec<FontFaceItem>,
@@ -22,7 +23,7 @@ pub struct JsonWriter {
2223

2324
impl JsonWriter {
2425
pub fn new(
25-
styles: IndexMap<(u32, String), Vec<StyleValueType>>,
26+
styles: IndexMap<(u32, String), DeclsAndVars>,
2627
keyframes: IndexMap<(u32, String), Vec<KeyFrameItem>>,
2728
medias: Vec<StyleMedia>,
2829
fonts: Vec<FontFaceItem>,
@@ -41,7 +42,7 @@ impl JsonWriter {
4142
let elems: Vec<Expr> = self
4243
.styles
4344
.iter()
44-
.filter_map(|((media_index, selector), prop_or_spreads)| {
45+
.filter_map(|((media_index, selector), item)| {
4546
Some({
4647
// 识别伪类
4748
let mut new_selector = selector.clone();
@@ -113,9 +114,13 @@ impl JsonWriter {
113114
key: PropName::Ident(Ident::new("declarations".into(), DUMMY_SP)),
114115
value: Box::new(Expr::Array(ArrayLit {
115116
span: DUMMY_SP,
116-
elems: parse_style_values(prop_or_spreads.clone(), Platform::Harmony),
117+
elems: parse_style_values(item.decls.clone(), Platform::Harmony),
117118
})),
118119
}))),
120+
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
121+
key: PropName::Ident(Ident::new("has_env".into(), DUMMY_SP)),
122+
value: Box::new(Expr::Lit(Lit::Bool(Bool { span: DUMMY_SP, value: item.has_env }))),
123+
})))
119124
];
120125

121126
if pseudo_key.len() > 0 {

src/parse_style_properties.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ use crate::{
7575
utils::lowercase_first,
7676
};
7777

78-
pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<StyleValueType> {
78+
#[derive(Debug, Clone)]
79+
pub struct DeclsAndVars {
80+
pub decls:Vec<StyleValueType>,
81+
pub has_env: bool
82+
}
83+
84+
pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> DeclsAndVars {
7985
let mut final_properties = vec![];
86+
let mut has_env = false;
8087
for (id, value) in properties.iter() {
8188
let mut is_env: bool = false;
8289
match value {
@@ -98,6 +105,7 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<Style
98105
_ => {}
99106
};
100107
if is_env {
108+
has_env = true;
101109
continue;
102110
}
103111

@@ -539,5 +547,8 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec<Style
539547
}
540548
}
541549

542-
final_properties
550+
DeclsAndVars {
551+
has_env: has_env,
552+
decls: final_properties
553+
}
543554
}

src/style_parser.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{cell::RefCell, convert::Infallible, rc::Rc};
22

33
use super::parse_style_properties::parse_style_properties;
4+
use crate::parse_style_properties::DeclsAndVars;
45
use crate::{generate_expr_enum, generate_expr_lit_str};
56
use crate::style_propetries::font_weight::{self, FontWeight};
67
use crate::style_propetries::style_property_enum::ArkUI_FontWeight;
@@ -30,7 +31,7 @@ use crate::style_propetries::style_media::StyleMedia;
3031
pub type StyleValue = Vec<StyleValueType>;
3132
#[derive(Debug)]
3233
pub struct StyleData {
33-
pub all_style: Rc<RefCell<IndexMap<(u32, String), StyleValue>>>,
34+
pub all_style: Rc<RefCell<IndexMap<(u32, String), DeclsAndVars>>>,
3435
pub all_keyframes: Rc<RefCell<IndexMap<(u32, String), Vec<KeyFrameItem>>>>,
3536
pub all_medias: Rc<RefCell<Vec<StyleMedia>>>,
3637
pub all_fonts: Rc<RefCell<Vec<FontFaceItem>>>,
@@ -213,7 +214,7 @@ impl<'i> Visitor<'i> for StyleVisitor<'i> {
213214
KeyframeSelector::From => 0.0,
214215
KeyframeSelector::To => 1.0,
215216
},
216-
declarations: parse_style_properties(&properties),
217+
declarations: parse_style_properties(&properties).decls,
217218
};
218219

219220
keyframe_data.keyframes.push(keyframe_item)

0 commit comments

Comments
 (0)