aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lib.rs
blob: cc4818af517c7f056d81f63650523ede5ef6457e (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#![recursion_limit = "1024"]
mod lints;
mod make;
pub mod session;
mod utils;

pub use lints::LINTS;
use session::SessionInfo;

use rnix::{parser::ParseError, SyntaxElement, SyntaxKind, TextRange};
use std::{convert::Into, default::Default};

#[cfg(feature = "json-out")]
use serde::{
    ser::{SerializeStruct, Serializer},
    Serialize,
};

#[derive(Debug)]
#[cfg_attr(feature = "json-out", derive(Serialize))]
pub enum Severity {
    Warn,
    Error,
    Hint,
}

impl Default for Severity {
    fn default() -> Self {
        Self::Warn
    }
}

/// Report generated by a lint
#[derive(Debug, Default)]
#[cfg_attr(feature = "json-out", derive(Serialize))]
pub struct Report {
    /// General information about this lint and where it applies.
    pub note: &'static str,
    /// An error code to uniquely identify this lint
    pub code: u32,
    /// Report severity level
    pub severity: Severity,
    /// Collection of diagnostics raised by this lint
    pub diagnostics: Vec<Diagnostic>,
}

impl Report {
    /// Construct a report. Do not invoke `Report::new` manually, see `lint` macro
    pub fn new(note: &'static str, code: u32) -> Self {
        Self {
            note,
            code,
            ..Default::default()
        }
    }
    /// Add a diagnostic to this report
    pub fn diagnostic<S: AsRef<str>>(mut self, at: TextRange, message: S) -> Self {
        self.diagnostics.push(Diagnostic::new(at, message));
        self
    }
    /// Add a diagnostic with a fix to this report
    pub fn suggest<S: AsRef<str>>(
        mut self,
        at: TextRange,
        message: S,
        suggestion: Suggestion,
    ) -> Self {
        self.diagnostics
            .push(Diagnostic::suggest(at, message, suggestion));
        self
    }
    /// Set severity level
    pub fn severity(mut self, severity: Severity) -> Self {
        self.severity = severity;
        self
    }
    /// A range that encompasses all the suggestions provided in this report
    pub fn total_suggestion_range(&self) -> Option<TextRange> {
        self.diagnostics
            .iter()
            .flat_map(|d| Some(d.suggestion.as_ref()?.at))
            .reduce(|acc, next| acc.cover(next))
    }
    /// A range that encompasses all the diagnostics provided in this report
    pub fn total_diagnostic_range(&self) -> Option<TextRange> {
        self.diagnostics
            .iter()
            .flat_map(|d| Some(d.at))
            .reduce(|acc, next| acc.cover(next))
    }
    /// Unsafe but handy replacement for above
    pub fn range(&self) -> TextRange {
        self.total_suggestion_range().unwrap()
    }
    /// Apply all diagnostics. Assumption: diagnostics do not overlap
    pub fn apply(&self, src: &mut String) {
        for d in self.diagnostics.iter() {
            d.apply(src);
        }
    }
    /// Create a report out of a parse error
    pub fn from_parse_err(err: ParseError) -> Self {
        let at = match err {
            ParseError::Unexpected(at) => at,
            ParseError::UnexpectedExtra(at) => at,
            ParseError::UnexpectedWanted(_, at, _) => at,
            ParseError::UnexpectedDoubleBind(at) => at,
            ParseError::UnexpectedEOF | ParseError::UnexpectedEOFWanted(_) => {
                TextRange::empty(0u32.into())
            }
            _ => panic!("report a bug, pepper forgot to handle a parse error"),
        };
        let mut message = err.to_string();
        message
            .as_mut_str()
            .get_mut(0..1)
            .unwrap()
            .make_ascii_uppercase();
        Self::new("Syntax error", 0)
            .diagnostic(at, message)
            .severity(Severity::Error)
    }
}

/// Mapping from a bytespan to an error message.
/// Can optionally suggest a fix.
#[derive(Debug)]
pub struct Diagnostic {
    pub at: TextRange,
    pub message: String,
    pub suggestion: Option<Suggestion>,
}

impl Diagnostic {
    /// Construct a diagnostic.
    pub fn new<S: AsRef<str>>(at: TextRange, message: S) -> Self {
        Self {
            at,
            message: message.as_ref().into(),
            suggestion: None,
        }
    }
    /// Construct a diagnostic with a fix.
    pub fn suggest<S: AsRef<str>>(at: TextRange, message: S, suggestion: Suggestion) -> Self {
        Self {
            at,
            message: message.as_ref().into(),
            suggestion: Some(suggestion),
        }
    }
    /// Apply a diagnostic to a source file
    pub fn apply(&self, src: &mut String) {
        if let Some(s) = &self.suggestion {
            s.apply(src);
        }
    }
}

#[cfg(feature = "json-out")]
impl Serialize for Diagnostic {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut s = serializer.serialize_struct("Diagnostic", 3)?;
        let at = {
            let start = usize::from(self.at.start());
            let end = usize::from(self.at.end());
            (start, end)
        };
        s.serialize_field("at", &at)?;
        s.serialize_field("message", &self.message)?;
        if let Some(suggestion) = &self.suggestion {
            s.serialize_field("suggestion", suggestion)?;
        }
        s.end()
    }
}

