1- using Mono . Cecil ;
2- using System ;
1+ using System ;
32using System . Collections . Generic ;
43using System . IO ;
54using System . Linq ;
65using System . Text . RegularExpressions ;
6+ using Mono . Cecil ;
77using WebApiToTypeScript . Config ;
88
99namespace WebApiToTypeScript . Types
@@ -13,22 +13,28 @@ public class TypeService : ServiceAware
1313 private Dictionary < string , List < Type > > PrimitiveTypesMapping { get ; }
1414 = new Dictionary < string , List < Type > > ( ) ;
1515
16+ private List < string > ReservedWords { get ; set ; }
17+ = new List < string > ( ) ;
18+
1619 public List < TypeDefinition > Types { get ; }
1720 = new List < TypeDefinition > ( ) ;
1821
1922 public TypeService ( )
2023 {
2124 LoadPrimitiveTypesMapping ( ) ;
25+ LoadReservedWords ( ) ;
2226 }
2327
24- private void LoadPrimitiveTypesMapping ( )
28+ public bool IsReservedWord ( string word )
2529 {
26- var mapping = PrimitiveTypesMapping ;
30+ return ReservedWords . Contains ( word ) ;
31+ }
2732
28- mapping [ "string" ] = new List < Type > { typeof ( string ) , typeof ( System . Guid ) , typeof ( DateTime ) , typeof ( TimeSpan ) } ;
29- mapping [ "boolean" ] = new List < Type > { typeof ( bool ) } ;
30- mapping [ "number" ] = new List < Type > { typeof ( byte ) , typeof ( short ) , typeof ( int ) , typeof ( long ) , typeof ( float ) , typeof ( double ) , typeof ( decimal ) } ;
31- mapping [ "any" ] = new List < Type > { typeof ( object ) } ;
33+ public string FixIfReservedWord ( string word )
34+ {
35+ return IsReservedWord ( word )
36+ ? $ "_{ word } "
37+ : word ;
3238 }
3339
3440 public void LoadAllTypes ( string webApiModuleFilePath )
@@ -297,5 +303,35 @@ public string StripCollection(TypeReference type)
297303
298304 return null ;
299305 }
306+
307+ private void LoadReservedWords ( )
308+ {
309+ ReservedWords = new List < string >
310+ {
311+ "break" , "case" , "catch" , "class" , "const" ,
312+ "continue" , "debugger" , "default" , "delete" , "do" ,
313+ "else" , "enum" , "export" , "extends" , "false" ,
314+ "finally" , "for" , "function" , "if" , "import" ,
315+ "in" , "instanceof" , "new" , "null" , "return" ,
316+ "super" , "switch" , "this" , "throw" , "true" ,
317+ "try" , "typeof" , "var" , "void" , "while" ,
318+ "with" , "as" , "implements" , "interface" , "let" ,
319+ "package" , "private" , "protected" , "public" , "static" ,
320+ "yield" , "any" , "boolean" , "constructor" , "declare" ,
321+ "get" , "module" , "require" , "number" , "set" ,
322+ "string" , "symbol" , "type" , "from" , "of" ,
323+ "namespace" , "async" , "await"
324+ } ;
325+ }
326+
327+ private void LoadPrimitiveTypesMapping ( )
328+ {
329+ var mapping = PrimitiveTypesMapping ;
330+
331+ mapping [ "string" ] = new List < Type > { typeof ( string ) , typeof ( Guid ) , typeof ( DateTime ) , typeof ( TimeSpan ) } ;
332+ mapping [ "boolean" ] = new List < Type > { typeof ( bool ) } ;
333+ mapping [ "number" ] = new List < Type > { typeof ( byte ) , typeof ( short ) , typeof ( int ) , typeof ( long ) , typeof ( float ) , typeof ( double ) , typeof ( decimal ) } ;
334+ mapping [ "any" ] = new List < Type > { typeof ( object ) } ;
335+ }
300336 }
301337}
0 commit comments