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
/// Collection of lint diagnostics for a file or project.
///
/// `LintResult` aggregates all linting findings and provides utilities
/// for querying by severity, counting issues, and merging results.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// let span = Span::new(1, 5, 1, 10);
///
/// result.add(Diagnostic::new("SC2086", Severity::Warning, "Quote variable", span));
/// result.add(Diagnostic::new("SC2046", Severity::Error, "Quote command", span));
///
/// assert_eq!(result.diagnostics.len(), 2);
/// assert!(result.has_errors());
/// assert!(result.has_warnings());
/// ```
///
/// ## Merging results
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result1 = LintResult::new();
/// let mut result2 = LintResult::new();
/// let span = Span::new(1, 1, 1, 5);
///
/// result1.add(Diagnostic::new("SC2086", Severity::Warning, "Test 1", span));
/// result2.add(Diagnostic::new("SC2046", Severity::Warning, "Test 2", span));
///
/// result1.merge(result2);
/// assert_eq!(result1.diagnostics.len(), 2);
/// ```
///
/// ## Severity analysis
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// let span = Span::new(1, 1, 1, 5);
///
/// result.add(Diagnostic::new("W1", Severity::Warning, "Warning", span));
/// result.add(Diagnostic::new("E1", Severity::Error, "Error", span));
///
/// assert_eq!(result.count_by_severity(Severity::Warning), 1);
/// assert_eq!(result.count_by_severity(Severity::Error), 1);
/// assert_eq!(result.max_severity(), Some(Severity::Error));
/// ```
#[derive(Debug, Clone, Default)]
pub struct LintResult {
/// All diagnostics found during linting.
pub diagnostics: Vec<Diagnostic>,
}
impl LintResult {
/// Creates an empty lint result.
///
/// # Examples
///
/// ```
/// use bashrs::linter::LintResult;
///
/// let result = LintResult::new();
/// assert_eq!(result.diagnostics.len(), 0);
/// ```
pub fn new() -> Self {
Self {
diagnostics: Vec::new(),
}
}
/// Adds a diagnostic to this result.
///
/// # Arguments
///
/// * `diagnostic` - The diagnostic to add
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// let span = Span::new(1, 1, 1, 5);
/// let diag = Diagnostic::new("SC2086", Severity::Warning, "Test", span);
///
/// result.add(diag);
/// assert_eq!(result.diagnostics.len(), 1);
/// ```
pub fn add(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}
/// Merges another result into this one.
///
/// All diagnostics from `other` are appended to this result.
///
/// # Arguments
///
/// * `other` - The result to merge
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result1 = LintResult::new();
/// let mut result2 = LintResult::new();
/// let span = Span::new(1, 1, 1, 5);
///
/// result1.add(Diagnostic::new("SC2086", Severity::Warning, "Test 1", span));
/// result2.add(Diagnostic::new("SC2046", Severity::Warning, "Test 2", span));
///
/// result1.merge(result2);
/// assert_eq!(result1.diagnostics.len(), 2);
/// ```
pub fn merge(&mut self, other: LintResult) {
self.diagnostics.extend(other.diagnostics);
}
/// Checks if there are any errors (Severity::Error).
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// assert!(!result.has_errors());
///
/// let span = Span::new(1, 1, 1, 5);
/// result.add(Diagnostic::new("SC2086", Severity::Warning, "Test", span));
/// assert!(!result.has_errors());
///
/// result.add(Diagnostic::new("SC2046", Severity::Error, "Test", span));
/// assert!(result.has_errors());
/// ```
pub fn has_errors(&self) -> bool {
self.diagnostics
.iter()
.any(|d| d.severity == Severity::Error)
}
/// Checks if there are any warnings (Severity::Warning).
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// assert!(!result.has_warnings());
///
/// let span = Span::new(1, 1, 1, 5);
/// result.add(Diagnostic::new("SC2086", Severity::Info, "Test", span));
/// assert!(!result.has_warnings());
///
/// result.add(Diagnostic::new("SC2046", Severity::Warning, "Test", span));
/// assert!(result.has_warnings());
/// ```
pub fn has_warnings(&self) -> bool {
self.diagnostics
.iter()
.any(|d| d.severity == Severity::Warning)
}
/// Counts diagnostics by severity level.
///
/// # Arguments
///
/// * `severity` - The severity level to count
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// let span = Span::new(1, 1, 1, 5);
///
/// result.add(Diagnostic::new("W1", Severity::Warning, "Test", span));
/// result.add(Diagnostic::new("W2", Severity::Warning, "Test", span));
/// result.add(Diagnostic::new("E1", Severity::Error, "Test", span));
///
/// assert_eq!(result.count_by_severity(Severity::Warning), 2);
/// assert_eq!(result.count_by_severity(Severity::Error), 1);
/// assert_eq!(result.count_by_severity(Severity::Info), 0);
/// ```
pub fn count_by_severity(&self, severity: Severity) -> usize {
self.diagnostics
.iter()
.filter(|d| d.severity == severity)
.count()
}
/// Gets the highest severity level present.
///
/// Returns `None` if there are no diagnostics.
///
/// # Examples
///
/// ```
/// use bashrs::linter::{Diagnostic, LintResult, Severity, Span};
///
/// let mut result = LintResult::new();
/// assert_eq!(result.max_severity(), None);
///
/// let span = Span::new(1, 1, 1, 5);
/// result.add(Diagnostic::new("SC2086", Severity::Warning, "Test", span));
/// assert_eq!(result.max_severity(), Some(Severity::Warning));
///
/// result.add(Diagnostic::new("SC2046", Severity::Error, "Test", span));
/// assert_eq!(result.max_severity(), Some(Severity::Error));
/// ```
pub fn max_severity(&self) -> Option<Severity> {
self.diagnostics.iter().map(|d| d.severity).max()
}
}
#[cfg(test)]
#[path = "diagnostic_tests_span_creatio.rs"]
mod tests_extracted;