Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LipatovAlexander committed Jul 9, 2024
1 parent d958f63 commit 5bce4ed
Showing 1 changed file with 68 additions and 37 deletions.
105 changes: 68 additions & 37 deletions Mindbox.Analyzers/Rules/GroupByEntityRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,55 +35,86 @@ public void AnalyzeModel(SemanticModel model, out ICollection<Diagnostic> foundP

foreach (var invocationExpression in invocationExpressions)
{
if (invocationExpression.Expression is not MemberAccessExpressionSyntax memberAccessExpr)
if (!TryGetMethodSymbol(model, invocationExpression, out var methodSymbol)
|| !IsGroupByMethod(methodSymbol)
|| !TryGetGroupByProperty(model, invocationExpression, out var groupByProperty)
|| !HasTableAttribute(groupByProperty.Type))
{
continue;
}

if (model.GetSymbolInfo(memberAccessExpr).Symbol is not IMethodSymbol memberSymbol
|| memberSymbol.ContainingNamespace.ToString() != "System.Linq"
|| memberSymbol.ContainingType.Name != "Queryable"
|| memberSymbol.Name != "GroupBy")
{
continue;
}
var diagnostic = CreateDiagnosticForLocation(invocationExpression.GetLocation());
foundProblems.Add(diagnostic);
}
}

var argumentList = invocationExpression.ArgumentList.Arguments;
if (argumentList.Count == 0)
{
continue;
}
private static bool HasTableAttribute(ITypeSymbol typeSymbol)
{
var attributes = typeSymbol.GetAttributes();

if (argumentList[0].Expression is not SimpleLambdaExpressionSyntax lambdaExpr)
{
continue;
}
var attributeData = attributes.FirstOrDefault(x =>
x.AttributeClass is not null
&& x.AttributeClass.ContainingNamespace.ToString() == "System.ComponentModel.DataAnnotations.Schema"
&& x.AttributeClass.Name == "TableAttribute");

if (lambdaExpr.Body is not MemberAccessExpressionSyntax groupByProperty)
{
continue;
}
return attributeData is not null;
}

if (model.GetSymbolInfo(groupByProperty).Symbol is not IPropertySymbol propertySymbol)
{
continue;
}
private static bool TryGetGroupByProperty(
SemanticModel model,
InvocationExpressionSyntax invocationExpression,
out IPropertySymbol groupByProperty)
{
groupByProperty = null;

var argumentList = invocationExpression.ArgumentList.Arguments;
if (argumentList.Count == 0)
{
return false;
}

var propertyTypeSymbol = propertySymbol.Type;
var attributes = propertyTypeSymbol.GetAttributes();
if (argumentList[0].Expression is not SimpleLambdaExpressionSyntax lambdaExpr)
{
return false;
}

var tableAttribute = attributes.FirstOrDefault(x =>
x.AttributeClass is not null
&& x.AttributeClass.ContainingNamespace.ToString() == "System.ComponentModel.DataAnnotations.Schema"
&& x.AttributeClass.Name == "TableAttribute");
if (lambdaExpr.Body is not MemberAccessExpressionSyntax memberAccessExpression)
{
return false;
}

if (tableAttribute is null)
{
continue;
}
if (model.GetSymbolInfo(memberAccessExpression).Symbol is not IPropertySymbol propertySymbol)
{
return false;
}

var diagnostic = CreateDiagnosticForLocation(groupByProperty.GetLocation());
foundProblems.Add(diagnostic);
groupByProperty = propertySymbol;
return true;
}

private static bool IsGroupByMethod(IMethodSymbol methodSymbol)
{
return
methodSymbol.ContainingNamespace.ToString() == "System.Linq"
&& methodSymbol.ContainingType.Name == "Queryable"
&& methodSymbol.Name == "GroupBy";
}

private static bool TryGetMethodSymbol(SemanticModel model, InvocationExpressionSyntax invocationExpression, out IMethodSymbol methodSymbol)
{
methodSymbol = null;

if (invocationExpression.Expression is not MemberAccessExpressionSyntax memberAccessExpr)
{
return false;
}

if (model.GetSymbolInfo(memberAccessExpr).Symbol is not IMethodSymbol memberSymbol)
{
return false;
}

methodSymbol = memberSymbol;
return true;
}
}

0 comments on commit 5bce4ed

Please sign in to comment.