Skip to content

Commit

Permalink
feat: implemented the analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabganji committed Apr 25, 2024
1 parent 2a1142b commit 22cb9ca
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 1 addition & 2 deletions CodeJoyRide.Api/Customers/Services/CustomerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public Maybe<Customer> Find(Guid customerId)
{
var valueExists = Customers.TryGetValue(customerId, out var customer);
if (!valueExists)
throw new CustomerNotFoundException(customerId);

return CodeJoyRide.Fx.Maybe.None;
return Maybe.Some(customer!);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace CodeJoyRide.Fx.Analyzer;

Expand Down Expand Up @@ -52,9 +53,27 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
), diagnostic);
}

private Task<Document> ReplaceThrowWithReturnStatement(
private async Task<Document> ReplaceThrowWithReturnStatement(
Document document, CSharpSyntaxNode throwSyntaxNode, CancellationToken token)
{
throw new NotImplementedException();
var returnStatement = ReturnStatement(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("CodeJoyRide"),
IdentifierName("Fx")),
IdentifierName("Maybe")),
IdentifierName("None")
.WithLeadingTrivia(throwSyntaxNode.GetLeadingTrivia())
.WithTrailingTrivia(throwSyntaxNode.GetTrailingTrivia())
.NormalizeWhitespace()));

var root = await document.GetSyntaxRootAsync(token);
var newRoot = root?.ReplaceNode(throwSyntaxNode, returnStatement);

return document.WithSyntaxRoot(newRoot!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ public override void Initialize(AnalysisContext context)

private void AnalyzeThrowStatements(OperationAnalysisContext context)
{
// TODO: Implement the Analyzer
if (context.Operation is not IThrowOperation || context.Operation.Syntax is not ThrowStatementSyntax)
return;

var containingMethodSyntax = GetContainingMethodSyntax(context.Operation.Syntax);
var containingMethodSymbol =
context.Operation.SemanticModel.GetDeclaredSymbol(containingMethodSyntax) as IMethodSymbol;

var returnSymbol = containingMethodSymbol!.ReturnType;
var maybeTypeSymbol = context.Compilation.GetTypeByMetadataName("CodeJoyRide.Fx.Maybe`1");

if(!returnSymbol.OriginalDefinition.Equals(maybeTypeSymbol, SymbolEqualityComparer.Default))
return;

var diagnostic = Diagnostic.Create(Rule, context.Operation.Syntax.GetLocation());
context.ReportDiagnostic(diagnostic);
}

private MethodDeclarationSyntax GetContainingMethodSyntax(SyntaxNode operationSyntax)
{
while (true)
{
if (operationSyntax.Parent is MethodDeclarationSyntax mds) return mds;
operationSyntax = operationSyntax.Parent!;
}
}
}

0 comments on commit 22cb9ca

Please sign in to comment.