/// Suggested fix for a diagnostic, the fix is provided as a syntax element.
/// Look at `make.rs` to construct fixes.
#[derive(Debug)]
pub struct Suggestion {
    pub at: TextRange,
    pub fix: SyntaxElement,
}

impl Suggestion {
    /// Construct a suggestion.
    pub fn new<E: Into<SyntaxElement>>(at: TextRange, fix: E) -> Self {
        Self {
            at,
            fix: fix.into(),
        }
    }
    /// Apply a suggestion to a source file
    pub fn apply(&self, src: &mut String) {
        let start = usize::from(self.at.start());
        let end = usize::from(self.at.end());
        src.replace_range(start..end, &self.fix.to_string())
    }
}

#[cfg(feature = "json-out")]
impl Serialize for Suggestion {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut s = serializer.serialize_struct("Suggestion", 2)?;
        let at = {
            let start = usize::from(self.at.start());
            let end = usize::from(self.at.end());
            (start, end)
        };
        let fix = self.fix.to_string();
        s.serialize_field("at", &at)?;
        s.serialize_field("fix", &fix)?;
        s.end()
    }
}

/// Lint logic is defined via this trait. Do not implement manually,
/// look at the `lint` attribute macro instead for implementing rules
pub trait Rule {
    fn validate(&self, node: &SyntaxElement, sess: &SessionInfo) -> Option<Report>;
}

/// Contains information about the lint itself. Do not implement manually,
/// look at the `lint` attribute macro instead for implementing rules
pub trait Metadata {
    fn name(&self) -> &'static str;
    fn note(&self) -> &'static str;
    fn code(&self) -> u32;
    fn report(&self) -> Report;
    fn match_with(&self, with: &SyntaxKind) -> bool;
    fn match_kind(&self) -> Vec<SyntaxKind>;
}

/// Contains offline explanation for each lint
/// The `lint` macro scans nearby doc comments for
/// explanations and derives this trait.
///
/// FIXME: the lint macro does way too much, maybe
/// split it into smaller macros.
pub trait Explain {
    fn explanation(&self) -> &'static str {
        "no explanation found"
    }
}

/// Combines Rule and Metadata, do not implement manually, this is derived by
/// the `lint` macro.
pub trait Lint: Metadata + Explain + Rule + Send + Sync {}

/// Helper utility to take lints from modules and insert them into a map for efficient
/// access. Mapping is from a SyntaxKind to a list of lints that apply on that Kind.
///
/// See `lints.rs` for usage.
#[macro_export]
macro_rules! lints {
    ($($s:ident),*,) => {
        lints!($($s),*);
    };
    ($($s:ident),*) => {
        $(
            mod $s;
        )*
        ::lazy_static::lazy_static! {
            pub static ref LINTS: Vec<&'static Box<dyn $crate::Lint>> = {
                let mut v = Vec::new();
                $(
                    {
                        let temp_lint = &*$s::LINT;
                        v.push(temp_lint);
                    }
                )*
                v
            };
        }
    }
}