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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{fmt::Debug, sync::Arc};

use auto_impl::auto_impl;
use parking_lot::Mutex;
use rayon::prelude::*;
use serde::Serialize;
use swc_common::{
    errors::{Diagnostic, DiagnosticBuilder, Emitter, Handler, HANDLER},
    Spanned,
};
use swc_css_ast::Stylesheet;
use swc_css_visit::{Visit, VisitWith};

use super::config::{LintRuleReaction, RuleConfig};

/// A lint rule.
///
/// # Implementation notes
///
/// Must report error to [swc_common::HANDLER]
#[auto_impl(Box, &mut)]
pub trait LintRule: Debug + Send + Sync {
    fn lint_stylesheet(&mut self, stylesheet: &Stylesheet);
}

/// This preserves the order of errors.
impl<R> LintRule for Vec<R>
where
    R: LintRule,
{
    fn lint_stylesheet(&mut self, stylesheet: &Stylesheet) {
        if cfg!(target_arch = "wasm32") {
            for rule in self {
                rule.lint_stylesheet(stylesheet);
            }
        } else {
            let errors = self
                .par_iter_mut()
                .flat_map(|rule| {
                    let emitter = Capturing::default();
                    {
                        let handler = Handler::with_emitter(true, false, Box::new(emitter.clone()));
                        HANDLER.set(&handler, || {
                            rule.lint_stylesheet(stylesheet);
                        });
                    }

                    Arc::try_unwrap(emitter.errors).unwrap().into_inner()
                })
                .collect::<Vec<_>>();

            HANDLER.with(|handler| {
                for error in errors {
                    DiagnosticBuilder::new_diagnostic(handler, error).emit();
                }
            });
        }
    }
}

#[derive(Default, Clone)]
struct Capturing {
    errors: Arc<Mutex<Vec<Diagnostic>>>,
}

impl Emitter for Capturing {
    fn emit(&mut self, db: &DiagnosticBuilder<'_>) {
        self.errors.lock().push((**db).clone());
    }
}

pub(crate) fn visitor_rule<V>(reaction: LintRuleReaction, v: V) -> Box<dyn LintRule>
where
    V: 'static + Send + Sync + Visit + Default + Debug,
{
    Box::new(VisitorRule(v, reaction))
}

#[derive(Debug)]
struct VisitorRule<V>(V, LintRuleReaction)
where
    V: Send + Sync + Visit;

impl<V> LintRule for VisitorRule<V>
where
    V: Send + Sync + Visit + Debug,
{
    fn lint_stylesheet(&mut self, stylesheet: &Stylesheet) {
        if !matches!(self.1, LintRuleReaction::Off) {
            stylesheet.visit_with(&mut self.0);
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct LintRuleContext<C>
where
    C: Debug + Clone + Serialize + Default,
{
    reaction: LintRuleReaction,
    config: C,
}

impl<C> LintRuleContext<C>
where
    C: Debug + Clone + Serialize + Default,
{
    pub(crate) fn report<N, S>(&self, ast_node: N, message: S)
    where
        N: Spanned,
        S: AsRef<str>,
    {
        HANDLER.with(|handler| match self.reaction {
            LintRuleReaction::Error => handler
                .struct_span_err(ast_node.span(), message.as_ref())
                .emit(),
            LintRuleReaction::Warning => handler
                .struct_span_warn(ast_node.span(), message.as_ref())
                .emit(),
            _ => {}
        });
    }

    #[inline]
    pub(crate) fn config(&self) -> &C {
        &self.config
    }

    #[inline]
    pub(crate) fn reaction(&self) -> LintRuleReaction {
        self.reaction
    }
}

impl<C> From<&RuleConfig<C>> for LintRuleContext<C>
where
    C: Debug + Clone + Serialize + Default,
{
    fn from(config: &RuleConfig<C>) -> Self {
        Self {
            reaction: config.get_rule_reaction(),
            config: config.get_rule_config().clone(),
        }
    }
}