Skip to content

Commit 0de6098

Browse files
committed
SourceGenerators
1 parent 64d6d05 commit 0de6098

6 files changed

Lines changed: 67 additions & 68 deletions

File tree

Source/Expr.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,6 @@
77
using System.Linq.Expressions;
88
using System.Reflection;
99

10-
#if !NETCOREAPP3_1
11-
12-
namespace System.Diagnostics.CodeAnalysis
13-
{
14-
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
15-
internal sealed class NotNullIfNotNullAttribute : Attribute
16-
{
17-
public NotNullIfNotNullAttribute(string parameterName)
18-
{
19-
ParameterName = parameterName;
20-
}
21-
22-
public string ParameterName { get; }
23-
}
24-
25-
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
26-
internal sealed class NotNullAttribute : Attribute
27-
{
28-
}
29-
}
30-
31-
#endif
32-
3310
namespace Linq.Expressions.Deconstruct
3411
{
3512
public abstract partial class Expr

Source/Linq.Expressions.Deconstruct.csproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@
88
<AssemblyOriginatorKeyFile>Linq.Expressions.Deconstruct.snk</AssemblyOriginatorKeyFile>
99
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1010
<Authors>Igor Tkachev</Authors>
11-
<PackageLicenseFile>LICENSE.TXT</PackageLicenseFile>
1211
<Copyright>Igor Tkachev</Copyright>
1312
<PackageProjectUrl>https://github.com/igor-tkachev/Linq.Expressions.Deconstruct</PackageProjectUrl>
1413
<RepositoryUrl>https://github.com/igor-tkachev/Linq.Expressions.Deconstruct</RepositoryUrl>
1514
<RepositoryType>git</RepositoryType>
1615
<PackageTags>System.Linq.Expressions Linq Expressions Deconstruct</PackageTags>
1716
<Description>Pattern matching support for System.Linq.Expressions.</Description>
17+
<PackageLicenseFile>LICENSE.TXT</PackageLicenseFile>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
1919
<Version>1.0.8</Version>
20+
21+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
22+
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
2023
</PropertyGroup>
2124

2225
<ItemGroup>
2326
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
2427

2528
<None Include="..\LICENSE.TXT" Pack="true" PackagePath="\" />
26-
<None Include="..\README.md" Pack="true" PackagePath="\" />
29+
<None Include="..\README.md" Pack="true" PackagePath="\" />
30+
31+
<ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
32+
33+
<PackageReference Include="PolySharp" Version="1.13.2" />
2734

2835
<Compile Update="Expr.generated.cs">
2936
<DesignTime>True</DesignTime>
@@ -34,8 +41,5 @@
3441
<Generator>TextTemplatingFileGenerator</Generator>
3542
<LastGenOutput>Expr.generated.cs</LastGenOutput>
3643
</None>
37-
38-
<ProjectReference Include="..\SourceGenerators\SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
39-
4044
</ItemGroup>
4145
</Project>

SourceGenerators/SourceGenerators.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
10+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
1111
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
12+
<PackageReference Include="PolySharp" Version="1.13.2" />
1213
</ItemGroup>
1314
</Project>

SourceGenerators/ToExprGenerator.cs

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System.Text;
66

77
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.CSharp.Syntax;
9-
using Microsoft.CodeAnalysis.Text;
1010

1111
namespace SourceGenerators
1212
{
@@ -23,19 +23,25 @@ public void Initialize(GeneratorInitializationContext context)
2323
#endif
2424

2525
context.RegisterForPostInitialization(i => i.AddSource("ToExprAttribute.g.cs",
26-
@"// <auto-generated/>
27-
using System;
26+
"""
27+
// <auto-generated/>
28+
#pragma warning disable
29+
#nullable enable annotations
2830
29-
namespace Linq.Expressions.Deconstruct
30-
{
31-
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
32-
[System.Diagnostics.Conditional(""ToExprGenerator_DEBUG"")]
33-
sealed class ToExprAttribute : Attribute
34-
{
35-
public string PropertyName { get; set; }
36-
public bool IsNullable { get; set; }
37-
}
38-
}"));
31+
using System;
32+
33+
namespace Linq.Expressions.Deconstruct
34+
{
35+
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
36+
[System.Diagnostics.Conditional("ToExprGenerator_DEBUG")]
37+
sealed class ToExprAttribute : Attribute
38+
{
39+
public string PropertyName { get; set; }
40+
public bool IsNullable { get; set; }
41+
}
42+
}
43+
44+
"""));
3945

4046
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
4147
}
@@ -63,22 +69,29 @@ string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fields, ISy
6369

6470
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
6571

66-
var source = new StringBuilder($$"""
67-
#nullable enable
72+
var source = new StringBuilder(
73+
$$"""
74+
// <auto-generated/>
75+
#pragma warning disable
76+
#nullable enable annotations
6877
69-
namespace {{namespaceName}}
70-
{
71-
partial class Expr
72-
{
73-
partial class {{classSymbol.Name}}
74-
{
75-
""");
78+
using System;
79+
80+
#nullable enable
81+
82+
namespace {{namespaceName}}
83+
{
84+
partial class Expr
85+
{
86+
partial class {{classSymbol.Name}}
87+
{
88+
89+
""");
7690

7791
foreach (var fieldSymbol in fields)
7892
ProcessField(source, classSymbol, fieldSymbol, attributeSymbol);
7993

8094
source
81-
.AppendLine()
8295
.AppendLine(" }")
8396
.AppendLine(" }")
8497
.AppendLine("}")
@@ -110,25 +123,29 @@ void ProcessField(StringBuilder source, INamedTypeSymbol classSymbol, IFieldSymb
110123
propertyName = overridenName.Value!.ToString();
111124
}
112125

113-
if (propertyName.Length == 0 || propertyName == fieldName)
126+
if (propertyName.Length == 0 || propertyName == fieldName || classSymbol.MemberNames.Contains(propertyName))
114127
{
115-
source.AppendLine($"#error Generator failed for : {fieldName}.");
116-
return;
117-
}
128+
if (fieldSymbol.Locations[0] is { Kind: LocationKind.SourceFile } location)
129+
{
130+
var ls = location.GetMappedLineSpan();
131+
source
132+
.AppendLine($"#line ({ls.Span.Start.Line + 1},{ls.Span.Start.Character})-({ls.Span.End.Line + 1},{ls.Span.End.Character}) \"{ls.Path}\"")
133+
;
134+
}
118135

119-
// if the class doesn't implement INotifyPropertyChanged already, add it
120-
if (classSymbol.MemberNames.Contains(propertyName))
121-
{
122-
source.AppendLine($"#error Generator failed for : {fieldName}.");
136+
source
137+
.AppendLine($"#error Generator failed on '{fieldName}'.")
138+
.AppendLine("#line default")
139+
;
123140
return;
124141
}
125142

126143
if (isNullable.IsNull || isNullable.Value is false)
127144
fieldType = fieldType.TrimEnd('?');
128145

129146
source.Append($$"""
130-
131147
public {{fieldType}} {{propertyName}} => {{fieldName}} ??= Expr.{{propertyName}}.ToExpr();
148+
132149
""");
133150
}
134151

@@ -138,7 +155,7 @@ public class SyntaxReceiver : ISyntaxContextReceiver
138155

139156
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
140157
{
141-
if (context.Node is FieldDeclarationSyntax { AttributeLists.Count: > 0 } field)
158+
if (context.Node is FieldDeclarationSyntax { AttributeLists: [_, ..] } field)
142159
{
143160
foreach (var variable in field.Declaration.Variables)
144161
{

Tests/Linq.Expressions.Deconstruct.Tests.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Appveyor.TestLogger" Version="2.0.0" />
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
12-
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
13-
<PackageReference Include="NUnit" Version="3.12.0" />
14-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
10+
<PackageReference Include="Appveyor.TestLogger" Version="2.0.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
12+
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
13+
<PackageReference Include="NUnit" Version="3.12.0" />
14+
<PackageReference Include="PolySharp" Version="1.13.2" />
1515

1616
<ProjectReference Include="..\Source\Linq.Expressions.Deconstruct.csproj" />
1717
</ItemGroup>

Tests/Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static string GetDebugView(Expression expression)
378378
}
379379
catch (ArgumentException)
380380
{
381-
_getDebugView = e => e.ToString();
381+
_getDebugView = e => e.ToString()!;
382382
}
383383
}
384384

0 commit comments

Comments
 (0)