swc_css_lints/rules/
keyframe_declaration_no_important.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
use swc_css_ast::*;
use swc_css_visit::{Visit, VisitWith};

use crate::rule::{visitor_rule, LintRule, LintRuleContext};

pub fn keyframe_declaration_no_important(ctx: LintRuleContext<()>) -> Box<dyn LintRule> {
    visitor_rule(
        ctx.reaction(),
        KeyframeDeclarationNoImportant {
            ctx,
            in_keyframes_at_rule: false,
        },
    )
}

const MESSAGE: &str = "Unexpected '!important'.";

#[derive(Debug, Default)]
struct KeyframeDeclarationNoImportant {
    ctx: LintRuleContext<()>,
    in_keyframes_at_rule: bool,
}

impl Visit for KeyframeDeclarationNoImportant {
    fn visit_at_rule(&mut self, at_rule: &AtRule) {
        if let Some(AtRulePrelude::KeyframesPrelude(_)) = at_rule.prelude.as_deref() {
            let old_value_in_keyframes_at_rule = self.in_keyframes_at_rule;

            self.in_keyframes_at_rule = true;

            at_rule.visit_children_with(self);

            self.in_keyframes_at_rule = old_value_in_keyframes_at_rule;
        }
    }

    fn visit_important_flag(&mut self, important_flag: &ImportantFlag) {
        important_flag.visit_children_with(self);

        if !self.in_keyframes_at_rule {
            return;
        }

        self.ctx.report(important_flag, MESSAGE);
    }
}