Skip to content

Commit

Permalink
wip: trying to implement a semantic analyzer and code fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabganji committed Apr 12, 2024
1 parent a9ff06e commit 38ea979
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CodeJoyRide.Api/Customers/Services/CustomerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +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 @@ -2,6 +2,7 @@

### New Rules

| Rule ID | Category | Severity | Notes |
|---------|----------|----------|--------------------------------------------------|
| CJR01 | Usage | Warning | The speed must be lower than the Speed of Light. |

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
CJR01 | Usage | Error | CJR_01_The speed must be lower than the Speed of Light [Documentation](../../README.md)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### New Rules

| Rule ID | Category | Severity | Notes |
|---------|----------|----------|-------|
Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
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")))
.NormalizeWhitespace()
.WithLeadingTrivia(throwSyntaxNode.GetLeadingTrivia())
.WithTrailingTrivia(throwSyntaxNode.GetTrailingTrivia());

var currentRoot = await document.GetSyntaxRootAsync(token);
var newRoot = currentRoot!.ReplaceNode(throwSyntaxNode, returnStatement);

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

private void AnalyzeThrowStatements(OperationAnalysisContext context)
{
if (context.Operation is not IThrowOperation || context.Operation.Syntax is not ThrowStatementSyntax)
return;

if (context.Operation.SemanticModel is null)
return;

Expand Down

0 comments on commit 38ea979

Please sign in to comment.