Skip to content

Commit

Permalink
AK2001: harden so rule only runs for Akka.NET v1.5.15 and later (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb authored Jan 16, 2024
1 parent 389b40b commit 6118cae
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
5 changes: 1 addition & 4 deletions src/Akka.Analyzers.Tests/Akka.Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

<ItemGroup>
<!-- Download packages referenced by ReferenceAssembliesHelper -->
<!-- TODO: are transitive references downloaded? If so we could trim this list to just Akka.Cluster.Sharding -->
<PackageDownload Include="Akka" Version="[1.5.14]"/>
<PackageDownload Include="Akka.Cluster.Sharding" Version="[1.5.14]"/>
<PackageDownload Include="Akka.Cluster" Version="[1.5.14]"/>
<PackageDownload Include="Akka.Cluster.Sharding" Version="[1.5.15]"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ public async Task SuccessCase(string testCode)

[Theory]
[MemberData(nameof(FailureCases))]
public async Task FailureCase(
public Task FailureCase(
(string testCode, (int startLine, int startColumn, int endLine, int endColumn) spanData) d)
{
var expected = Verify.Diagnostic()
.WithSpan(d.spanData.startLine, d.spanData.startColumn, d.spanData.endLine, d.spanData.endColumn)
.WithArguments("Sender")
.WithSeverity(DiagnosticSeverity.Error);

await Verify.VerifyAnalyzer(d.testCode, expected).ConfigureAwait(true);
return Verify.VerifyAnalyzer(d.testCode, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public string ShardId(object message)
{
return Random.Shared.Next(0,10).ToString();
}
public string ShardId(string entityId, object message)
{
return Random.Shared.Next(0,10).ToString();
}
}
""", new[]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static ReferenceAssembliesHelper()

// TODO: does this bring all other transitive dependencies?
CurrentAkka = defaultAssemblies.AddPackages(
[new PackageIdentity("Akka", "1.5.14"), new PackageIdentity("Akka.Cluster.Sharding", "1.5.14")]
[new PackageIdentity("Akka.Cluster.Sharding", "1.5.15")]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ namespace Akka.Analyzers;
public class MustNotUseAutomaticallyHandledMessagesInsideMessageExtractorAnalyzer()
: AkkaDiagnosticAnalyzer(RuleDescriptors.Ak2001DoNotUseAutomaticallyHandledMessagesInShardMessageExtractor)
{
private static readonly Version RelevantVersion = new(1, 5, 15, 0);

protected override bool ShouldAnalyze(AkkaContext akkaContext)
{
Guard.AssertIsNotNull(akkaContext);

// Akka.Cluster.Sharding has to be installed and the version of it used has to be greater than or equal to v1.5.15
return akkaContext.HasAkkaClusterShardingInstalled && akkaContext.AkkaClusterSharding.Version >= RelevantVersion;
}

public override void AnalyzeCompilation(CompilationStartAnalysisContext context, AkkaContext akkaContext)
{
Guard.AssertIsNotNull(context);
Guard.AssertIsNotNull(akkaContext);

context.RegisterSyntaxNodeAction(ctx =>
{
if (akkaContext.HasAkkaClusterShardingInstalled == false)
return; // exit early if we don't have Akka.Cluster.Sharding installed

AnalyzeMethodDeclaration(ctx, akkaContext);
}, SyntaxKind.MethodDeclaration);

context.RegisterSyntaxNodeAction(ctx =>
{
if (akkaContext.HasAkkaClusterShardingInstalled == false)
return; // exit early if we don't have Akka.Cluster.Sharding installed

var invocationExpr = (InvocationExpressionSyntax)ctx.Node;
var semanticModel = ctx.SemanticModel;
if (semanticModel.GetSymbolInfo(invocationExpr).Symbol is not IMethodSymbol methodSymbol)
Expand Down

0 comments on commit 6118cae

Please sign in to comment.