Skip to content

Commit b0c6802

Browse files
committed
add a few more errors
TypeAssertionInJs MappedTypeMustBeStatic -- this is incorrectly named but it conveys my idea of it best ElementImplicitAnyInvalidIndexTypeForObject -- long ass name but its for invalid indexing on an object like the other but this gives more context JsxFlagNotProvided MissingJsxIntrinsicElementsDeclaration - speaks for itself UnreachableCode - was in prev commit but might as well mention here
1 parent 3512eef commit b0c6802

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
type Obj = {
2+
a: number;
3+
b: number;
4+
};
5+
6+
declare const key: string;
7+
8+
const obj: Obj = { a: 1, b: 2 };
9+
10+
11+
obj[key];
12+
13+
14+
115
import { anotherExported, exported, notExported } from "./export-smth";
216

317
console.log(exported);

src/diagnostics/suggestions.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,92 @@ impl ErrorDiagnostic for ErrorCode {
6969
ErrorCode::ImportedButNeverUsed => suggest_imported_unused(),
7070
ErrorCode::InvalidDefaultImport => suggest_invalid_default_import(),
7171
ErrorCode::UnreachableCode => unreachable_suggestion(),
72+
ErrorCode::TypeAssertionInJsNotAllowed => type_assertion_in_js_not_allowed_suggestion(),
73+
ErrorCode::MappedTypeMustBeStatic => mapped_type_must_be_static_suggestion(),
74+
ErrorCode::ElementImplicitAnyInvalidIndexTypeForObject => {
75+
suggest_element_implicit_any_invalid_index_type_for_object(err)
76+
}
77+
ErrorCode::MissingJsxIntrinsicElementsDeclaration => {
78+
suggest_missing_jsx_intrinsic_elements_declaration()
79+
}
7280
ErrorCode::Unsupported(_) => None,
7381
}
7482
}
7583
}
7684

