swc_css_lints/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::fmt::Debug;

use serde::{Deserialize, Serialize};

use crate::rules::{
    at_rule_no_unknown::AtRuleNoUnknownConfig, color_hex_alpha::ColorHexAlphaConfig,
    color_hex_length::ColorHexLengthConfig,
    font_family_no_duplicate_names::FontFamilyNoDuplicateNamesConfig,
    no_invalid_position_at_import_rule::NoInvalidPositionAtImportRuleConfig,
    selector_max_class::SelectorMaxClassConfig,
    selector_max_combinators::SelectorMaxCombinatorsConfig, unit_no_unknown::UnitNoUnknownConfig,
};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LintRuleReaction {
    Off,
    Warning,
    Error,
}

impl Default for LintRuleReaction {
    fn default() -> Self {
        Self::Off
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(untagged)]
enum LintRuleLevel {
    Str(LintRuleReaction),
    Number(u8),
}

impl Default for LintRuleLevel {
    fn default() -> Self {
        Self::Str(LintRuleReaction::Off)
    }
}

impl From<LintRuleLevel> for LintRuleReaction {
    fn from(level: LintRuleLevel) -> Self {
        match level {
            LintRuleLevel::Str(level) => level,
            LintRuleLevel::Number(level) => match level {
                1 => LintRuleReaction::Warning,
                2 => LintRuleReaction::Error,
                _ => LintRuleReaction::Off,
            },
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RuleConfig<T: Debug + Clone + Serialize + Default>(
    #[serde(default)] LintRuleLevel,
    #[serde(default)] T,
);

impl<T: Debug + Clone + Serialize + Default> RuleConfig<T> {
    #[inline]
    pub(crate) fn get_rule_reaction(&self) -> LintRuleReaction {
        self.0.into()
    }

    #[inline]
    pub(crate) fn get_rule_config(&self) -> &T {
        &self.1
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub struct RulesConfig {
    #[serde(default, alias = "blockNoEmpty")]
    pub block_no_empty: RuleConfig<()>,

    #[serde(default, alias = "atRuleNoUnknown")]
    pub at_rule_no_unknown: RuleConfig<AtRuleNoUnknownConfig>,

    #[serde(default, alias = "noEmptySource")]
    pub no_empty_source: RuleConfig<()>,

    #[serde(default, alias = "declarationNoImportant")]
    pub declaration_no_important: RuleConfig<()>,

    #[serde(default, alias = "keyframeDeclarationNoImportant")]
    pub keyframe_declaration_no_important: RuleConfig<()>,

    #[serde(default, alias = "noInvalidPositionAtImportRule")]
    pub no_invalid_position_at_import_rule: RuleConfig<NoInvalidPositionAtImportRuleConfig>,

    #[serde(default, alias = "selectorMaxClass")]
    pub selector_max_class: RuleConfig<SelectorMaxClassConfig>,

    #[serde(default, alias = "colorHexLength")]
    pub color_hex_length: RuleConfig<ColorHexLengthConfig>,

    #[serde(default, alias = "colorNoInvalidHex")]
    pub color_no_invalid_hex: RuleConfig<()>,

    #[serde(default, alias = "unitNoUnknown")]
    pub unit_no_unknown: RuleConfig<UnitNoUnknownConfig>,

    #[serde(default, alias = "selectorMaxCombinators")]
    pub selector_max_combinators: RuleConfig<SelectorMaxCombinatorsConfig>,

    #[serde(default, alias = "fontFamilyNoDuplicateNames")]
    pub font_family_no_duplicate_names: RuleConfig<FontFamilyNoDuplicateNamesConfig>,

    #[serde(default, alias = "colorHexAlpha")]
    pub color_hex_alpha: RuleConfig<ColorHexAlphaConfig>,

    #[serde(default, alias = "noDuplicateAtImportRules")]
    pub no_duplicate_at_import_rules: RuleConfig<()>,

    #[serde(default, alias = "customPropertyNoMissingVarFunction")]
    pub custom_property_no_missing_var_function: RuleConfig<()>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LintConfig {
    #[serde(default)]
    pub rules: RulesConfig,
}