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
use std::borrow::Cow;
use std::fmt;
use crate::swc::common::Span;
use crate::swc::common::Spanned;
use crate::swc::parser::error::SyntaxError;
use crate::SourceTextInfo;
pub type LineAndColumnIndex = text_lines::LineAndColumnIndex;
pub type LineAndColumnDisplay = text_lines::LineAndColumnDisplay;
#[derive(Debug, Clone, PartialEq)]
pub struct Diagnostic {
pub specifier: String,
pub span: Span,
pub display_position: LineAndColumnDisplay,
pub kind: SyntaxError,
}
impl Diagnostic {
pub fn message(&self) -> Cow<str> {
self.kind.msg()
}
}
impl Diagnostic {
pub(crate) fn from_swc_error(
err: crate::swc::parser::error::Error,
specifier: &str,
source: &SourceTextInfo,
) -> Diagnostic {
Diagnostic {
span: err.span(),
display_position: source.line_and_column_display(err.span().lo),
specifier: specifier.to_string(),
kind: err.into_kind(),
}
}
}
impl std::error::Error for Diagnostic {}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} at {}:{}:{}",
self.message(),
self.specifier,
self.display_position.line_number,
self.display_position.column_number
)
}
}
#[derive(Debug)]
pub struct DiagnosticsError(pub Vec<Diagnostic>);
impl std::error::Error for DiagnosticsError {}
impl fmt::Display for DiagnosticsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, diagnostic) in self.0.iter().enumerate() {
if i > 0 {
write!(f, "\n\n")?;
}
write!(f, "{}", diagnostic)?
}
Ok(())
}
}