Skip to content

Commit dfea71e

Browse files
committed
Handle interfaces
1 parent 80892dc commit dfea71e

File tree

3 files changed

+123
-16
lines changed

3 files changed

+123
-16
lines changed

src/LazyCoder/BaseCoder.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,21 @@ protected virtual TsInterface Rewrite(CsClass csClass)
7878
.Select(Rewrite)
7979
.ToArray();
8080

81+
var baseTypes = new List<Type>();
82+
if (csClass.CsType.OriginalType.BaseType != typeof(object))
83+
{
84+
baseTypes.Add(csClass.CsType.OriginalType.BaseType);
85+
}
86+
87+
baseTypes.AddRange(GetInterfaces(csClass.CsType.OriginalType, false));
88+
8189
return new TsInterface
8290
{
8391
CsType = csClass.CsType,
8492
Name = csClass.Name,
8593
ExportKind = TsExportKind.Named,
8694
TypeParameters = csClass.TypeParameters,
87-
Base = csClass.CsType.OriginalType.BaseType == typeof(object)
88-
? Array.Empty<TsType>()
89-
: new[]
90-
{
91-
TsType.From(new CsType(csClass.CsType.OriginalType
92-
.BaseType))
93-
},
95+
Base = baseTypes.Select(x=> TsType.From(new CsType(x))).ToArray(),
9496
Properties = properties.Concat(fields).ToArray()
9597
};
9698
}
@@ -103,6 +105,9 @@ protected virtual TsInterface Rewrite(CsInterface csInterface)
103105
Name = csInterface.Name,
104106
ExportKind = TsExportKind.Named,
105107
TypeParameters = csInterface.TypeParameters,
108+
Base = GetInterfaces(csInterface.CsType.OriginalType, false)
109+
.Select(x=> TsType.From(new CsType(x)))
110+
.ToArray(),
106111
Properties = csInterface.Members
107112
.Where(x => !x.IsStatic)
108113
.OfType<CsProperty>()
@@ -113,12 +118,21 @@ protected virtual TsInterface Rewrite(CsInterface csInterface)
113118

114119
protected virtual TsInterface Rewrite(CsStruct csStruct)
115120
{
121+
var baseTypes = new List<Type>();
122+
if (csStruct.CsType.OriginalType.BaseType != typeof(object))
123+
{
124+
baseTypes.Add(csStruct.CsType.OriginalType.BaseType);
125+
}
126+
127+
baseTypes.AddRange(GetInterfaces(csStruct.CsType.OriginalType, false));
128+
116129
return new TsInterface
117130
{
118131
CsType = csStruct.CsType,
119132
Name = csStruct.Name,
120133
ExportKind = TsExportKind.Named,
121134
TypeParameters = csStruct.TypeParameters,
135+
Base = baseTypes.Select(x=> TsType.From(new CsType(x))).ToArray(),
122136
Properties = csStruct.Members
123137
.Where(x => !x.IsStatic)
124138
.OfType<CsProperty>()
@@ -127,6 +141,16 @@ protected virtual TsInterface Rewrite(CsStruct csStruct)
127141
};
128142
}
129143

144+
private static IEnumerable<Type> GetInterfaces(Type type, bool includeInherited)
145+
{
146+
if (includeInherited || type.BaseType == null)
147+
{
148+
return type.GetInterfaces();
149+
}
150+
151+
return type.GetInterfaces().Except(type.BaseType.GetInterfaces());
152+
}
153+
130154
protected virtual TsTypeMember? Rewrite(CsTypeMember csTypeMember)
131155
{
132156
switch (csTypeMember)

tests/LazyCoder.Tests/LazyCoder.Tests.csproj

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
4+
<TargetFramework>net5.0</TargetFramework>
65
<IsPackable>false</IsPackable>
7-
8-
<LangVersion>8</LangVersion>
9-
6+
<LangVersion>9</LangVersion>
107
<Nullable>enable</Nullable>
118
</PropertyGroup>
129

1310
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
15-
<PackageReference Include="Shouldly" Version="3.0.2" />
16-
<PackageReference Include="xunit" Version="2.4.0" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
12+
<PackageReference Include="Shouldly" Version="4.0.3" />
13+
<PackageReference Include="xunit" Version="2.4.1" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
1815
</ItemGroup>
1916

2017
<ItemGroup>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using LazyCoder.CSharp;
4+
using LazyCoder.Typescript;
5+
using Shouldly;
6+
using Xunit;
7+
8+
namespace LazyCoder.Tests.Samples.Inheritance
9+
{
10+
public class InterfacesSample
11+
{
12+
[Fact]
13+
public void Test()
14+
{
15+
var tsFiles = Converter.Convert(
16+
new[]
17+
{
18+
typeof(IInterface), typeof(FirstClass), typeof(SecondClass),
19+
typeof(ISecondInterface), typeof(IThirdInterface),
20+
},
21+
new[] { new ContractCoder() });
22+
23+
tsFiles.Select(x => x.Name)
24+
.ShouldBe(new[] { "IInterface", "FirstClass", "SecondClass", "ISecondInterface", "IThirdInterface" },
25+
ignoreOrder: true);
26+
27+
Converter.WriteFileToString(tsFiles.Single(x => x.Name == "IInterface"))
28+
.ShouldBeLines("export interface IInterface {",
29+
"}",
30+
"");
31+
32+
Converter.WriteFileToString(tsFiles.Single(x => x.Name == "FirstClass"))
33+
.ShouldBeLines("import { IInterface } from \"./IInterface\";",
34+
"",
35+
"export interface FirstClass extends IInterface {",
36+
"}",
37+
"");
38+
39+
Converter.WriteFileToString(tsFiles.Single(x => x.Name == "ISecondInterface"))
40+
.ShouldBeLines("import { IInterface } from \"./IInterface\";",
41+
"",
42+
"export interface ISecondInterface extends IInterface {",
43+
"}",
44+
"");
45+
46+
Converter.WriteFileToString(tsFiles.Single(x => x.Name == "SecondClass"))
47+
.ShouldBeLines("import { FirstClass } from \"./FirstClass\";",
48+
"import { IThirdInterface } from \"./IThirdInterface\";",
49+
"",
50+
"export interface SecondClass extends FirstClass, IThirdInterface {",
51+
"}",
52+
"");
53+
}
54+
55+
private class ContractCoder: ICoder
56+
{
57+
public IEnumerable<TsFile> Rewrite(IEnumerable<CsDeclaration> csDeclarations)
58+
{
59+
return csDeclarations.Where(x =>
60+
x.CsType.OriginalType == typeof(FirstClass) ||
61+
x.CsType.OriginalType == typeof(SecondClass))
62+
.Select(x => new DefaultCoder().Rewrite(x));
63+
}
64+
}
65+
66+
private interface IInterface
67+
{
68+
}
69+
70+
private class FirstClass: IInterface
71+
{
72+
}
73+
74+
private interface ISecondInterface: IInterface
75+
{
76+
}
77+
78+
private interface IThirdInterface
79+
{
80+
}
81+
82+
private class SecondClass: FirstClass, IThirdInterface
83+
{
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)