Skip to content

Commit

Permalink
fixed IsActorBaseSubclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Dec 28, 2023
1 parent bc760bc commit cda2001
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ void Method()

[Theory]
[MemberData(nameof(SuccessCases))]
public async Task Analyzer_Should_Not_Report_Diagnostic_For_Valid_Usage(string testCode)
public async Task SuccessCase(string testCode)
{
await Verify.VerifyAnalyzer(testCode).ConfigureAwait(true);
}

[Fact]
public async Task Analyzer_Should_Report_Diagnostic_For_Invalid_Usage()
public async Task FailureCase()
{
var testCode = @"using Akka.Actor;
Expand All @@ -97,7 +97,7 @@ void Method()
}";

var expected = Verify.Diagnostic()
.WithSpan(13, 9, 13, 47)
.WithSpan(13, 33, 13, 46)
.WithArguments("MyActor");

await Verify.VerifyAnalyzer(testCode, expected).ConfigureAwait(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public override void AnalyzeCompilation(CompilationStartAnalysisContext context,
// Check if it's within the context of Props.Create
if (IsInsidePropsCreate(objectCreation))
return;

var diagnostic = Diagnostic.Create(RuleDescriptors.Ak1000DoNotNewActors, objectCreation.GetLocation(), typeSymbol.Name);
ctx.ReportDiagnostic(diagnostic);
}, SyntaxKind.ObjectCreationExpression);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Akka.Analyzers/Utility/AkkaDiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public sealed override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(ctx =>
{
var xunitContext = CreateAkkaContext(ctx.Compilation);
if (ShouldAnalyze(xunitContext))
AnalyzeCompilation(ctx, xunitContext);
var akkaContext = CreateAkkaContext(ctx.Compilation);
if (ShouldAnalyze(akkaContext))
AnalyzeCompilation(ctx, akkaContext);
});
}

Expand Down
28 changes: 0 additions & 28 deletions src/Akka.Analyzers/Utility/AnalysisCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// </copyright>
// -----------------------------------------------------------------------

using Microsoft.CodeAnalysis;

namespace Akka.Analyzers;

public enum AnalysisCategory
Expand All @@ -14,30 +12,4 @@ public enum AnalysisCategory
/// 1xxx
/// </summary>
ActorDesign,
}

public static class RuleDescriptors
{
private static DiagnosticDescriptor Rule(
string id,
string title,
AnalysisCategory category,
DiagnosticSeverity defaultSeverity,
string messageFormat)
{
// TODO: need real help links for each rule code
var helpLink = $"https://getakka.net/";

return new DiagnosticDescriptor(id, title, messageFormat, category.ToString(), defaultSeverity,
isEnabledByDefault: true, helpLinkUri: helpLink);
}

public static DiagnosticDescriptor Ak1000DoNotNewActors { get; } = Rule("Akka1000",
"Do not use `new` to create actors", AnalysisCategory.ActorDesign, DiagnosticSeverity.Error,
"Actors must be instantiated using `ActorOf` or `ActorOfAsTestActorRef` via a `Props` class.");

public static DiagnosticDescriptor Ak1001CloseOverSenderUsingPipeTo { get; } = Rule("Akka1002",
"Should always close over `Sender` when using `PipeTo`", AnalysisCategory.ActorDesign, DiagnosticSeverity.Error,
"When using `PipeTo`, you must always close over `Sender` to ensure that the actor's `Sender` property " +
"is captured at the time you're scheduling the `PipeTo`, as this value may change asynchronously.");
}
12 changes: 11 additions & 1 deletion src/Akka.Analyzers/Utility/CodeAnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public static bool IsActorBaseSubclass(this INamedTypeSymbol typeSymbol, AkkaCon
if(akkaContext.AkkaCore.ActorBaseType is null)
return false;

return SymbolEqualityComparer.Default.Equals(typeSymbol, akkaContext.AkkaCore.ActorBaseType);
var currentBaseType = typeSymbol.BaseType;
while (currentBaseType != null)
{
if (SymbolEqualityComparer.Default.Equals(currentBaseType, akkaContext.AkkaCore.ActorBaseType))
{
return true;
}
currentBaseType = currentBaseType.BaseType;
}

return false;
}
}
29 changes: 29 additions & 0 deletions src/Akka.Analyzers/Utility/RuleDescriptors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.CodeAnalysis;

namespace Akka.Analyzers;

public static class RuleDescriptors
{
private static DiagnosticDescriptor Rule(
string id,
string title,
AnalysisCategory category,
DiagnosticSeverity defaultSeverity,
string messageFormat)
{
// TODO: need real help links for each rule code
var helpLink = $"https://getakka.net/";

return new DiagnosticDescriptor(id, title, messageFormat, category.ToString(), defaultSeverity,
isEnabledByDefault: true, helpLinkUri: helpLink);
}

public static DiagnosticDescriptor Ak1000DoNotNewActors { get; } = Rule("Akka1000",
"Do not use `new` to create actors", AnalysisCategory.ActorDesign, DiagnosticSeverity.Error,
"Actors must be instantiated using `ActorOf` or `ActorOfAsTestActorRef` via a `Props` class.");

public static DiagnosticDescriptor Ak1001CloseOverSenderUsingPipeTo { get; } = Rule("Akka1002",
"Should always close over `Sender` when using `PipeTo`", AnalysisCategory.ActorDesign, DiagnosticSeverity.Error,
"When using `PipeTo`, you must always close over `Sender` to ensure that the actor's `Sender` property " +
"is captured at the time you're scheduling the `PipeTo`, as this value may change asynchronously.");
}

0 comments on commit cda2001

Please sign in to comment.