Skip to content

Commit 585ba16

Browse files
committed
Fix property key logic
1 parent e4f1789 commit 585ba16

15 files changed

+181
-104
lines changed

libs/css/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ pub fn disassemble_property(property: &str) -> Vec<String> {
6868
})
6969
}
7070

71+
pub fn add_selector_params(selector: StyleSelector, params: &str) -> StyleSelector {
72+
match selector {
73+
StyleSelector::Selector(value) => StyleSelector::Selector(format!("{}({})", value, params)),
74+
StyleSelector::Global(value, file) => {
75+
StyleSelector::Global(format!("{}({})", value, params), file)
76+
}
77+
StyleSelector::Media { query, selector } => StyleSelector::Media {
78+
query: query.to_string(),
79+
selector: selector.map(|s| format!("{}({})", s, params)),
80+
},
81+
}
82+
}
83+
7184
pub fn get_enum_property_value(property: &str, value: &str) -> Option<Vec<(String, String)>> {
7285
if let Some(map) = GLOBAL_ENUM_STYLE_PROPERTY.get(property) {
7386
if let Some(map) = map.get(value) {
@@ -677,4 +690,32 @@ mod tests {
677690
assert_eq!(keyframes_to_keyframes_name("spin", None), "k-spin");
678691
assert_eq!(keyframes_to_keyframes_name("spin1", None), "k-spin1");
679692
}
693+
694+
#[test]
695+
fn test_add_selector_params() {
696+
assert_eq!(
697+
add_selector_params(StyleSelector::Selector("hover:is".to_string()), "test"),
698+
StyleSelector::Selector("hover:is(test)".to_string())
699+
);
700+
assert_eq!(
701+
add_selector_params(
702+
StyleSelector::Global("&:is".to_string(), "file.ts".to_string()),
703+
"test"
704+
),
705+
StyleSelector::Global("&:is(test)".to_string(), "file.ts".to_string())
706+
);
707+
assert_eq!(
708+
add_selector_params(
709+
StyleSelector::Media {
710+
query: "print".to_string(),
711+
selector: Some("&:is".to_string())
712+
},
713+
"test"
714+
),
715+
StyleSelector::Media {
716+
query: "print".to_string(),
717+
selector: Some("&:is(test)".to_string())
718+
}
719+
);
720+
}
680721
}

libs/extractor/src/extractor/extract_global_style_from_expression.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
extractor::{
1111
GlobalExtractResult, extract_style_from_expression::extract_style_from_expression,
1212
},
13-
utils::get_string_by_literal_expression,
13+
utils::{get_string_by_literal_expression, get_string_by_property_key},
1414
};
1515
use css::{
1616
disassemble_property,
@@ -19,7 +19,7 @@ use css::{
1919
};
2020
use oxc_ast::{
2121
AstBuilder,
22-
ast::{ArrayExpressionElement, Expression, ObjectPropertyKind, PropertyKey},
22+
ast::{ArrayExpressionElement, Expression, ObjectPropertyKind},
2323
};
2424

2525
pub fn extract_global_style_from_expression<'a>(
@@ -33,20 +33,7 @@ pub fn extract_global_style_from_expression<'a>(
3333
for p in obj.properties.iter_mut() {
3434
match p {
3535
ObjectPropertyKind::ObjectProperty(o) => {
36-
if let Some(name) = if let PropertyKey::StaticIdentifier(ident) = &o.key {
37-
Some(ident.name.to_string())
38-
} else if let PropertyKey::StringLiteral(s) = &o.key {
39-
Some(s.value.to_string())
40-
} else if let PropertyKey::TemplateLiteral(t) = &o.key {
41-
Some(
42-
t.quasis
43-
.iter()
44-
.map(|q| q.value.raw.as_str())
45-
.collect::<String>(),
46-
)
47-
} else {
48-
None
49-
} {
36+
if let Some(name) = get_string_by_property_key(&o.key) {
5037
if name == "imports" {
5138
if let Expression::ArrayExpression(arr) = &o.value {
5239
for p in arr.elements.iter() {
@@ -108,11 +95,11 @@ pub fn extract_global_style_from_expression<'a>(
10895
.iter()
10996
.filter_map(|p| {
11097
if let ObjectPropertyKind::ObjectProperty(o) = p
111-
&& let PropertyKey::StaticIdentifier(ident) = &o.key
98+
&& let Some(property_name) = get_string_by_property_key(&o.key)
11299
&& let Some(s) = get_string_by_literal_expression(&o.value)
113100
{
114101
Some(
115-
disassemble_property(&ident.name)
102+
disassemble_property(&property_name)
116103
.iter()
117104
.map(|p| {
118105
let v = if check_multi_css_optimize(p) { optimize_mutli_css_value(&s) } else { s.clone() };

libs/extractor/src/extractor/extract_keyframes_from_expression.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ use crate::{
55
ExtractResult, KeyframesExtractResult,
66
extract_style_from_expression::extract_style_from_expression,
77
},
8+
utils::get_string_by_property_key,
89
};
910
use oxc_ast::{
1011
AstBuilder,
11-
ast::{Expression, ObjectPropertyKind, PropertyKey},
12+
ast::{Expression, ObjectPropertyKind},
1213
};
1314

1415
pub fn extract_keyframes_from_expression<'a>(
@@ -20,22 +21,7 @@ pub fn extract_keyframes_from_expression<'a>(
2021
if let Expression::ObjectExpression(obj) = expression {
2122
for p in obj.properties.iter_mut() {
2223
if let ObjectPropertyKind::ObjectProperty(o) = p
23-
&& let Some(name) = if let PropertyKey::StaticIdentifier(ident) = &o.key {
24-
Some(ident.name.to_string())
25-
} else if let PropertyKey::StringLiteral(s) = &o.key {
26-
Some(s.value.to_string())
27-
} else if let PropertyKey::TemplateLiteral(t) = &o.key {
28-
Some(
29-
t.quasis
30-
.iter()
31-
.map(|q| q.value.raw.as_str())
32-
.collect::<String>(),
33-
)
34-
} else if let PropertyKey::NumericLiteral(n) = &o.key {
35-
Some(n.value.to_string())
36-
} else {
37-
None
38-
}
24+
&& let Some(name) = get_string_by_property_key(&o.key)
3925
{
4026
let ExtractResult { styles, .. } =
4127
extract_style_from_expression(ast_builder, None, &mut o.value, 0, &None);

libs/extractor/src/extractor/extract_style_from_expression.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ use crate::{
1010
},
1111
utils::{
1212
expression_to_code, get_number_by_literal_expression, get_string_by_literal_expression,
13-
is_same_expression,
13+
get_string_by_property_key, is_same_expression,
1414
},
1515
};
1616
use css::{
17-
disassemble_property, get_enum_property_map, get_enum_property_value,
17+
add_selector_params, disassemble_property, get_enum_property_map, get_enum_property_value,
1818
is_special_property::is_special_property, style_selector::StyleSelector, utils::to_kebab_case,
1919
};
2020
use oxc_allocator::CloneIn;
2121
use oxc_ast::{
2222
AstBuilder,
2323
ast::{
24-
BinaryOperator, Expression, LogicalOperator, ObjectPropertyKind, PropertyKey,
24+
BinaryOperator, Expression, LogicalOperator, ObjectPropertyKind,
2525
TemplateElementValue, UnaryOperator,
2626
},
2727
};
@@ -50,9 +50,8 @@ pub fn extract_style_from_expression<'a>(
5050
let mut prop = obj.properties.remove(idx);
5151
if !match &mut prop {
5252
ObjectPropertyKind::ObjectProperty(prop) => {
53-
if let PropertyKey::StaticIdentifier(ident) = &prop.key
54-
&& let name = ident.name.as_str()
55-
&& !is_special_property(name)
53+
if let Some(name) = get_string_by_property_key(&prop.key)
54+
&& !is_special_property(&name)
5655
{
5756
let property_name = name.to_string();
5857
for name in disassemble_property(&property_name) {
@@ -588,22 +587,12 @@ pub fn extract_style_from_expression<'a>(
588587
}
589588
});
590589

591-
let selector = selector.clone().map(|s| match &s {
592-
StyleSelector::Selector(selector) => {
593-
if let Some(params) = params {
594-
StyleSelector::Selector(format!("{}({})", selector, params))
595-
} else {
596-
s
597-
}
598-
}
599-
StyleSelector::Global(selector, file) => {
600-
if let Some(params) = params {
601-
StyleSelector::Global(format!("{}({})", selector, params), file.clone())
602-
} else {
603-
s
604-
}
590+
let selector = selector.clone().map(|s| {
591+
if let Some(params) = params {
592+
add_selector_params(s, &params)
593+
} else {
594+
s
605595
}
606-
_ => s,
607596
});
608597

609598
for p in obj.properties.iter_mut() {

libs/extractor/src/extractor/extract_style_from_member_expression.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ use crate::{
44
ExtractResult,
55
extract_style_from_expression::{dynamic_style, extract_style_from_expression},
66
},
7-
utils::{get_number_by_literal_expression, get_string_by_literal_expression},
7+
utils::{
8+
get_number_by_literal_expression, get_string_by_literal_expression,
9+
get_string_by_property_key,
10+
},
811
};
912
use css::style_selector::StyleSelector;
1013
use oxc_allocator::CloneIn;
1114
use oxc_ast::{
1215
AstBuilder,
1316
ast::{
1417
ArrayExpressionElement, ComputedMemberExpression, Expression, ObjectPropertyKind,
15-
PropertyKey,
1618
},
1719
};
1820
use oxc_span::SPAN;
@@ -113,8 +115,8 @@ pub(super) fn extract_style_from_member_expression<'a>(
113115
let mut etc = None;
114116
for p in obj.properties.iter_mut() {
115117
if let ObjectPropertyKind::ObjectProperty(o) = p {
116-
if let PropertyKey::StaticIdentifier(ref pk) = o.key
117-
&& pk.name == k
118+
if let Some(property_name) = get_string_by_property_key(&o.key)
119+
&& property_name == k
118120
{
119121
return ExtractResult {
120122
styles: extract_style_from_expression(
@@ -154,12 +156,10 @@ pub(super) fn extract_style_from_member_expression<'a>(
154156

155157
for p in obj.properties.iter_mut() {
156158
if let ObjectPropertyKind::ObjectProperty(o) = p
157-
&& let PropertyKey::StaticIdentifier(_)
158-
| PropertyKey::NumericLiteral(_)
159-
| PropertyKey::StringLiteral(_) = o.key
159+
&& let Some(property_name) = get_string_by_property_key(&o.key)
160160
{
161161
map.insert(
162-
o.key.name().unwrap().to_string(),
162+
property_name,
163163
Box::new(ExtractStyleProp::StaticArray(
164164
extract_style_from_expression(
165165
ast_builder,

libs/extractor/src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7409,9 +7409,28 @@ keyframes({
74097409
_hover: {
74107410
bg: "blue"
74117411
},
7412-
_print: {
7413-
bg: "green",
7414-
},
7412+
bg: "red"
7413+
}
7414+
})
7415+
"#,
7416+
ExtractOption {
7417+
package: "@devup-ui/core".to_string(),
7418+
css_dir: "@devup-ui/core".to_string(),
7419+
single_css: false,
7420+
import_main_css: false
7421+
}
7422+
)
7423+
.unwrap()
7424+
));
7425+
7426+
reset_class_map();
7427+
assert_debug_snapshot!(ToBTreeSet::from(
7428+
extract(
7429+
"test.tsx",
7430+
r#"import {css} from '@devup-ui/core'
7431+
css({
7432+
"_is": {
7433+
params: ["test"],
74157434
bg: "red"
74167435
}
74177436
})

libs/extractor/src/prop_modify_utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::ExtractStyleProp;
22
use crate::gen_class_name::gen_class_names;
33
use crate::gen_style::gen_styles;
4+
use crate::utils::get_string_by_property_key;
45
use oxc_allocator::CloneIn;
56
use oxc_ast::AstBuilder;
67
use oxc_ast::ast::JSXAttributeName::Identifier;
@@ -27,8 +28,7 @@ pub fn modify_prop_object<'a>(
2728
let prop = props.remove(idx);
2829
match prop {
2930
ObjectPropertyKind::ObjectProperty(attr) => {
30-
if let PropertyKey::StaticIdentifier(ident) = &attr.key {
31-
let name = ident.name.as_str();
31+
if let Some(name) = get_string_by_property_key(&attr.key) {
3232
if name == "className" {
3333
class_name_prop = Some(attr.value.clone_in(ast_builder.allocator));
3434
} else if name == "style" {
@@ -430,10 +430,8 @@ pub fn convert_style_vars<'a>(
430430
let mut prop = obj.properties.remove(idx);
431431

432432
if let ObjectPropertyKind::ObjectProperty(p) = &mut prop {
433-
let name = if let PropertyKey::StaticIdentifier(ident) = &p.key {
434-
Some(ident.name)
435-
} else if let PropertyKey::StringLiteral(ident) = &p.key {
436-
Some(ident.value)
433+
let name = if let Some(name) = get_string_by_property_key(&p.key) {
434+
Some(name)
437435
} else {
438436
obj.properties.insert(
439437
idx,

libs/extractor/src/snapshots/extractor__tests__extract_advenced_selector-7.snap

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: libs/extractor/src/lib.rs
3-
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {globalCss} from '@devup-ui/core'\n globalCss({\n \"_is\": {\n params: [\"test\", variable],\n _hover: {\n bg: \"blue\"\n },\n _print: {\n bg: \"green\",\n },\n bg: \"red\"\n }\n })\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: false, import_main_css: false\n}).unwrap())"
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {globalCss} from '@devup-ui/core'\n globalCss({\n \"_is\": {\n params: [\"test\", variable],\n _hover: {\n bg: \"blue\"\n },\n bg: \"red\"\n }\n })\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: false, import_main_css: false\n}).unwrap())"
44
---
55
ToBTreeSet {
66
styles: {
@@ -20,22 +20,6 @@ ToBTreeSet {
2020
),
2121
},
2222
),
23-
Static(
24-
ExtractStaticStyle {
25-
property: "background",
26-
value: "green",
27-
level: 0,
28-
selector: Some(
29-
Global(
30-
"*:is(test):print",
31-
"test.tsx",
32-
),
33-
),
34-
style_order: Some(
35-
0,
36-
),
37-
},
38-
),
3923
Static(
4024
ExtractStaticStyle {
4125
property: "background",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import {css} from '@devup-ui/core'\n css({\n \"_is\": {\n params: [\"test\"],\n bg: \"red\"\n }\n })\n \"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: false, import_main_css: false\n}).unwrap())"
4+
---
5+
ToBTreeSet {
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "red",
11+
level: 0,
12+
selector: Some(
13+
Selector(
14+
"&:is(test)",
15+
),
16+
),
17+
style_order: None,
18+
},
19+
),
20+
},
21+
code: "import \"@devup-ui/core/devup-ui-0.css\";\n\"a-a\";\n",
22+
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
---
22
source: libs/extractor/src/lib.rs
3-
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import { globalCss } from \"@devup-ui/core\";\nglobalCss({\n [1]: {\n bg: \"red\"\n }\n})\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
3+
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import { globalCss } from \"@devup-ui/core\";\nglobalCss({\n [1]: {\n bg: \"red\"\n }\n})\n\"#,\nExtractOption\n{\n package: \"@devup-ui/core\".to_string(), css_dir:\n \"@devup-ui/core\".to_string(), single_css: true, import_main_css: false\n}).unwrap())"
44
---
55
ToBTreeSet {
6-
styles: {},
7-
code: ";\n",
6+
styles: {
7+
Static(
8+
ExtractStaticStyle {
9+
property: "background",
10+
value: "red",
11+
level: 0,
12+
selector: Some(
13+
Global(
14+
"1",
15+
"test.tsx",
16+
),
17+
),
18+
style_order: Some(
19+
0,
20+
),
21+
},
22+
),
23+
},
24+
code: "import \"@devup-ui/core/devup-ui.css\";\n;\n",
825
}

0 commit comments

Comments
 (0)