Skip to content

Commit 8a28184

Browse files
committed
Fix lint
1 parent 8cb7697 commit 8a28184

5 files changed

Lines changed: 113 additions & 109 deletions

File tree

libs/css/src/style_selector.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ impl Display for AtRuleKind {
3232
}
3333
}
3434

35+
impl From<&str> for AtRuleKind {
36+
fn from(value: &str) -> Self {
37+
match value {
38+
"media" => AtRuleKind::Media,
39+
"supports" => AtRuleKind::Supports,
40+
"container" => AtRuleKind::Container,
41+
"layer" => AtRuleKind::Layer,
42+
_ => unreachable!(),
43+
}
44+
}
45+
}
46+
3547
#[derive(Debug, PartialEq, Clone, Hash, Eq, Serialize, Deserialize)]
3648
pub enum StyleSelector {
3749
At {
@@ -467,4 +479,19 @@ mod tests {
467479
fn test_at_rule_kind_display(#[case] kind: AtRuleKind, #[case] expected: &str) {
468480
assert_eq!(format!("{kind}"), expected);
469481
}
482+
483+
#[rstest]
484+
#[case("media", AtRuleKind::Media)]
485+
#[case("supports", AtRuleKind::Supports)]
486+
#[case("container", AtRuleKind::Container)]
487+
#[case("layer", AtRuleKind::Layer)]
488+
fn test_at_rule_kind_from_str(#[case] input: &str, #[case] expected: AtRuleKind) {
489+
assert_eq!(AtRuleKind::from(input), expected);
490+
}
491+
492+
#[test]
493+
#[should_panic]
494+
fn test_at_rule_kind_from_str_unknown() {
495+
let _ = AtRuleKind::from("unknown");
496+
}
470497
}

libs/extractor/src/extractor/extract_global_style_from_expression.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ pub fn extract_global_style_from_expression<'a>(
183183
ref mut st,
184184
)) = style
185185
{
186-
// Skip @layer property - it's not a CSS property
187-
if st.property() == "@layer" {
188-
continue;
189-
}
190-
// Set layer on other styles
191-
if let Some(ref layer) = layer_name {
192-
st.layer = Some(layer.clone());
186+
// Skip @layer property - it's not a CSS property, set layer on other styles
187+
if st.property() != "@layer" {
188+
if let Some(ref layer) = layer_name {
189+
st.layer = Some(layer.clone());
190+
}
191+
styles.push(style);
193192
}
193+
} else {
194+
styles.push(style);
194195
}
195-
styles.push(style);
196196
}
197197
}
198198
}

