swc_css_minifier/compressor/
container.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use swc_css_ast::*;

use super::Compressor;
use crate::compressor::math::{is_calc_function_name, transform_calc_value_into_component_value};

impl Compressor {
    pub(super) fn compress_calc_sum_in_size_feature_value(&mut self, n: &mut SizeFeatureValue) {
        match n {
            SizeFeatureValue::Function(Function { name, value, .. })
                if is_calc_function_name(name) && value.len() == 1 =>
            {
                match &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]
                                {
                                    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);
                                        }
                                        _ => {}
                                    }
                                }
                            }
                            _ => {}
                        }
                    }
                    _ => {}
                }
            }
            _ => {}
        }
    }

    pub(super) fn compress_size_feature_value_length(&mut self, n: &mut SizeFeatureValue) {
        if let SizeFeatureValue::Dimension(Dimension::Length(length)) = n {
            if let Some(number) = self.length_to_zero(length) {
                *n = SizeFeatureValue::Number(number)
            }
        }
    }
}