swc_css_lints/rules/
unit_no_unknown.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use serde::{Deserialize, Serialize};
use swc_css_ast::*;
use swc_css_visit::{Visit, VisitWith};

use crate::{
    pattern::NamePattern,
    rule::{visitor_rule, LintRule, LintRuleContext},
    ConfigError,
};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UnitNoUnknownConfig {
    ignore_units: Option<Vec<String>>,
}

pub fn unit_no_unknown(
    ctx: LintRuleContext<UnitNoUnknownConfig>,
) -> Result<Box<dyn LintRule>, ConfigError> {
    let ignored_units = ctx
        .config()
        .ignore_units
        .clone()
        .unwrap_or_default()
        .into_iter()
        .map(NamePattern::try_from)
        .collect::<Result<_, _>>()?;
    Ok(visitor_rule(
        ctx.reaction(),
        UnitNoUnknown { ctx, ignored_units },
    ))
}

#[derive(Debug, Default)]
struct UnitNoUnknown {
    ctx: LintRuleContext<UnitNoUnknownConfig>,
    ignored_units: Vec<NamePattern>,
}

impl Visit for UnitNoUnknown {
    fn visit_unknown_dimension(&mut self, unknown_dimension: &UnknownDimension) {
        let unit = &unknown_dimension.unit.value;

        if self.ignored_units.iter().all(|item| !item.is_match(unit)) {
            let message = format!("Unexpected unknown unit \"{}\".", unit);
            self.ctx.report(&unknown_dimension.unit, message);
        }

        unknown_dimension.visit_children_with(self);
    }

    fn visit_component_value(&mut self, component_value: &ComponentValue) {
        if let Some(token_and_span) = component_value.as_preserved_token() {
            if let Some(unit) = match &token_and_span.token {
                Token::Dimension(dimension_token) => Some(dimension_token.unit.as_ref()),
                _ => None,
            } {
                if self.ignored_units.iter().all(|item| !item.is_match(unit)) {
                    let message = format!("Unexpected unknown unit \"{}\".", unit);
                    self.ctx.report(token_and_span, message);
                }
            }
        }

        component_value.visit_children_with(self);
    }
}