Skip to content

Commit 3a270e3

Browse files
gliljasmaca88
andauthored
Omit init properties (#169)
* Omit init properties * Add test for NewType and make init work for older .NET versions Co-authored-by: maca88 <bostjan.markezic@siol.net>
1 parent 1c4f814 commit 3a270e3

File tree

7 files changed

+184
-3
lines changed

7 files changed

+184
-3
lines changed

Source/AsyncGenerator.Tests/AsyncGenerator.Tests.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>$(TestTargetFramework)</TargetFramework>
55
<IsPackable>false</IsPackable>
6-
<LangVersion>7.3</LangVersion>
6+
<LangVersion>9.0</LangVersion>
77
<NoWarn Condition="'$(TargetFramework)' == 'netcoreapp3.1'">CS8032</NoWarn>
88
</PropertyGroup>
99

@@ -55,6 +55,10 @@
5555
<EmbeddedResource Include="**\*.txt;**\*.yml;**\*.xml" Exclude="bin\**\*.*;obj\**\*.*" />
5656
</ItemGroup>
5757

58+
<ItemGroup>
59+
<None Remove="AsyncProperites\Output\InitSetter.txt" />
60+
</ItemGroup>
61+
5862
<ItemGroup>
5963
<Compile Include="..\AsyncGenerator\Internal\EnvironmentHelper.cs">
6064
<Link>EnvironmentHelper.cs</Link>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AsyncGenerator.TestCases;
7+
8+
namespace AsyncGenerator.Tests.AsyncProperites.Input
9+
{
10+
public class InitSetter
11+
{
12+
public string InitWriteSuccess
13+
{
14+
init { Write(value); }
15+
}
16+
17+
public string WriteSuccess
18+
{
19+
set { Write(value); }
20+
}
21+
22+
private bool Write(string value)
23+
{
24+
return SimpleFile.Write(value);
25+
}
26+
27+
protected void SetSuccess()
28+
{
29+
WriteSuccess = "";
30+
}
31+
}
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
using AsyncGenerator.TestCases;
17+
18+
namespace AsyncGenerator.Tests.AsyncProperites.Input
19+
{
20+
public partial class InitSetter
21+
{
22+
23+
public Task SetWriteSuccessAsync(string value)
24+
{
25+
return WriteAsync(value);
26+
}
27+
28+
private Task<bool> WriteAsync(string value)
29+
{
30+
return SimpleFile.WriteAsync(value);
31+
}
32+
33+
protected async Task SetSuccessAsync()
34+
{
35+
await (SetWriteSuccessAsync(""));
36+
}
37+
}
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
using AsyncGenerator.TestCases;
17+
18+
namespace AsyncGenerator.Tests.AsyncProperites.Input
19+
{
20+
public class InitSetterAsync
21+
{
22+
public string InitWriteSuccess
23+
{
24+
init { Write(value); }
25+
}
26+
27+
public string WriteSuccess
28+
{
29+
set { Write(value); }
30+
}
31+
32+
public Task SetWriteSuccessAsync(string value)
33+
{
34+
return WriteAsync(value);
35+
}
36+
37+
private Task<bool> WriteAsync(string value)
38+
{
39+
return SimpleFile.WriteAsync(value);
40+
}
41+
42+
private bool Write(string value)
43+
{
44+
return SimpleFile.Write(value);
45+
}
46+
47+
protected async Task SetSuccessAsync()
48+
{
49+
await (SetWriteSuccessAsync(""));
50+
}
51+
}
52+
}

Source/AsyncGenerator.Tests/AsyncProperites/SetterFixture.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,48 @@ public Task TestSetAccessorAfterTransformation()
162162
)
163163
);
164164
}
165+
166+
[Test]
167+
public Task TestInitSetterAfterTransformation()
168+
{
169+
return ReadonlyTest(nameof(InitSetter), p => p
170+
.ConfigureAnalyzation(a => a
171+
.MethodConversion(symbol => MethodConversion.Smart)
172+
.PropertyConversion(true)
173+
)
174+
.ConfigureTransformation(t => t
175+
.AfterTransformation(result =>
176+
{
177+
AssertValidAnnotations(result);
178+
Assert.AreEqual(1, result.Documents.Count);
179+
var document = result.Documents[0];
180+
Assert.NotNull(document.OriginalModified);
181+
Assert.AreEqual(GetOutputFile(nameof(InitSetter)), document.Transformed.ToFullString());
182+
})
183+
)
184+
);
185+
}
186+
187+
[Test]
188+
public Task TestInitSetterNewTypeAfterTransformation()
189+
{
190+
return ReadonlyTest(nameof(InitSetter), p => p
191+
.ConfigureAnalyzation(a => a
192+
.MethodConversion(symbol => MethodConversion.Smart)
193+
.PropertyConversion(true)
194+
.TypeConversion(o => TypeConversion.NewType)
195+
)
196+
.ConfigureTransformation(t => t
197+
.AfterTransformation(result =>
198+
{
199+
AssertValidAnnotations(result);
200+
Assert.AreEqual(1, result.Documents.Count);
201+
var document = result.Documents[0];
202+
Assert.IsNull(document.OriginalModified);
203+
Assert.AreEqual(GetOutputFile($"{nameof(InitSetter)}NewType"), document.Transformed.ToFullString());
204+
})
205+
)
206+
);
207+
}
165208
}
166209
}

Source/AsyncGenerator/Internal/EnvironmentHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
using Microsoft.Build.Locator;
1111
using Microsoft.CodeAnalysis.MSBuild;
1212

13+
#if NET472 || (NETCOREAPP && !NET5_0_OR_GREATER)
14+
15+
// To make init (C# 9.0) work with older .NET versions
16+
namespace System.Runtime.CompilerServices
17+
{
18+
internal static class IsExternalInit
19+
{
20+
}
21+
}
22+
#endif
23+
1324
#if NETCOREAPP || NET472
1425

1526
namespace AsyncGenerator.Internal

Source/AsyncGenerator/Internal/PropertyData.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ public PropertyData(TypeData typeData, IPropertySymbol symbol, PropertyDeclarati
2424
? (SyntaxNode)Node.AccessorList.Accessors.First(o => o.Keyword.IsKind(SyntaxKind.GetKeyword))
2525
: Node.ExpressionBody);
2626
}
27-
if (Symbol.SetMethod != null)
27+
//Ignore init setters
28+
if (Symbol.SetMethod != null && Node.AccessorList.Accessors.FirstOrDefault(o => o.Keyword.IsKind(SyntaxKind.SetKeyword)) is AccessorDeclarationSyntax accessorDeclaration)
2829
{
29-
SetAccessorData = new AccessorData(this, Symbol.SetMethod, Node.AccessorList.Accessors.First(o => o.Keyword.IsKind(SyntaxKind.SetKeyword)));
30+
SetAccessorData = new AccessorData(this, Symbol.SetMethod, accessorDeclaration);
3031
}
3132
}
3233

0 commit comments

Comments
 (0)