Skip to main content

mlua_check/
types.rs

1use serde::Serialize;
2use std::fmt;
3
4/// Unique identifier for each lint rule.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
6#[serde(rename_all = "snake_case")]
7pub enum RuleId {
8    /// Reference to a variable not defined in any enclosing scope.
9    UndefinedVariable,
10    /// Reference to a global name not in the known symbol table.
11    UndefinedGlobal,
12    /// Access to a field not declared in a `---@class` definition.
13    UndefinedField,
14    /// A local variable that is declared but never referenced.
15    UnusedVariable,
16}
17
18impl fmt::Display for RuleId {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::UndefinedVariable => write!(f, "undefined-variable"),
22            Self::UndefinedGlobal => write!(f, "undefined-global"),
23            Self::UndefinedField => write!(f, "undefined-field"),
24            Self::UnusedVariable => write!(f, "unused-variable"),
25        }
26    }
27}
28
29/// Severity level for a diagnostic.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
31#[serde(rename_all = "lowercase")]
32pub enum Severity {
33    Error,
34    Warning,
35    Info,
36}
37
38/// A single lint diagnostic.
39#[derive(Debug, Clone, Serialize)]
40pub struct Diagnostic {
41    pub rule: RuleId,
42    pub severity: Severity,
43    pub message: String,
44    pub line: usize,
45    pub column: usize,
46}
47
48/// Aggregated lint result.
49#[derive(Debug, Clone, Serialize)]
50pub struct LintResult {
51    pub diagnostics: Vec<Diagnostic>,
52    pub error_count: usize,
53    pub warning_count: usize,
54}
55
56impl LintResult {
57    pub fn new(diagnostics: Vec<Diagnostic>) -> Self {
58        let error_count = diagnostics
59            .iter()
60            .filter(|d| d.severity == Severity::Error)
61            .count();
62        let warning_count = diagnostics
63            .iter()
64            .filter(|d| d.severity == Severity::Warning)
65            .count();
66        Self {
67            diagnostics,
68            error_count,
69            warning_count,
70        }
71    }
72
73    /// Returns `true` if any diagnostic has error severity.
74    pub fn has_errors(&self) -> bool {
75        self.error_count > 0
76    }
77}