Skip to content

Commit

Permalink
refactor(css/minifier): Support stable rust (#8389)
Browse files Browse the repository at this point in the history
**Related issue:**
 - #8316
  • Loading branch information
magic-akari committed Dec 7, 2023
1 parent d3ba980 commit 305e72e
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 928 deletions.
9 changes: 3 additions & 6 deletions crates/swc_css_minifier/src/compressor/alpha_value.rs
Expand Up @@ -48,14 +48,11 @@ impl Compressor {
}

match component_value {
ComponentValue::Percentage(box Percentage {
span,
value: number,
}) if number.value % 10.0 == 0.0 => {
let new_value = number.value / 100.0;
ComponentValue::Percentage(percentage) if percentage.value.value % 10.0 == 0.0 => {
let new_value = percentage.value.value / 100.0;

*component_value = ComponentValue::Number(Box::new(Number {
span: *span,
span: percentage.span,
value: new_value,
raw: None,
}));
Expand Down
34 changes: 15 additions & 19 deletions crates/swc_css_minifier/src/compressor/angle.rs
Expand Up @@ -8,25 +8,21 @@ impl Compressor {
&mut self,
component_value: &mut ComponentValue,
) {
if self.ctx.in_transform_function {
match &component_value {
ComponentValue::Dimension(box Dimension::Angle(Angle {
value:
Number {
value: number_value,
..
},
span,
..
})) if *number_value == 0.0 => {
*component_value = ComponentValue::Number(Box::new(Number {
span: *span,
value: 0.0,
raw: None,
}));
}
_ => {}
}
if !self.ctx.in_transform_function {
return;
}

if let Some(span) = component_value
.as_dimension()
.and_then(|dimension| dimension.as_angle())
.filter(|angle| angle.value.value == 0.0)
.map(|angle| angle.span)
{
*component_value = ComponentValue::Number(Box::new(Number {
span,
value: 0.0,
raw: None,
}));
}
}

Expand Down
37 changes: 18 additions & 19 deletions crates/swc_css_minifier/src/compressor/calc_sum.rs
Expand Up @@ -1177,30 +1177,29 @@ impl Compressor {
match &component_value {
// Transform "calc(calc-sum)" into "simple value" when calc-sum is not a complex
// expression
ComponentValue::Function(box Function { name, value, .. })
if is_calc_function_name(name) && value.len() == 1 =>
ComponentValue::Function(function)
if is_calc_function_name(&function.name) && function.value.len() == 1 =>
{
match &value[0] {
ComponentValue::CalcSum(box CalcSum {
expressions: calc_sum_expressions,
..
}) if calc_sum_expressions.len() == 1 => match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
if let Some(cv) =
transform_calc_value_into_component_value(calc_value)
match &function.value[0] {
ComponentValue::CalcSum(calc_sum) if calc_sum.expressions.len() == 1 => {
match &calc_sum.expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
*component_value = cv;
if let Some(cv) =
transform_calc_value_into_component_value(calc_value)
{
*component_value = cv;
}
}
}
_ => {}
}
_ => {}
},
}
_ => {}
}
}
Expand Down
114 changes: 49 additions & 65 deletions crates/swc_css_minifier/src/compressor/color.rs
Expand Up @@ -224,42 +224,43 @@ impl Compressor {
}

fn get_alpha_value(&self, alpha_value: Option<&&ComponentValue>) -> Option<f64> {
match alpha_value {
Some(ComponentValue::AlphaValue(box AlphaValue::Number(Number { value, .. }))) => {
if *value > 1.0 {
return Some(1.0);
} else if *value < 0.0 {
return Some(0.0);
}
let Some(alpha_value) = alpha_value else {
return Some(1.0);
};

Some(*value)
}
Some(ComponentValue::AlphaValue(box AlphaValue::Percentage(Percentage {
value: Number { value, .. },
..
}))) => {
if *value > 100.0 {
return Some(1.0);
} else if *value < 0.0 {
return Some(0.0);
match &alpha_value {
ComponentValue::AlphaValue(alpha_value) => match &**alpha_value {
AlphaValue::Number(Number { value, .. }) => {
if *value > 1.0 {
return Some(1.0);
} else if *value < 0.0 {
return Some(0.0);
}

Some(*value)
}
AlphaValue::Percentage(Percentage {
value: Number { value, .. },
..
}) => {
if *value > 100.0 {
return Some(1.0);
} else if *value < 0.0 {
return Some(0.0);
}

Some(*value / 100.0)
}
Some(ComponentValue::Ident(box Ident { value, .. }))
if value.eq_ignore_ascii_case("none") =>
{
Some(0.0)
}
None => Some(1.0),
Some(*value / 100.0)
}
},
ComponentValue::Ident(ident) if ident.value.eq_ignore_ascii_case("none") => Some(0.0),
_ => None,
}
}

fn get_hue(&self, hue: Option<&&ComponentValue>) -> Option<f64> {
match hue {
Some(ComponentValue::Hue(box hue)) => {
let mut value = match hue {
Some(ComponentValue::Hue(hue)) => {
let mut value = match &**hue {
Hue::Number(Number { value, .. }) => *value,
Hue::Angle(Angle {
value: Number { value, .. },
Expand All @@ -276,9 +277,7 @@ impl Compressor {

Some(value)
}
Some(ComponentValue::Ident(box Ident { value, .. }))
if value.eq_ignore_ascii_case("none") =>
{
Some(ComponentValue::Ident(ident)) if ident.value.eq_ignore_ascii_case("none") => {
Some(0.0)
}
_ => None,
Expand All @@ -287,10 +286,8 @@ impl Compressor {

fn get_percentage(&self, percentage: Option<&&ComponentValue>) -> Option<f64> {
match percentage {
Some(ComponentValue::Percentage(box Percentage {
value: Number { value, .. },
..
})) => {
Some(ComponentValue::Percentage(percentage)) => {
let Number { value, .. } = &percentage.value;
if *value > 100.0 {
return Some(1.0);
} else if *value < 0.0 {
Expand All @@ -299,9 +296,7 @@ impl Compressor {

Some(*value / 100.0)
}
Some(ComponentValue::Ident(box Ident { value, .. }))
if value.eq_ignore_ascii_case("none") =>
{
Some(ComponentValue::Ident(ident)) if ident.value.eq_ignore_ascii_case("none") => {
Some(0.0)
}
_ => None,
Expand All @@ -313,30 +308,25 @@ impl Compressor {
number_or_percentage: Option<&&ComponentValue>,
) -> Option<f64> {
match number_or_percentage {
Some(ComponentValue::Number(box Number { value, .. })) => {
if *value > 255.0 {
Some(ComponentValue::Number(number)) => {
if number.value > 255.0 {
return Some(255.0);
} else if *value < 0.0 {
} else if number.value < 0.0 {
return Some(0.0);
}

Some(*value)
Some(number.value)
}
Some(ComponentValue::Percentage(box Percentage {
value: Number { value, .. },
..
})) => {
if *value > 100.0 {
Some(ComponentValue::Percentage(percentage)) => {
if percentage.value.value > 100.0 {
return Some(255.0);
} else if *value < 0.0 {
} else if percentage.value.value < 0.0 {
return Some(0.0);
}

Some((2.55 * *value).round())
Some((2.55 * percentage.value.value).round())
}
Some(ComponentValue::Ident(box Ident { value, .. }))
if value.eq_ignore_ascii_case("none") =>
{
Some(ComponentValue::Ident(ident)) if ident.value.eq_ignore_ascii_case("none") => {
Some(0.0)
}
_ => None,
Expand Down Expand Up @@ -391,13 +381,10 @@ impl Compressor {
let rgba: Vec<_> = value
.iter()
.filter(|n| {
!matches!(
n,
ComponentValue::Delimiter(box Delimiter {
value: DelimiterValue::Comma | DelimiterValue::Solidus,
..
})
)
!n.as_delimiter()
.map(|delimiter| delimiter.value)
.map(|v| matches!(v, DelimiterValue::Comma | DelimiterValue::Solidus))
.unwrap_or_default()
})
.collect();

Expand Down Expand Up @@ -429,13 +416,10 @@ impl Compressor {
let hsla: Vec<_> = value
.iter()
.filter(|n| {
!matches!(
n,
ComponentValue::Delimiter(box Delimiter {
value: DelimiterValue::Comma | DelimiterValue::Solidus,
..
})
)
!n.as_delimiter()
.map(|delimiter| delimiter.value)
.map(|v| matches!(v, DelimiterValue::Comma | DelimiterValue::Solidus))
.unwrap_or_default()
})
.collect();

Expand Down
45 changes: 22 additions & 23 deletions crates/swc_css_minifier/src/compressor/container.rs
Expand Up @@ -10,33 +10,32 @@ impl Compressor {
if is_calc_function_name(name) && value.len() == 1 =>
{
match &value[0] {
ComponentValue::CalcSum(box CalcSum {
expressions: calc_sum_expressions,
..
}) if calc_sum_expressions.len() == 1 => match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
match transform_calc_value_into_component_value(calc_value) {
Some(ComponentValue::Function(function)) => {
*n = SizeFeatureValue::Function(*function);
ComponentValue::CalcSum(calc_sum) if calc_sum.expressions.len() == 1 => {
match &calc_sum.expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
match transform_calc_value_into_component_value(calc_value) {
Some(ComponentValue::Function(function)) => {
*n = SizeFeatureValue::Function(*function);
}
Some(ComponentValue::Dimension(dimension)) => {
*n = SizeFeatureValue::Dimension(*dimension);
}
Some(ComponentValue::Number(number)) => {
*n = SizeFeatureValue::Number(*number);
}
_ => {}
}
Some(ComponentValue::Dimension(dimension)) => {
*n = SizeFeatureValue::Dimension(*dimension);
}
Some(ComponentValue::Number(number)) => {
*n = SizeFeatureValue::Number(*number);
}
_ => {}
}
}
_ => {}
}
_ => {}
},
}
_ => {}
}
}
Expand Down

0 comments on commit 305e72e

Please sign in to comment.