Skip to content

Commit 22ff516

Browse files
committed
add one more new cool ass error - and prefix with [FATAL] to see if i should make categories for warnings/fatals/etc
im unsure doe
1 parent c670cd7 commit 22ff516

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/diagnostics/suggestions.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,32 @@ impl ErrorDiagnostic for ErrorCode {
7979
}
8080
ErrorCode::ConstEnumsDisallowed => suggest_const_enums_disallowed(),
8181
ErrorCode::JsxModuleNotSet => suggest_jsx_not_set(err),
82+
ErrorCode::UnexpectedKeywordOrIdentifier => {
83+
suggest_unexpected_kw_or_identifier(err, tokens)
84+
}
8285
ErrorCode::Unsupported(_) => None,
8386
}
8487
}
8588
}
8689

90+
/// Suggestion for unexpected keyword or identifier
91+
fn suggest_unexpected_kw_or_identifier(err: &TsError, tokens: &[Token]) -> Option<Suggestion> {
92+
let keyword = find_token_at_position(tokens, err.line, err.column)?;
93+
94+
Some(Suggestion {
95+
suggestions: vec![format!(
96+
"{} `{}` is not expected in this context.",
97+
"[FATAL]".bright_red().bold().italic(),
98+
keyword.raw.bright_yellow().bold()
99+
)],
100+
help: Some(
101+
"Avoid using unknown, undeclared or invalid keywords or identifiers.".to_string(),
102+
),
103+
span: None,
104+
})
105+
}
106+
107+
/// Suggestion for when jsx compiler flag is not set but jsx is used
87108
fn suggest_jsx_not_set(err: &TsError) -> Option<Suggestion> {
88109
let module_name = extract_first_quoted(&err.message)?;
89110
let resolved_name = extract_second_quoted(&err.message)?;

src/error/codes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub enum ErrorCode {
2424
ExpressionExpected,
2525
DisallowedTrailingComma,
2626
SpreadParameterMustBeLast,
27+
UnexpectedKeywordOrIdentifier,
2728

2829
// Module/import errors (TS23xx, TS6xxx)
2930
NonExistentModuleImport,
@@ -65,6 +66,7 @@ pub enum ErrorCode {
6566
/// Catch-all for unsupported error codes
6667
Unsupported(u16),
6768
}
69+
6870
impl ErrorCode {
6971
/// Create an `ErrorCode` from a string represenatation like "TS2322"
7072
pub fn from_str(code: &str) -> Self {
@@ -115,6 +117,7 @@ impl ErrorCode {
115117
"TS7026" => ErrorCode::MissingJsxIntrinsicElementsDeclaration,
116118
"TS6244" => ErrorCode::ConstEnumsDisallowed,
117119
"TS6142" => ErrorCode::JsxModuleNotSet,
120+
"TS1434" => ErrorCode::UnexpectedKeywordOrIdentifier,
118121

119122
other => {
120123
if let Some(num_str) = other.strip_prefix("TS")
@@ -176,6 +179,7 @@ impl ErrorCode {
176179
ErrorCode::MissingJsxIntrinsicElementsDeclaration => "TS7026",
177180
ErrorCode::ConstEnumsDisallowed => "TS6244",
178181
ErrorCode::JsxModuleNotSet => "TS6142",
182+
ErrorCode::UnexpectedKeywordOrIdentifier => "TS1434",
179183
ErrorCode::Unsupported(_) => {
180184
// This will return a static string for known codes, but for unsupported codes,
181185
// we return a dynamically allocated string. To keep the return type consistent,

0 commit comments

Comments
 (0)