swc_css_lints/rules/
color_no_invalid_hex.rs1use swc_css_ast::*;
2use swc_css_visit::{Visit, VisitWith};
3
4use crate::rule::{visitor_rule, LintRule, LintRuleContext};
5
6pub fn color_no_invalid_hex(ctx: LintRuleContext<()>) -> Box<dyn LintRule> {
7 visitor_rule(ctx.reaction(), ColorNoInvalidHex { ctx })
8}
9
10#[derive(Debug, Default)]
11struct ColorNoInvalidHex {
12 ctx: LintRuleContext<()>,
13}
14
15impl Visit for ColorNoInvalidHex {
16 fn visit_hex_color(&mut self, hex_color: &HexColor) {
17 let HexColor { value, .. } = hex_color;
18 let length = value.len();
19 if (length == 3 || length == 4 || length == 6 || length == 8)
20 && value.chars().all(|c| c.is_ascii_hexdigit())
21 {
22 hex_color.visit_children_with(self);
23 return;
24 }
25
26 let message = format!("Unexpected invalid hex color '#{value}'.");
27
28 self.ctx.report(hex_color, message);
29
30 hex_color.visit_children_with(self);
31 }
32}