Skip to content

Commit

Permalink
General Improvements (#47)
Browse files Browse the repository at this point in the history
* General code improvements
* Update dependencies
  • Loading branch information
viceroypenguin authored Mar 21, 2024
1 parent 38a0801 commit fd5d13c
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 108 deletions.
20 changes: 10 additions & 10 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.4.5" />
<PackageVersion Include="Basic.Reference.Assemblies.Net80" Version="1.5.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageVersion Include="Mediator.Abstractions" Version="2.1.7" />
<PackageVersion Include="Mediator.SourceGenerator" Version="2.1.7" />
<PackageVersion Include="MediatR" Version="12.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
Expand All @@ -22,17 +22,17 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MinVer" Version="4.3.0" />
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
<PackageVersion Include="Scriban" Version="5.9.1" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
<PackageVersion Include="Verify.Xunit" Version="23.1.0" />
<PackageVersion Include="xunit" Version="2.6.6" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
<PackageVersion Include="Verify.Xunit" Version="23.5.2" />
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" />
</ItemGroup>
</Project>
35 changes: 18 additions & 17 deletions src/Immediate.Handlers.Analyzers/BehaviorsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
if (context.Operation is not IAttributeOperation { Operation: IObjectCreationOperation attribute })
return;

var behaviorsAttributeSymbol = context.Compilation.GetTypeByMetadataName("Immediate.Handlers.Shared.BehaviorsAttribute");
if (behaviorsAttributeSymbol is null)
return;

token.ThrowIfCancellationRequested();
if (!SymbolEqualityComparer.Default.Equals(attribute.Type?.OriginalDefinition, behaviorsAttributeSymbol))
if (!attribute.Type.IsBehaviorsAttribute())
return;

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

var compilation = context.Compilation;
var arrayTypeSymbol = compilation.CreateArrayTypeSymbol(compilation.GetTypeByMetadataName("System.Type")!, 1);
if (!SymbolEqualityComparer.Default.Equals(array.Type, arrayTypeSymbol)
|| array.ChildOperations.Count != 2
if (array is not
{
Type: IArrayTypeSymbol
{
ElementType:
{
Name: "Type",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
ChildOperations.Count: 2
}
|| array.ChildOperations.ElementAt(1) is not IArrayInitializerOperation aio)
{
// note: this will already be a compiler error anyway
Expand Down Expand Up @@ -117,7 +124,7 @@ private void AnalyzeOperation(OperationAnalysisContext context)
var location = toes.Type.GetLocation();
var originalDefinition = behaviorType.OriginalDefinition;

if (!ImplementsBaseClass(originalDefinition, baseBehaviorSymbol))
if (!originalDefinition.ImplementsBehavior())
{
context.ReportDiagnostic(
Diagnostic.Create(
Expand Down Expand Up @@ -148,10 +155,4 @@ private void AnalyzeOperation(OperationAnalysisContext context)
}
}
}

private static bool ImplementsBaseClass(INamedTypeSymbol typeSymbol, INamedTypeSymbol typeToCheck) =>
SymbolEqualityComparer.Default.Equals(typeSymbol, typeToCheck)
|| (typeSymbol.BaseType is not null
&& ImplementsBaseClass(typeSymbol.BaseType.OriginalDefinition, typeToCheck)
);
}
86 changes: 86 additions & 0 deletions src/Immediate.Handlers.Analyzers/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Microsoft.CodeAnalysis;

namespace Immediate.Handlers.Analyzers;

internal static class ITypeSymbolExtensions
{
public static bool IsBehaviorsAttribute(this ITypeSymbol? typeSymbol) =>
typeSymbol is
{
Name: "BehaviorsAttribute",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Handlers",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};

public static bool IsRenderModeAttribute(this ITypeSymbol? typeSymbol) =>
typeSymbol is
{
Name: "RenderModeAttribute",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Handlers",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};

public static bool IsRenderMode(this ITypeSymbol? typeSymbol) =>
typeSymbol is
{
Name: "RenderMode",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Handlers",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};

public static bool IsBehavior2(this ITypeSymbol typeSymbol) =>
typeSymbol is
{
MetadataName: "Behavior`2",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Handlers",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};

public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) =>
typeSymbol.IsBehavior2()
|| (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition));
}
8 changes: 2 additions & 6 deletions src/Immediate.Handlers.Analyzers/RenderModeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ private void AnalyzeOperation(OperationAnalysisContext context)
if (context.Operation is not IAttributeOperation { Operation: IObjectCreationOperation attribute })
return;

var renderModeAttributeSymbol = context.Compilation.GetTypeByMetadataName("Immediate.Handlers.Shared.RenderModeAttribute");

token.ThrowIfCancellationRequested();
if (!SymbolEqualityComparer.Default.Equals(attribute.Type?.OriginalDefinition, renderModeAttributeSymbol))
if (!attribute.Type.IsRenderModeAttribute())
return;

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

if (attribute.Arguments.Length != 1
|| attribute.Arguments[0].Value is not IFieldReferenceOperation value
|| !SymbolEqualityComparer.Default.Equals(value.Type?.OriginalDefinition, renderModeSymbol)
|| !value.Type.IsRenderMode()
|| value.Member.Name is "None")
{
context.ReportDiagnostic(Diagnostic.Create(
Expand Down
78 changes: 78 additions & 0 deletions src/Immediate.Handlers.Generators/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.CodeAnalysis;

namespace Immediate.Handlers.Generators;

internal static class ITypeSymbolExtensions
{
public static bool IsBehavior2(this ITypeSymbol typeSymbol) =>
typeSymbol is
{
MetadataName: "Behavior`2",
ContainingNamespace:
{
Name: "Shared",
ContainingNamespace:
{
Name: "Handlers",
ContainingNamespace:
{
Name: "Immediate",
ContainingNamespace.IsGlobalNamespace: true,
},
},
},
};

public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) =>
typeSymbol.IsBehavior2()
|| (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition));

public static bool IsValueTask1(this ITypeSymbol typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
MetadataName: "ValueTask`1",
ContainingNamespace:
{
Name: "Tasks",
ContainingNamespace:
{
Name: "Threading",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
}
}
}
};

public static bool IsValueTask(this ITypeSymbol typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
Name: "ValueTask",
ContainingNamespace:
{
Name: "Tasks",
ContainingNamespace:
{
Name: "Threading",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
}
}
}
};

public static bool IsIEquatable1(this ITypeSymbol typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
MetadataName: "IEquatable`1",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
<TargetFramework>netstandard2.0</TargetFramework>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<NoWarn>$(NoWarn);CA1716</NoWarn>
</PropertyGroup>

<ItemGroup>
<None Remove="Templates\*.sbntxt" />
<EmbeddedResource Include="Templates\*.sbntxt" />
<None Remove="Templates/*.sbntxt" />
<EmbeddedResource Include="Templates/*.sbntxt" />
</ItemGroup>

<ItemGroup>
<Compile Include="../Immediate.Handlers.Shared/RenderMode.cs" />
</ItemGroup>

<ItemGroup>
Expand All @@ -19,18 +24,13 @@
<PackageReference Include="MinVer" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Immediate.Handlers.Shared/Immediate.Handlers.Shared.csproj" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PkgScriban)/lib/netstandard2.0/Scriban.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(MSBuildThisFileDirectory)bin/$(Configuration)/$(TargetFramework)/Immediate.Handlers.Shared.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ string GetVariableNameSuffix(string typeName)

private static bool ValidateType(string? type, GenericType implementedTypes) =>
type is null
|| implementedTypes.Implements
.Contains(type);
|| implementedTypes.Implements.Contains(type);

private static Template GetTemplate(string name)
{
Expand Down
Loading

0 comments on commit fd5d13c

Please sign in to comment.