Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ jobs:
- run: |
pnpm build
pnpm lint
# rust coverage issue
echo 'max_width = 1000' > .rustfmt.toml
echo 'tab_spaces = 4' >> .rustfmt.toml
echo 'newline_style = "Unix"' >> .rustfmt.toml
echo 'fn_call_width = 1000' >> .rustfmt.toml
echo 'fn_params_layout = "Compressed"' >> .rustfmt.toml
echo 'chain_width = 1000' >> .rustfmt.toml
echo 'merge_derives = true' >> .rustfmt.toml
echo 'use_small_heuristics = "Default"' >> .rustfmt.toml
cargo fmt
pnpm test
- name: Benchmark
run: pnpm benchmark
Expand Down
6 changes: 3 additions & 3 deletions bindings/devup-ui-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,11 @@ mod tests {
#[test]
#[serial]
fn test_debug() {
assert_eq!(is_debug(), false);
assert!(!is_debug());
set_debug(true);
assert_eq!(is_debug(), true);
assert!(is_debug());
set_debug(false);
assert_eq!(is_debug(), false);
assert!(!is_debug());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions libs/css/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ mod tests {
"cls",
Some(&StyleSelector::Media {
query: "print".to_string(),
selector: None,
selector: None
})
),
".cls"
Expand All @@ -465,7 +465,7 @@ mod tests {
"cls",
Some(&StyleSelector::Media {
query: "print".to_string(),
selector: Some("&:hover".to_string()),
selector: Some("&:hover".to_string())
})
),
".cls:hover"
Expand Down
2 changes: 1 addition & 1 deletion libs/css/src/optimize_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn optimize_value(value: &str) -> String {
}

fn optimize_color(value: &str) -> String {
let mut ret = value.to_string().to_uppercase();
let mut ret = value.to_uppercase();

if ret.len() == 6 {
let ch = ret.chars().collect::<Vec<char>>();
Expand Down
11 changes: 10 additions & 1 deletion libs/extractor/src/css_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn optimize_css_block(css: &str) -> String {
.split(";")
.map(|s| {
if !s.contains(":") {
s.to_string().trim().to_string()
s.trim().to_string()
} else {
let mut iter = s.split(":");
let property = iter.next().unwrap().trim();
Expand Down Expand Up @@ -470,11 +470,20 @@ mod tests {
("50%", vec![("color", "red"), ("background", "blue")]),
],
)]
// error case
#[case(
"50% { color: red ; background: blue ",
vec![
],
)]
fn test_keyframes_to_keyframes_style(
#[case] input: &str,
#[case] expected: Vec<(&str, Vec<(&str, &str)>)>,
) {
let styles = keyframes_to_keyframes_style(input);
if styles.len() != expected.len() {
panic!("styles.len() != expected.len()");
}
for (expected_key, expected_styles) in styles.iter() {
let styles = expected_styles;
let mut result: Vec<(&str, &str)> = styles
Expand Down
4 changes: 2 additions & 2 deletions libs/extractor/src/extract_style/extract_dynamic_style.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use css::{
optimize_value::optimize_value, sheet_to_classname,
sheet_to_variable_name, style_selector::StyleSelector,
optimize_value::optimize_value, sheet_to_classname, sheet_to_variable_name,
style_selector::StyleSelector,
};

use crate::extract_style::{ExtractStyleProperty, style_property::StyleProperty};
Expand Down
5 changes: 1 addition & 4 deletions libs/extractor/src/extract_style/extract_static_style.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use css::{
optimize_value::optimize_value, sheet_to_classname,
style_selector::StyleSelector,
};
use css::{optimize_value::optimize_value, sheet_to_classname, style_selector::StyleSelector};

use crate::{
extract_style::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ pub fn extract_global_style_from_expression<'a>(
t.quasis
.iter()
.map(|q| q.value.raw.as_str())
.collect::<Vec<_>>()
.join("")
.collect::<String>()
} else {
continue;
};
Expand All @@ -46,8 +45,7 @@ pub fn extract_global_style_from_expression<'a>(
t.quasis
.iter()
.map(|q| q.value.raw.as_str())
.collect::<Vec<_>>()
.join("")
.collect::<String>()
.trim()
.to_string()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub fn extract_keyframes_from_expression<'a>(
t.quasis
.iter()
.map(|q| q.value.raw.as_str())
.collect::<Vec<_>>()
.join("")
.collect::<String>()
} else if let PropertyKey::NumericLiteral(n) = &o.key {
n.value.to_string()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ pub fn extract_style_from_expression<'a>(
&tmp.quasis
.iter()
.map(|q| q.value.raw.as_str())
.collect::<Vec<_>>()
.join(""),
.collect::<String>(),
level,
selector,
),
Expand Down
2 changes: 1 addition & 1 deletion libs/extractor/src/gen_class_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn merge_expression_for_class_name<'a>(
let mut unknown_expr = vec![];
for expr in expressions {
if let Expression::StringLiteral(str) = &expr {
class_names.push(str.value.to_string().trim().to_string())
class_names.push(str.value.trim().to_string())
} else {
unknown_expr.push(expr);
}
Expand Down
42 changes: 19 additions & 23 deletions libs/extractor/src/gen_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,25 @@ fn gen_style<'a>(
let mut properties = vec![];
match style {
ExtractStyleProp::Static(st) => {
if let Some(ex) = st.extract() {
match ex {
StyleProperty::ClassName(_) => {}
StyleProperty::Variable {
variable_name,
identifier,
..
} => {
properties.push(ast_builder.object_property_kind_object_property(
SPAN,
PropertyKind::Init,
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
SPAN,
ast_builder.atom(&variable_name),
None,
)),
ast_builder.expression_identifier(SPAN, ast_builder.atom(&identifier)),
false,
false,
false,
));
}
}
if let Some(StyleProperty::Variable {
variable_name,
identifier,
..
}) = st.extract()
{
properties.push(ast_builder.object_property_kind_object_property(
SPAN,
PropertyKind::Init,
PropertyKey::StringLiteral(ast_builder.alloc_string_literal(
SPAN,
ast_builder.atom(&variable_name),
None,
)),
ast_builder.expression_identifier(SPAN, ast_builder.atom(&identifier)),
false,
false,
false,
));
}
}
ExtractStyleProp::StaticArray(res) => {
Expand Down
62 changes: 27 additions & 35 deletions libs/extractor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ mod tests {
r#"import {Box} from '@devup-ui/core'
<Box padding={1} ref={ref} data-test={1} role={2} children={[]} onClick={()=>{}} aria-valuenow={24} key={2} tabIndex={1} id="id" />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
ExtractOption { package: "@devup-ui/core".to_string(), css_file: None }
)
.unwrap()
));
Expand Down Expand Up @@ -2595,37 +2592,13 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
#[serial]
fn support_transpile_cjs() {
reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(extract(
"test.cjs",
r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(r.Text,{typography:"header",children:"typo"}),e.jsx(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#,
ExtractOption {
package: "@devup-ui/react".to_string(),
css_file: None
}
)
.unwrap()));
assert_debug_snapshot!(ToBTreeSet::from(extract("test.cjs", r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(r.Text,{typography:"header",children:"typo"}),e.jsx(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#, ExtractOption { package: "@devup-ui/react".to_string(), css_file: None }).unwrap()));

reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(extract(
"test.cjs",
r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const {jsx:e1, jsxs:e2}=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e2("div",{children:[e1(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e1(r.Text,{typography:"header",children:"typo"}),e1(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#,
ExtractOption {
package: "@devup-ui/react".to_string(),
css_file: None
}
)
.unwrap()));
assert_debug_snapshot!(ToBTreeSet::from(extract("test.cjs", r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const {jsx:e1, jsxs:e2}=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e2("div",{children:[e1(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e1(r.Text,{typography:"header",children:"typo"}),e1(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#, ExtractOption { package: "@devup-ui/react".to_string(), css_file: None }).unwrap()));

reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(extract(
"test.js",
r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(r.Text,{typography:"header",children:"typo"}),e.jsx(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#,
ExtractOption {
package: "@devup-ui/react".to_string(),
css_file: None
}
)
.unwrap()));
assert_debug_snapshot!(ToBTreeSet::from(extract("test.js", r#""use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("@devup-ui/react");function t(){return e.jsxs("div",{children:[e.jsx(r.Box,{_hover:{bg:"blue"},bg:"$text",color:"red",children:"hello"}),e.jsx(r.Text,{typography:"header",children:"typo"}),e.jsx(r.Flex,{as:"section",mt:2,children:"section"})]})}exports.Lib=t;"#, ExtractOption { package: "@devup-ui/react".to_string(), css_file: None }).unwrap()));
}

#[test]
Expand All @@ -2638,10 +2611,7 @@ e(o, { className: "a", bg: variable, style: { color: "blue" }, ...props })
r#"import {Flex} from '@devup-ui/core'
<Flex opacity={1} zIndex={2} fontWeight={900} scale={2} flex={1} lineHeight={1} tabSize={4} MozTabSize={4} />
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
ExtractOption { package: "@devup-ui/core".to_string(), css_file: None }
)
.unwrap()
));
Expand Down Expand Up @@ -5011,6 +4981,28 @@ keyframes({
[`50%`]: { opacity: 0.5 },
[`100%`]: { opacity: 1 }
})
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
css_file: None
}
)
.unwrap()
));
}
#[test]
#[serial]
fn extract_wrong_keyframs() {
reset_class_map();
assert_debug_snapshot!(ToBTreeSet::from(
extract(
"test.tsx",
r#"import { keyframes } from "@devup-ui/core";
keyframes({
from: { opacity: 0 },
[true]: { opacity: 0.5 },
to: { opacity: 1, color: dy }
})
"#,
ExtractOption {
package: "@devup-ui/core".to_string(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: libs/extractor/src/lib.rs
expression: "ToBTreeSet::from(extract(\"test.tsx\",\nr#\"import { keyframes } from \"@devup-ui/core\";\nkeyframes({\n from: { opacity: 0 },\n [true]: { opacity: 0.5 },\n to: { opacity: 1 }\n})\n\"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap())"
---
ToBTreeSet {
styles: {
Keyframes(
ExtractKeyframes {
keyframes: {
"from": [
ExtractStaticStyle {
property: "opacity",
value: "0",
level: 0,
selector: None,
style_order: None,
},
],
"to": [
ExtractStaticStyle {
property: "opacity",
value: "1",
level: 0,
selector: None,
style_order: None,
},
],
},
},
),
},
code: "import \"@devup-ui/core/devup-ui.css\";\n\"k0\";\n",
}
3 changes: 1 addition & 2 deletions libs/extractor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ pub(super) fn get_number_by_literal_expression(expr: &Expression) -> Option<f64>
.quasis
.iter()
.map(|q| q.value.raw.to_string())
.collect::<Vec<String>>()
.join("")
.collect::<String>()
.parse::<f64>()
.ok(),
Expression::NumericLiteral(num) => Some(num.value),
Expand Down
5 changes: 2 additions & 3 deletions libs/extractor/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> {
.quasi
.quasis
.iter()
.map(|quasi| quasi.value.raw.as_str())
.collect::<Vec<&str>>()
.join("");
.map(|quasi| quasi.value.raw.to_string())
.collect::<String>();
match css_type.as_ref() {
UtilType::Css => {
let mut styles = css_to_style(&css_str, 0, &None);
Expand Down
2 changes: 1 addition & 1 deletion libs/sheet/benches/my_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{Criterion, criterion_group, criterion_main};
use once_cell::sync::Lazy;
use regex::Regex;
use std::hint::black_box;
Expand Down
Loading