@@ -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 ( ) ) ,
0 commit comments