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
use std::borrow::Cow;
use swc_atoms::JsWord;
use swc_common::{
errors::{DiagnosticBuilder, Handler},
Span,
};
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
inner: Box<(Span, ErrorKind)>,
}
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.inner.1
}
pub fn into_inner(self) -> Box<(Span, ErrorKind)> {
self.inner
}
pub fn new(span: Span, kind: ErrorKind) -> Self {
Error {
inner: Box::new((span, kind)),
}
}
pub fn to_diagnostics<'a>(&self, handler: &'a Handler) -> DiagnosticBuilder<'a> {
let msg: Cow<_> = match &self.inner.1 {
ErrorKind::Eof => "Unexpected end of file".into(),
ErrorKind::Ignore => "Not an error".into(),
ErrorKind::UnexpectedChar(c) => format!("Unexpected character `{:?}`", c).into(),
ErrorKind::UnterminatedUrl => "Unterminated url literal".into(),
ErrorKind::InvalidEscape => "Invalid escape".into(),
ErrorKind::Expected(s) => format!("Expected {}", s).into(),
ErrorKind::ExpectedButGot(s) => format!("Expected {}", s).into(),
ErrorKind::ExpectedSelectorText => "Expected a text for selector".into(),
ErrorKind::UnterminatedBlockComment => "Unterminated block comment".into(),
ErrorKind::InvalidCharsetAtRule => "Invalid @charset at-rule".into(),
ErrorKind::InvalidTypeSelector => "Invalid type selector".into(),
ErrorKind::InvalidSelector => "Invalid selector".into(),
ErrorKind::InvalidAttrSelectorName => "Invalid attribute name".into(),
ErrorKind::InvalidAttrSelectorMatcher => "Invalid attribute matcher".into(),
ErrorKind::InvalidAttrSelectorMatcherValue => "Invalid attribute matcher value".into(),
ErrorKind::InvalidAttrSelectorModifier => "Invalid attribute modifier".into(),
ErrorKind::ExpectedNumber => "Expected a number".into(),
ErrorKind::InvalidSupportQuery => "Invalid support query".into(),
ErrorKind::InvalidMediaQuery => "Invalid media query".into(),
ErrorKind::InvalidLayerBlockAtRule => "Invalid @layer block at-rule".into(),
ErrorKind::UnknownAtRuleNotTerminated => "Unknown @rule is not terminated".into(),
ErrorKind::InvalidDeclarationValue => "Expected a property value".into(),
ErrorKind::InvalidAnPlusBMicrosyntax => "Invalid An+B microsyntax".into(),
ErrorKind::InvalidCustomIdent(s) => format!(
"The CSS-wide keywords are not valid custom ident, found '{}'",
s
)
.into(),
ErrorKind::InvalidKeyframesName(s) => {
format!("{} is not valid name for keyframes", s).into()
}
};
handler.struct_span_err(self.inner.0, &msg)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
Eof,
Ignore,
UnexpectedChar(char),
UnterminatedUrl,
InvalidEscape,
Expected(&'static str),
ExpectedButGot(&'static str),
ExpectedSelectorText,
UnterminatedBlockComment,
InvalidCharsetAtRule,
InvalidTypeSelector,
InvalidSelector,
InvalidDeclarationValue,
InvalidAttrSelectorName,
InvalidAttrSelectorMatcher,
InvalidAttrSelectorMatcherValue,
InvalidAttrSelectorModifier,
ExpectedNumber,
InvalidSupportQuery,
InvalidLayerBlockAtRule,
InvalidMediaQuery,
InvalidAnPlusBMicrosyntax,
InvalidCustomIdent(JsWord),
InvalidKeyframesName(&'static str),
UnknownAtRuleNotTerminated,
}