libs/extractor/src/extractor/extract_style_from_expression.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,7 @@ pub fn extract_style_from_expression<'a>(
254254
&& let Some(query) = get_string_by_property_key(&o.key)
255255
{
256256
let at_selector = StyleSelector::At {
257-
kind: match at_rule {
258-
"media" => css::style_selector::AtRuleKind::Media,
259-
"supports" => css::style_selector::AtRuleKind::Supports,
260-
"container" => css::style_selector::AtRuleKind::Container,
261-
_ => unreachable!(),
262-
},
257+
kind: at_rule.into(),
263258
query: query.to_string(),
264259
selector: selector.as_ref().map(|s| s.to_string()),
265260
};

libs/extractor/src/lib.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -252,54 +252,53 @@ fn extract_class_map_from_code(
252252
..
253253
} = Parser::new(&allocator, partial_code, source_type).parse();
254254
if panicked {
255-
return Ok(std::collections::HashMap::new());
256-
}
257-
258-
let mut visitor = DevupVisitor::new(
259-
&allocator,
260-
filename,
261-
&option.package,
262-
css_files,
263-
if !option.single_css {
264-
Some(filename.to_string())
265-
} else {
266-
None
267-
},
268-
);
269-
visitor.visit_program(&mut program);
270-
271-
let result = Codegen::new().build(&program);
272-
273-
// Parse the output code to extract class name assignments
274-
// Format: const styleName = "className" or const styleName = "className1 className2"
275-
let mut class_map = std::collections::HashMap::new();
276-
for line in result.code.lines() {
277-
let line = line.trim();
278-
if line.starts_with("const ") || line.starts_with("export const ") {
279-
// Parse: [export] const name = "value"
280-
let after_const = if line.starts_with("export ") {
281-
line.strip_prefix("export const ").unwrap_or(line)
255+
Ok(std::collections::HashMap::new())
256+
} else {
257+
let mut visitor = DevupVisitor::new(
258+
&allocator,
259+
filename,
260+
&option.package,
261+
css_files,
262+
if !option.single_css {
263+
Some(filename.to_string())
282264
} else {
283-
line.strip_prefix("const ").unwrap_or(line)
284-
};
285-
286-
if let Some((name, rest)) = after_const.split_once(" = ") {
287-
// Extract value from "value" or "value";
288-
let value = rest
289-
.trim_start_matches('"')
290-
.trim_end_matches(';')
291-
.trim_end_matches('"');
292-
293-
if style_names.contains(name) {
294-
// For multi-class values like "a b", take the first class
295-
let first_class = value.split_whitespace().next().unwrap_or(value);
296-
class_map.insert(name.to_string(), first_class.to_string());
265+
None
266+
},
267+
);
268+
visitor.visit_program(&mut program);
269+
270+
let result = Codegen::new().build(&program);
271+
272+
// Parse the output code to extract class name assignments
273+
// Format: const styleName = "className" or const styleName = "className1 className2"
274+
let mut class_map = std::collections::HashMap::new();
275+
for line in result.code.lines() {
276+
let line = line.trim();
277+
if line.starts_with("const ") || line.starts_with("export const ") {
278+
// Parse: [export] const name = "value"
279+
let after_const = if line.starts_with("export ") {
280+
line.strip_prefix("export const ").unwrap_or(line)
281+
} else {
282+
line.strip_prefix("const ").unwrap_or(line)
283+
};
284+
285+
if let Some((name, rest)) = after_const.split_once(" = ") {
286+
// Extract value from "value" or "value";
287+
let value = rest
288+
.trim_start_matches('"')
289+
.trim_end_matches(';')
290+
.trim_end_matches('"');
291+
292+
if style_names.contains(name) {
293+
// For multi-class values like "a b", take the first class
294+
let first_class = value.split_whitespace().next().unwrap_or(value);
295+
class_map.insert(name.to_string(), first_class.to_string());
296+
}
297297
}
298298
}
299299
}
300+
Ok(class_map)
300301
}
301-
302-
Ok(class_map)
303302
}
304303

305304
/// Check if the code has an import from the specified package

libs/extractor/src/vanilla_extract.rs

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -343,26 +343,12 @@ fn js_value_to_json(value: &JsValue, context: &mut Context) -> String {
343343
}
344344

345345
// Fallback: simple conversion using boa_engine 0.21 API
346-
match value.get_type() {
347-
boa_engine::value::Type::String => {
348-
format!(
349-
"\"{}\"",
350-
value
351-
.to_string(context)
352-
.unwrap_or_default()
353-
.to_std_string_escaped()
354-
)
355-
}
356-
boa_engine::value::Type::Boolean => value.to_boolean().to_string(),
357-
boa_engine::value::Type::Null => "null".to_string(),
358-
boa_engine::value::Type::Undefined => "undefined".to_string(),
359-
boa_engine::value::Type::Number => {
360-
if let Ok(n) = value.to_number(context) {
361-
n.to_string()
362-
} else {
363-
"NaN".to_string()
364-
}
346+
match value.variant() {
347+
boa_engine::value::JsVariant::String(str) => {
348+
format!("\"{}\"", str.to_std_string_escaped())
365349
}
350+
boa_engine::value::JsVariant::Null => "null".to_string(),
351+
boa_engine::value::JsVariant::Undefined => "undefined".to_string(),
366352
_ => "{}".to_string(),
367353
}
368354
}
@@ -855,44 +841,41 @@ fn register_vanilla_extract_apis(
855841

856842
// Check if argument is an array (composition syntax)
857843
let (json, bases) = if let Some(obj) = style_obj.as_object() {
858-
if let Ok(length_val) = obj.get(js_string!("length"), ctx) {
859-
if let Some(len) = length_val.as_number() {
860-
// It's an array - handle composition
861-
let len = len as u32;
862-
let mut base_classes = Vec::new();
863-
let mut merged_styles = String::from("{");
864-
let mut first_style = true;
865-
866-
for i in 0..len {
867-
if let Ok(elem) = obj.get(i, ctx) {
868-
if let Some(base_str) = elem.as_string() {
869-
// It's a base class reference (string)
870-
base_classes.push(base_str.to_std_string_escaped());
871-
} else if elem.is_object() {
872-
// It's a style object - merge it
873-
let elem_json = js_value_to_json(&elem, ctx);
874-
// Strip outer braces and merge
875-
let inner = elem_json
876-
.trim()
877-
.trim_start_matches('{')
878-
.trim_end_matches('}')
879-
.trim();
880-
if !inner.is_empty() {
881-
if !first_style {
882-
merged_styles.push(',');
883-
}
884-
merged_styles.push_str(inner);
885-
first_style = false;
844+
if let Ok(length_val) = obj.get(js_string!("length"), ctx)
845+
&& let Some(len) = length_val.as_number()
846+
{
847+
// It's an array - handle composition
848+
let len = len as u32;
849+
let mut base_classes = Vec::new();
850+
let mut merged_styles = String::from("{");
851+
let mut first_style = true;
852+
853+
for i in 0..len {
854+
if let Ok(elem) = obj.get(i, ctx) {
855+
if let Some(base_str) = elem.as_string() {
856+
// It's a base class reference (string)
857+
base_classes.push(base_str.to_std_string_escaped());
858+
} else if elem.is_object() {
859+
// It's a style object - merge it
860+
let elem_json = js_value_to_json(&elem, ctx);
861+
// Strip outer braces and merge
862+
let inner = elem_json
863+
.trim()
864+
.trim_start_matches('{')
865+
.trim_end_matches('}')
866+
.trim();
867+
if !inner.is_empty() {
868+
if !first_style {
869+
merged_styles.push(',');
886870
}
871+
merged_styles.push_str(inner);
872+
first_style = false;
887873
}
888874
}
889875
}
890-
merged_styles.push('}');
891-
(merged_styles, base_classes)
892-
} else {
893-
// Not an array, just a style object
894-
(js_value_to_json(style_obj, ctx), Vec::new())
895876
}
877+
merged_styles.push('}');
878+
(merged_styles, base_classes)
896879
} else {
897880
// No length property, just a style object
898881
(js_value_to_json(style_obj, ctx), Vec::new())

0 commit comments

Comments
 (0)