Skip to content

Commit 94a23a5

Browse files
committed
Add list of reserved words which we replace if they occur as variable names (at least in some places, for now)
1 parent 16b2c75 commit 94a23a5

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/WebApiToTypeScript/Interfaces/InterfaceService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ private void WriteInterfaces(TypeScriptBlock interfacesBlock, InterfaceNode inte
221221

222222
var collectionString = thing.CSharpType.IsCollection ? "[]" : string.Empty;
223223

224-
// todo-balki sigh
225-
if (thingName == "constructor")
226-
thingName = "_constructor";
224+
thingName = TypeService.FixIfReservedWord(thingName);
227225

228226
interfaceBlock
229227
.AddStatement($"{thingName}: {interfaceName}{collectionString};");

src/WebApiToTypeScript/Types/TypeService.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using Mono.Cecil;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
65
using System.Text.RegularExpressions;
6+
using Mono.Cecil;
77
using WebApiToTypeScript.Config;
88

99
namespace 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

Comments
 (0)