swc_css_lints/rules/
keyframe_declaration_no_important.rs1use swc_css_ast::*;
2use swc_css_visit::{Visit, VisitWith};
3
4use crate::rule::{visitor_rule, LintRule, LintRuleContext};
5
6pub fn keyframe_declaration_no_important(ctx: LintRuleContext<()>) -> Box<dyn LintRule> {
7 visitor_rule(
8 ctx.reaction(),
9 KeyframeDeclarationNoImportant {
10 ctx,
11 in_keyframes_at_rule: false,
12 },
13 )
14}
15
16const MESSAGE: &str = "Unexpected '!important'.";
17
18#[derive(Debug, Default)]
19struct KeyframeDeclarationNoImportant {
20 ctx: LintRuleContext<()>,
21 in_keyframes_at_rule: bool,
22}
23
24impl Visit for KeyframeDeclarationNoImportant {
25 fn visit_at_rule(&mut self, at_rule: &AtRule) {
26 if let Some(AtRulePrelude::KeyframesPrelude(_)) = at_rule.prelude.as_deref() {
27 let old_value_in_keyframes_at_rule = self.in_keyframes_at_rule;
28
29 self.in_keyframes_at_rule = true;
30
31 at_rule.visit_children_with(self);
32
33 self.in_keyframes_at_rule = old_value_in_keyframes_at_rule;
34 }
35 }
36
37 fn visit_important_flag(&mut self, important_flag: &ImportantFlag) {
38 important_flag.visit_children_with(self);
39
40 if !self.in_keyframes_at_rule {
41 return;
42 }
43
44 self.ctx.report(important_flag, MESSAGE);
45 }
46}