Skip to content

Commit

Permalink
have analyzer syntax working
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Dec 27, 2023
1 parent 3bff133 commit 927ddbb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
50 changes: 41 additions & 9 deletions src/Akka.Analyzers/AK1000/MustNotUseNewKeywordOnActors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,51 @@ public override void AnalyzeCompilation(CompilationStartAnalysisContext context,
{
Guard.AssertIsNotNull(context);
Guard.AssertIsNotNull(akkaContext);

context.RegisterSyntaxNodeAction(ctx =>
{
var objectCreation = (ObjectCreationExpressionSyntax) ctx.Node;

var typeSymbol = ModelExtensions.GetTypeInfo(ctx.SemanticModel, objectCreation).Type;
if (typeSymbol is null)
var objectCreation = (ObjectCreationExpressionSyntax)ctx.Node;

if (ModelExtensions.GetTypeInfo(ctx.SemanticModel, objectCreation).Type is not INamedTypeSymbol typeSymbol)
return;

// Check if it's a subclass of ActorBase
if (!typeSymbol.IsActorBaseSubclass(akkaContext))
return;

if (typeSymbol.IsActorType(akkaContext.AkkaCore))

// Check if it's within the context of Props.Create
if (IsInsidePropsCreate(objectCreation))
return;
}, SyntaxKind.ObjectCreationExpression);
}

private static bool IsInsidePropsCreate(ObjectCreationExpressionSyntax objectCreation)
{
// Traverse upwards in the syntax tree from the object creation expression
var currentNode = objectCreation.Parent;

while (currentNode != null)
{
// Check if the current node is an ArgumentSyntax, which could be a part of a method call
if (currentNode is ArgumentSyntax { Parent.Parent: InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax
{
Name.Identifier.ValueText: "Create", Expression: IdentifierNameSyntax
{
Identifier.ValueText: "Props"
}
}
}
})
// Get the parent InvocationExpressionSyntax, if any
// Check if the method being called is 'Props.Create'
{
ctx.ReportDiagnostic(Diagnostic.Create(RuleDescriptors.Ak1000DoNotNewActors, objectCreation.GetLocation()));
return true;
}
}, SyntaxKind.ObjectCreationExpression);

// Move to the next parent node
currentNode = currentNode.Parent;
}

return false;
}
}
11 changes: 8 additions & 3 deletions src/Akka.Analyzers/Utility/CodeAnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ internal static class CodeAnalysisExtensions
// return false;
// }

private bool IsActorBaseSubclass(this INamedTypeSymbol typeSymbol, AkkaContext akkaContext)
public static bool IsActorBaseSubclass(this INamedTypeSymbol typeSymbol, AkkaContext akkaContext)
{
var actorBaseType = context.SemanticModel.Compilation.GetTypeByMetadataName("Akka.Actor.ActorBase");
return typeSymbol.BaseType.Equals(actorBaseType, SymbolEqualityComparer.Default);
Guard.AssertIsNotNull(typeSymbol);
Guard.AssertIsNotNull(akkaContext);

if(akkaContext.AkkaCore.ActorBaseType is null)
return false;

return SymbolEqualityComparer.Default.Equals(typeSymbol, akkaContext.AkkaCore.ActorBaseType);
}
}

0 comments on commit 927ddbb

Please sign in to comment.