Skip to content

Commit 1947b91

Browse files
authored
Merge pull request #1 from roman-g/master
Converting C# const fields into TS literal types
2 parents 4bd7527 + cb077c6 commit 1947b91

19 files changed

+265
-14
lines changed

src/LazyCoder/BaseCoder.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ protected virtual TsEnumValue Rewrite(CsEnumValue x)
6767

6868
protected virtual TsInterface Rewrite(CsClass csClass)
6969
{
70+
var properties = csClass.Members
71+
.Where(x => !x.IsStatic)
72+
.OfType<CsProperty>()
73+
.Select(Rewrite)
74+
.ToArray();
75+
76+
var fields = csClass.Members
77+
.OfType<CsField>()
78+
.Select(Rewrite)
79+
.ToArray();
80+
7081
return new TsInterface
7182
{
7283
CsType = csClass.CsType,
@@ -80,11 +91,7 @@ protected virtual TsInterface Rewrite(CsClass csClass)
8091
TsType.From(new CsType(csClass.CsType.OriginalType
8192
.BaseType))
8293
},
83-
Properties = csClass.Members
84-
.Where(x => !x.IsStatic)
85-
.OfType<CsProperty>()
86-
.Select(Rewrite)
87-
.ToArray()
94+
Properties = properties.Concat(fields).ToArray()
8895
};
8996
}
9097

@@ -120,7 +127,7 @@ protected virtual TsInterface Rewrite(CsStruct csStruct)
120127
};
121128
}
122129

123-
protected virtual TsTypeMember Rewrite(CsTypeMember csTypeMember)
130+
protected virtual TsTypeMember? Rewrite(CsTypeMember csTypeMember)
124131
{
125132
switch (csTypeMember)
126133
{
@@ -133,6 +140,15 @@ protected virtual TsTypeMember Rewrite(CsTypeMember csTypeMember)
133140
Type = TsType.From(csProperty.Type,
134141
forceNullable)
135142
};
143+
case CsField csField:
144+
if (csField.Value == null)
145+
return null;
146+
147+
return new TsPropertySignature
148+
{
149+
Name = csField.Name,
150+
Type = TsType.FromLiteral(csField.Value)
151+
};
136152
default:
137153
throw new ArgumentOutOfRangeException(nameof(csTypeMember),
138154
csTypeMember.GetType().Name, null);

src/LazyCoder/CSharp/CsField.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsField: CsTypeMember
4+
{
5+
public CsType Type { get; set; }
6+
public CsLiteral? Value { get; set; }
7+
}
8+
}

src/LazyCoder/CSharp/CsLiteral.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.CSharp
2+
{
3+
public class CsLiteral
4+
{
5+
public CsType Type { get; set; }
6+
public object Value { get; set; }
7+
}
8+
}

src/LazyCoder/CsDeclarationFactory.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public static CsDeclaration Create(Type type)
4545
.ToArray()
4646
: Array.Empty<string>(),
4747
Members = type.GetDefinition()
48-
.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
48+
.GetMembers(BindingFlags.Public | BindingFlags.Instance |
49+
BindingFlags.DeclaredOnly |
50+
BindingFlags.Static)
4951
.Where(m => !typeof(object)
5052
.GetMembers()
5153
.Select(me => me.Name)
@@ -67,7 +69,8 @@ public static CsDeclaration Create(Type type)
6769
.ToArray()
6870
: Array.Empty<string>(),
6971
Members = type.GetDefinition()
70-
.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
72+
.GetMembers(BindingFlags.Public | BindingFlags.Instance |
73+
BindingFlags.DeclaredOnly)
7174
.Where(m => !typeof(object)
7275
.GetMembers()
7376
.Select(me => me.Name)
@@ -112,8 +115,9 @@ private static CsTypeMember Create(MemberInfo memberInfo)
112115
return Create(methodInfo);
113116
case PropertyInfo propertyInfo:
114117
return Create(propertyInfo);
118+
case FieldInfo fieldInfo:
119+
return Create(fieldInfo);
115120
case TypeInfo _:
116-
case FieldInfo _:
117121
case EventInfo _:
118122
return null;
119123
default:
@@ -122,6 +126,50 @@ private static CsTypeMember Create(MemberInfo memberInfo)
122126
}
123127
}
124128

129+
private static CsTypeMember Create(FieldInfo fieldInfo)
130+
{
131+
CsLiteral? GetLiteral(Type fieldType)
132+
{
133+
if (!fieldInfo.IsLiteral)
134+
{
135+
return null;
136+
}
137+
138+
var constantValue = fieldInfo.GetRawConstantValue();
139+
140+
if (fieldType == typeof(string)
141+
|| fieldType == typeof(int)
142+
|| fieldType == typeof(bool)
143+
|| fieldType.IsEnum)
144+
145+
{
146+
return new CsLiteral { Value = constantValue, Type = new CsType(fieldType) };
147+
}
148+
149+
return null;
150+
}
151+
152+
return new CsField
153+
{
154+
Name = fieldInfo.Name,
155+
IsStatic = fieldInfo.IsStatic,
156+
IsInherited = fieldInfo.DeclaringType != fieldInfo.ReflectedType,
157+
AccessModifier = GetAccessModifier(fieldInfo.IsPrivate,
158+
fieldInfo.IsFamily,
159+
fieldInfo.IsPublic,
160+
fieldInfo.IsAssembly),
161+
Attributes = fieldInfo.CustomAttributes
162+
.Select(x => new CsAttribute
163+
{
164+
Name = x.AttributeType.Name,
165+
OriginalType = x.AttributeType
166+
})
167+
.ToArray(),
168+
Type = new CsType(fieldInfo.FieldType),
169+
Value = GetLiteral(fieldInfo.FieldType)
170+
};
171+
}
172+
125173
private static CsMethod Create(MethodInfo methodInfo)
126174
{
127175
if (methodInfo.IsSpecialName)

src/LazyCoder/DependencyFinder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ private static IEnumerable<TsType> Unwrap(TsType tsType)
6060
return tsTypeReference.TypeArguments
6161
.SelectMany(Unwrap)
6262
.Append(tsTypeReference);
63+
case TsEnumLiteralType tsEnumLiteralType:
64+
return new[] { tsEnumLiteralType.EnumType };
6365
case TsNull _:
6466
case TsPredefinedType _:
6567
case TsStringLiteralType _:
68+
case TsBoolLiteralType _:
69+
case TsIntLiteralType _:
6670
return new[]
6771
{
6872
tsType

src/LazyCoder/LazyCoder.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<LangVersion>7.3</LangVersion>
5+
<LangVersion>8</LangVersion>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace LazyCoder.Typescript
2+
{
3+
public class TsBoolLiteralType: TsType
4+
{
5+
public bool Bool { get; set; }
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LazyCoder.Typescript
2+
{
3+
public class TsEnumLiteralType: TsType
4+
{
5+
public string Value { get; set; }
6+
public TsTypeReference EnumType { get; set; }
7+
}
8+
}

src/LazyCoder/Typescript/TsIndexSignature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ public static TsIndexSignature ByNumber(TsType valueType)
2323
public TsType IndexType { get; private set; }
2424
public TsType ValueType { get; private set; }
2525
}
26-
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace LazyCoder.Typescript
2+
{
3+
public class TsIntLiteralType: TsType
4+
{
5+
public int Int { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)