85+
/// Suggestion for missing JSX intrinsic elements declaration
86+
/// explained here https://www.totaltypescript.com/what-is-jsx-intrinsicelements
87+
fn suggest_missing_jsx_intrinsic_elements_declaration() -> Option<Suggestion> {
88+
Some(Suggestion {
89+
suggestions: vec![
90+
"JSX intrinsic elements declaration is missing in global scope.".to_string(),
91+
],
92+
help: Some(
93+
"Either declare a global module with a JSX namespace or configure React or other JSX consumers correctly"
94+
.to_string(),
95+
),
96+
span: None,
97+
})
98+
}
99+
100+
/// Suggestion for when element is implicitly any and that index type is invalid for indexing
101+
/// object
102+
fn suggest_element_implicit_any_invalid_index_type_for_object(err: &TsError) -> Option<Suggestion> {
103+
let implicit_type = extract_quoted_value(&err.message, 1).unwrap_or_else(|| "any".to_string());
104+
105+
let index_type = extract_quoted_value(&err.message, 3).unwrap_or_else(|| "type".to_string());
106+
let object_to_index =
107+
extract_quoted_value(&err.message, 6).unwrap_or_else(|| "object".to_string());
108+
109+
Some(Suggestion {
110+
suggestions: vec![format!(
111+
"`{}` can not be used as an index to access `{}` - therefore element is implicitly `{}`.",
112+
index_type.red().bold(),
113+
object_to_index.red().bold(),
114+
implicit_type.red().bold()
115+
)],
116+
help: Some(format!(
117+
"Consider declaring the index with `{}` or loosen the type of `{}` to allow indexing with `{}`.",
118+
format!(
119+
"{} {}",
120+
"keyof typeof".yellow().bold(),
121+
object_to_index.yellow().bold()
122+
),
123+
object_to_index.yellow().bold(),
124+
index_type.yellow().bold()
125+
)),
126+
span: None,
127+
})
128+
}
129+
130+
/// Suggestion for mapped types with non-static keys
131+
fn suggest_mapped_type_must_be_static() -> Option<Suggestion> {
132+
Some(Suggestion {
133+
suggestions: vec![
134+
"Consider removing the properties and/or methods".to_string(),
135+
],
136+
help: Some(
137+
"Split multiple mapped property declarations into individual types and combine them using a type intersection."
138+
.to_string(),
139+
),
140+
span: None,
141+
})
142+
}
143+
144+
/// Suggestiong for using type assertions and annotations outside of TypeScript files
145+
fn suggest_type_assertion_in_js_not_allowed() -> Option<Suggestion> {
146+
Some(Suggestion {
147+
suggestions: vec!["Type assertions are not allowed in JavaScript files.".to_string()],
148+
help: Some(
149+
"Consider converting the file to TypeScript or removing the type assertion."
150+
.to_string(),
151+
),
152+
span: None,
153+
})
154+
}
155+
77156
/// Suggestion for TS95050
78-
fn unreachable_suggestion() -> Option<Suggestion> {
157+
fn suggest_unreachable() -> Option<Suggestion> {
79158
Some(Suggestion {
80159
suggestions: vec!["Code here is unreachable".to_string()],
81160
help: Some("Consider removing unreachable code or the statement that causes this to be unreachable".to_string()),

src/error/codes.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub enum ErrorCode {
55
InlineTypeMismatch,
66
PropertyMissingInType,
77
PropertyDoesNotExist,
8+
TypeAssertionInJsNotAllowed,
9+
MappedTypeMustBeStatic,
810

911
// null safety
1012
ObjectIsPossiblyNull,
@@ -45,15 +47,19 @@ pub enum ErrorCode {
4547
MissingReturnValue,
4648
InvalidIndexType,
4749
InvalidIndexTypeSignature,
50+
ElementImplicitAnyInvalidIndexTypeForObject,
4851
TypoPropertyOnType,
4952
UniqueObjectMemberNames,
5053
UninitializedConst,
5154
YieldNotInGenerator,
52-
JsxFlagNotProvided,
5355
DeclaredButNeverUsed,
5456
ImportedButNeverUsed,
5557
UnreachableCode,
5658

59+
// JSX related
60+
JsxFlagNotProvided,
61+
MissingJsxIntrinsicElementsDeclaration,
62+
5763
/// Catch-all for unsupported error codes
5864
Unsupported(u16),
5965
}
@@ -101,6 +107,10 @@ impl ErrorCode {
101107
"TS6192" => ErrorCode::ImportedButNeverUsed,
102108
"TS1259" => ErrorCode::InvalidDefaultImport,
103109
"TS95050" => ErrorCode::UnreachableCode,
110+
"TS8016" | "TS8010" => ErrorCode::TypeAssertionInJsNotAllowed,
111+
"TS7061" => ErrorCode::MappedTypeMustBeStatic,
112+
"TS7053" => ErrorCode::ElementImplicitAnyInvalidIndexTypeForObject,
113+
"TS7026" => ErrorCode::MissingJsxIntrinsicElementsDeclaration,
104114

105115
other => {
106116
if let Some(num_str) = other.strip_prefix("TS")
@@ -156,6 +166,10 @@ impl ErrorCode {
156166
ErrorCode::NoExportedMember => "TS2305",
157167
ErrorCode::InvalidDefaultImport => "TS1259",
158168
ErrorCode::UnreachableCode => "TS95050",
169+
ErrorCode::TypeAssertionInJsNotAllowed => "TS8016",
170+
ErrorCode::MappedTypeMustBeStatic => "TS7061",
171+
ErrorCode::ElementImplicitAnyInvalidIndexTypeForObject => "TS7053",
172+
ErrorCode::MissingJsxIntrinsicElementsDeclaration => "TS7026",
159173
ErrorCode::Unsupported(_) => {
160174
// This will return a static string for known codes, but for unsupported codes,
161175
// we return a dynamically allocated string. To keep the return type consistent,

0 commit comments

Comments
 (0)