Skip to content

Commit fd5d13c

Browse files
General Improvements (#47)
* General code improvements * Update dependencies
1 parent 38a0801 commit fd5d13c

15 files changed

+252
-108
lines changed

Directory.Packages.props

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
</PropertyGroup>
55

66
<ItemGroup>
7-
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
7+
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.5.0" />
88
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
9-
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
10-
<PackageVersion Include="coverlet.msbuild" Version="6.0.0" />
9+
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
10+
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
1111
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
1212
<PackageVersion Include="Mediator.Abstractions" Version="2.1.7" />
1313
<PackageVersion Include="Mediator.SourceGenerator" Version="2.1.7" />
1414
<PackageVersion Include="MediatR" Version="12.2.0" />
15-
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
15+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
1616
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
1717
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
1818
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
@@ -22,17 +22,17 @@
2222
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
2323
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
2424
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
25-
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
26-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
25+
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
26+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
2727
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
28-
<PackageVersion Include="MinVer" Version="4.3.0" />
28+
<PackageVersion Include="MinVer" Version="5.0.0" />
2929
<PackageVersion Include="PolySharp" Version="1.14.1" />
3030
<PackageVersion Include="Scriban" Version="5.9.1" />
3131
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
3232
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
3333
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
34-
<PackageVersion Include="Verify.Xunit" Version="23.1.0" />
35-
<PackageVersion Include="xunit" Version="2.6.6" />
36-
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
34+
<PackageVersion Include="Verify.Xunit" Version="23.5.2" />
35+
<PackageVersion Include="xunit" Version="2.7.0" />
36+
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
3737
</ItemGroup>
3838
</Project>

src/Immediate.Handlers.Analyzers/BehaviorsAnalyzer.cs

+18-17
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
6969
if (context.Operation is not IAttributeOperation { Operation: IObjectCreationOperation attribute })
7070
return;
7171

72-
var behaviorsAttributeSymbol = context.Compilation.GetTypeByMetadataName("Immediate.Handlers.Shared.BehaviorsAttribute");
73-
if (behaviorsAttributeSymbol is null)
74-
return;
75-
76-
token.ThrowIfCancellationRequested();
77-
if (!SymbolEqualityComparer.Default.Equals(attribute.Type?.OriginalDefinition, behaviorsAttributeSymbol))
72+
if (!attribute.Type.IsBehaviorsAttribute())
7873
return;
7974

8075
if (attribute.Arguments.Length != 1)
@@ -86,10 +81,22 @@ private void AnalyzeOperation(OperationAnalysisContext context)
8681
token.ThrowIfCancellationRequested();
8782
var array = attribute.Arguments[0].Value;
8883

89-
var compilation = context.Compilation;
90-
var arrayTypeSymbol = compilation.CreateArrayTypeSymbol(compilation.GetTypeByMetadataName("System.Type")!, 1);
91-
if (!SymbolEqualityComparer.Default.Equals(array.Type, arrayTypeSymbol)
92-
|| array.ChildOperations.Count != 2
84+
if (array is not
85+
{
86+
Type: IArrayTypeSymbol
87+
{
88+
ElementType:
89+
{
90+
Name: "Type",
91+
ContainingNamespace:
92+
{
93+
Name: "System",
94+
ContainingNamespace.IsGlobalNamespace: true,
95+
},
96+
},
97+
},
98+
ChildOperations.Count: 2
99+
}
93100
|| array.ChildOperations.ElementAt(1) is not IArrayInitializerOperation aio)
94101
{
95102
// note: this will already be a compiler error anyway
@@ -117,7 +124,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
117124
var location = toes.Type.GetLocation();
118125
var originalDefinition = behaviorType.OriginalDefinition;
119126

120-
if (!ImplementsBaseClass(originalDefinition, baseBehaviorSymbol))
127+
if (!originalDefinition.ImplementsBehavior())
121128
{
122129
context.ReportDiagnostic(
123130
Diagnostic.Create(
@@ -148,10 +155,4 @@ private void AnalyzeOperation(OperationAnalysisContext context)
148155
}
149156
}
150157
}
151-
152-
private static bool ImplementsBaseClass(INamedTypeSymbol typeSymbol, INamedTypeSymbol typeToCheck) =>
153-
SymbolEqualityComparer.Default.Equals(typeSymbol, typeToCheck)
154-
|| (typeSymbol.BaseType is not null
155-
&& ImplementsBaseClass(typeSymbol.BaseType.OriginalDefinition, typeToCheck)
156-
);
157158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Immediate.Handlers.Analyzers;
4+
5+
internal static class ITypeSymbolExtensions
6+
{
7+
public static bool IsBehaviorsAttribute(this ITypeSymbol? typeSymbol) =>
8+
typeSymbol is
9+
{
10+
Name: "BehaviorsAttribute",
11+
ContainingNamespace:
12+
{
13+
Name: "Shared",
14+
ContainingNamespace:
15+
{
16+
Name: "Handlers",
17+
ContainingNamespace:
18+
{
19+
Name: "Immediate",
20+
ContainingNamespace.IsGlobalNamespace: true,
21+
},
22+
},
23+
},
24+
};
25+
26+
public static bool IsRenderModeAttribute(this ITypeSymbol? typeSymbol) =>
27+
typeSymbol is
28+
{
29+
Name: "RenderModeAttribute",
30+
ContainingNamespace:
31+
{
32+
Name: "Shared",
33+
ContainingNamespace:
34+
{
35+
Name: "Handlers",
36+
ContainingNamespace:
37+
{
38+
Name: "Immediate",
39+
ContainingNamespace.IsGlobalNamespace: true,
40+
},
41+
},
42+
},
43+
};
44+
45+
public static bool IsRenderMode(this ITypeSymbol? typeSymbol) =>
46+
typeSymbol is
47+
{
48+
Name: "RenderMode",
49+
ContainingNamespace:
50+
{
51+
Name: "Shared",
52+
ContainingNamespace:
53+
{
54+
Name: "Handlers",
55+
ContainingNamespace:
56+
{
57+
Name: "Immediate",
58+
ContainingNamespace.IsGlobalNamespace: true,
59+
},
60+
},
61+
},
62+
};
63+
64+
public static bool IsBehavior2(this ITypeSymbol typeSymbol) =>
65+
typeSymbol is
66+
{
67+
MetadataName: "Behavior`2",
68+
ContainingNamespace:
69+
{
70+
Name: "Shared",
71+
ContainingNamespace:
72+
{
73+
Name: "Handlers",
74+
ContainingNamespace:
75+
{
76+
Name: "Immediate",
77+
ContainingNamespace.IsGlobalNamespace: true,
78+
},
79+
},
80+
},
81+
};
82+
83+
public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) =>
84+
typeSymbol.IsBehavior2()
85+
|| (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition));
86+
}

src/Immediate.Handlers.Analyzers/RenderModeAnalyzer.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,14 @@ private void AnalyzeOperation(OperationAnalysisContext context)
4444
if (context.Operation is not IAttributeOperation { Operation: IObjectCreationOperation attribute })
4545
return;
4646

47-
var renderModeAttributeSymbol = context.Compilation.GetTypeByMetadataName("Immediate.Handlers.Shared.RenderModeAttribute");
48-
49-
token.ThrowIfCancellationRequested();
50-
if (!SymbolEqualityComparer.Default.Equals(attribute.Type?.OriginalDefinition, renderModeAttributeSymbol))
47+
if (!attribute.Type.IsRenderModeAttribute())
5148
return;
5249

5350
token.ThrowIfCancellationRequested();
54-
var renderModeSymbol = context.Compilation.GetTypeByMetadataName("Immediate.Handlers.Shared.RenderMode");
5551

5652
if (attribute.Arguments.Length != 1
5753
|| attribute.Arguments[0].Value is not IFieldReferenceOperation value
58-
|| !SymbolEqualityComparer.Default.Equals(value.Type?.OriginalDefinition, renderModeSymbol)
54+
|| !value.Type.IsRenderMode()
5955
|| value.Member.Name is "None")
6056
{
6157
context.ReportDiagnostic(Diagnostic.Create(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Immediate.Handlers.Generators;
4+
5+
internal static class ITypeSymbolExtensions
6+
{
7+
public static bool IsBehavior2(this ITypeSymbol typeSymbol) =>
8+
typeSymbol is
9+
{
10+
MetadataName: "Behavior`2",
11+
ContainingNamespace:
12+
{
13+
Name: "Shared",
14+
ContainingNamespace:
15+
{
16+
Name: "Handlers",
17+
ContainingNamespace:
18+
{
19+
Name: "Immediate",
20+
ContainingNamespace.IsGlobalNamespace: true,
21+
},
22+
},
23+
},
24+
};
25+
26+
public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) =>
27+
typeSymbol.IsBehavior2()
28+
|| (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition));
29+
30+
public static bool IsValueTask1(this ITypeSymbol typeSymbol) =>
31+
typeSymbol is INamedTypeSymbol
32+
{
33+
MetadataName: "ValueTask`1",
34+
ContainingNamespace:
35+
{
36+
Name: "Tasks",
37+
ContainingNamespace:
38+
{
39+
Name: "Threading",
40+
ContainingNamespace:
41+
{
42+
Name: "System",
43+
ContainingNamespace.IsGlobalNamespace: true
44+
}
45+
}
46+
}
47+
};
48+
49+
public static bool IsValueTask(this ITypeSymbol typeSymbol) =>
50+
typeSymbol is INamedTypeSymbol
51+
{
52+
Name: "ValueTask",
53+
ContainingNamespace:
54+
{
55+
Name: "Tasks",
56+
ContainingNamespace:
57+
{
58+
Name: "Threading",
59+
ContainingNamespace:
60+
{
61+
Name: "System",
62+
ContainingNamespace.IsGlobalNamespace: true
63+
}
64+
}
65+
}
66+
};
67+
68+
public static bool IsIEquatable1(this ITypeSymbol typeSymbol) =>
69+
typeSymbol is INamedTypeSymbol
70+
{
71+
MetadataName: "IEquatable`1",
72+
ContainingNamespace:
73+
{
74+
Name: "System",
75+
ContainingNamespace.IsGlobalNamespace: true
76+
},
77+
};
78+
}

src/Immediate.Handlers.Generators/Immediate.Handlers.Generators.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
66
<IsRoslynComponent>true</IsRoslynComponent>
7+
<NoWarn>$(NoWarn);CA1716</NoWarn>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<None Remove="Templates\*.sbntxt" />
11-
<EmbeddedResource Include="Templates\*.sbntxt" />
11+
<None Remove="Templates/*.sbntxt" />
12+
<EmbeddedResource Include="Templates/*.sbntxt" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<Compile Include="../Immediate.Handlers.Shared/RenderMode.cs" />
1217
</ItemGroup>
1318

1419
<ItemGroup>
@@ -19,18 +24,13 @@
1924
<PackageReference Include="MinVer" PrivateAssets="all" />
2025
</ItemGroup>
2126

22-
<ItemGroup>
23-
<ProjectReference Include="../Immediate.Handlers.Shared/Immediate.Handlers.Shared.csproj" PrivateAssets="all" />
24-
</ItemGroup>
25-
2627
<PropertyGroup>
2728
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
2829
</PropertyGroup>
2930

3031
<Target Name="GetDependencyTargetPaths">
3132
<ItemGroup>
3233
<TargetPathWithTargetPlatformMoniker Include="$(PkgScriban)/lib/netstandard2.0/Scriban.dll" IncludeRuntimeDependency="false" />
33-
<TargetPathWithTargetPlatformMoniker Include="$(MSBuildThisFileDirectory)bin/$(Configuration)/$(TargetFramework)/Immediate.Handlers.Shared.dll" IncludeRuntimeDependency="false" />
3434
</ItemGroup>
3535
</Target>
3636

src/Immediate.Handlers.Generators/ImmediateHandlers/ImmediateHandlersGenerator_Entrypoint.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ string GetVariableNameSuffix(string typeName)
230230

231231
private static bool ValidateType(string? type, GenericType implementedTypes) =>
232232
type is null
233-
|| implementedTypes.Implements
234-
.Contains(type);
233+
|| implementedTypes.Implements.Contains(type);
235234

236235
private static Template GetTemplate(string name)
237236
{

0 commit comments

Comments
 (0)