Skip to content

Commit f350c1d

Browse files
authored
Merge pull request #7 from BurningMarshmallow/master
Add support for nullable reference types in properties
2 parents 50db770 + 2982e6e commit f350c1d

File tree

14 files changed

+21
-19
lines changed

14 files changed

+21
-19
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup .NET
1717
uses: actions/setup-dotnet@v1
1818
with:
19-
dotnet-version: 5.0.x
19+
dotnet-version: 6.0.x
2020
- name: Restore dependencies
2121
run: dotnet restore
2222
- name: Build

.github/workflows/pr.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup .NET
1717
uses: actions/setup-dotnet@v1
1818
with:
19-
dotnet-version: 5.0.x
19+
dotnet-version: 6.0.x
2020
- name: Restore dependencies
2121
run: dotnet restore
2222
- name: Build

src/LazyCoder/BaseCoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ private static IEnumerable<Type> GetInterfaces(Type type, bool includeInherited)
156156
switch (csTypeMember)
157157
{
158158
case CsProperty csProperty:
159-
var forceNullable = csProperty.Attributes
160-
.Any(a => a.Name.Contains("CanBeNull"));
159+
var forceNullable = csProperty.Attributes.Any(a => a.Name.Contains("CanBeNull"))
160+
|| csProperty.IsNullable;
161161
return new TsPropertySignature
162162
{
163163
Name = csProperty.Name,

src/LazyCoder/CSharp/CsClass.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32

43
namespace LazyCoder.CSharp
54
{

src/LazyCoder/CSharp/CsInterface.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32

43
namespace LazyCoder.CSharp
54
{

src/LazyCoder/CSharp/CsProperty.cs

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace LazyCoder.CSharp
33
public class CsProperty: CsTypeMember
44
{
55
public CsType Type { get; set; }
6+
public bool IsNullable { get; set; }
67
}
78
}

src/LazyCoder/CsDeclarationFactory.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ private static CsTypeMember Create(FieldInfo fieldInfo)
149149
return null;
150150
}
151151

152+
var context = new NullabilityInfoContext();
153+
var info = context.Create(fieldInfo);
152154
return new CsField
153155
{
154156
Name = fieldInfo.Name,
@@ -213,6 +215,8 @@ private static CsProperty Create(PropertyInfo propertyInfo)
213215
return null;
214216
}
215217

218+
var context = new NullabilityInfoContext();
219+
var info = context.Create(propertyInfo);
216220
return new CsProperty
217221
{
218222
Name = propertyInfo.Name,
@@ -229,7 +233,8 @@ private static CsProperty Create(PropertyInfo propertyInfo)
229233
OriginalType = x.AttributeType
230234
})
231235
.ToArray(),
232-
Type = new CsType(propertyInfo.PropertyType)
236+
Type = new CsType(propertyInfo.PropertyType),
237+
IsNullable = info.ReadState == NullabilityState.Nullable
233238
};
234239
}
235240

src/LazyCoder/DefaultCoder.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using LazyCoder.CSharp;
2-
using LazyCoder.Typescript;
3-
41
namespace LazyCoder
52
{
63
public class DefaultCoder: BaseCoder

src/LazyCoder/LazyCoder.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
<LangVersion>9</LangVersion>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<LangVersion>10</LangVersion>
66
<Nullable>enable</Nullable>
77
<Version>0.03.12</Version>
8+
<ImplicitUsings>enable</ImplicitUsings>
89
</PropertyGroup>
910

1011
</Project>

src/LazyCoder/Typescript/TsTypeReference.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using LazyCoder.CSharp;
43

54
namespace LazyCoder.Typescript

tests/LazyCoder.Tests/LazyCoder.Tests.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6-
<LangVersion>9</LangVersion>
6+
<LangVersion>10</LangVersion>
77
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
89
</PropertyGroup>
910

1011
<ItemGroup>

tests/LazyCoder.Tests/Samples/Literals/Simple.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Diagnostics.Contracts;
2-
using System.Linq;
1+
using System.Linq;
32
using Shouldly;
43
using Xunit;
54

tests/LazyCoder.Tests/Samples/Simple/FirstClass.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { SecondClass } from "./Simple/SecondClass";
33

44
export interface FirstClass {
55
StringProperty: string;
6+
StringNullableProperty: (string | null);
67
SecondClassProperty: SecondClass;
78
}

tests/LazyCoder.Tests/Samples/Simple/Simple.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
@@ -69,7 +68,7 @@ private static TsTypeMember[] GetProperties(CsClass csClass)
6968
.Select(p => new TsPropertySignature
7069
{
7170
Name = p.Name,
72-
Type = TsType.From(p.Type),
71+
Type = TsType.From(p.Type, p.IsNullable),
7372
Optional = false
7473
})
7574
.ToArray();
@@ -79,6 +78,7 @@ private static TsTypeMember[] GetProperties(CsClass csClass)
7978
private class FirstClass
8079
{
8180
public string StringProperty { get; set; }
81+
public string? StringNullableProperty { get; set; }
8282
public SecondClass SecondClassProperty { get; set; }
8383
}
8484

0 commit comments

Comments
 (0)