Skip to content

Commit

Permalink
Fix AK1001 code fix for one liner lambda expression (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Jan 17, 2024
1 parent e601f33 commit d053368
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ private static async Task<Document> UseLocalVariableForSenderAsync(Document docu
SyntaxFactory.IdentifierName("Sender")))
.WithAdditionalAnnotations(Formatter.Annotation); // need this line to get indentation right

// Find an appropriate insertion point for the local variable
var insertionPoint = invocationExpr.FirstAncestorOrSelf<StatementSyntax>();

if (insertionPoint == null)
// Unable to find a valid insertion point
return document;

// Insert the local variable declaration at the found insertion point
editor.InsertBefore(insertionPoint, senderVariable);

// Identify the 'recipient' argument - assuming it's the first argument
var arguments = invocationExpr.ArgumentList.Arguments;

Expand All @@ -93,8 +83,40 @@ private static async Task<Document> UseLocalVariableForSenderAsync(Document docu
var newArgumentList = SyntaxFactory.ArgumentList(newArguments);
var newInvocationExpr = invocationExpr.WithArgumentList(newArgumentList);

// Make sure to replace the old invocation with the new one
editor.ReplaceNode(invocationExpr, newInvocationExpr);
// Check to see if we're inside a lambda expression and it does not have a body block
var lambdaExpr = invocationExpr.FirstAncestorOrSelf<LambdaExpressionSyntax>();
if (lambdaExpr?.Body is ExpressionSyntax exprSyntax)
{
// Make sure to replace the old invocation with the new one
var newExprSyntax = exprSyntax.ReplaceNode(invocationExpr, newInvocationExpr);

// Create a new block with the original lambda body as a statement
var originalBody = SyntaxFactory.ExpressionStatement(newExprSyntax);

// Insert the local variable declaration at the start of the lambda block
var bodyBlock = SyntaxFactory.Block(
senderVariable.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed),
originalBody.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed));

// Replace the original with the new one
var newLambdaExpr = lambdaExpr.WithBody(null).WithBlock(bodyBlock);
editor.ReplaceNode(lambdaExpr, newLambdaExpr);
}
else
{
// Find an appropriate insertion point for the local variable
var insertionPoint = invocationExpr.FirstAncestorOrSelf<StatementSyntax>();

if (insertionPoint == null)
// Unable to find a valid insertion point
return document;

// Insert the local variable declaration at the found insertion point
editor.InsertBefore(insertionPoint, senderVariable);

// Make sure to replace the old invocation with the new one
editor.ReplaceNode(invocationExpr, newInvocationExpr);
}

var newDocument = editor.GetChangedDocument(); // error happens here

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,59 @@ async Task<int> LocalFunction(){
expectedDiagnostic);
}

[Fact]
public Task AddClosureInsideOneLinerReceiveMethod()
{
var before =
"""
using Akka.Actor;
using System.Threading.Tasks;
public sealed class MyActor : ReceiveActor
{
public MyActor()
{
Receive<string>(str => MessageHandler(str).PipeTo(Sender));
}
private Task<int> MessageHandler(string str)
{
return Task.FromResult(str.Length);
}
}
""";

var after =
"""
using Akka.Actor;
using System.Threading.Tasks;
public sealed class MyActor : ReceiveActor
{
public MyActor()
{
Receive<string>(str =>
{
var sender = this.Sender;
MessageHandler(str).PipeTo(sender);
});
}
private Task<int> MessageHandler(string str)
{
return Task.FromResult(str.Length);
}
}
""";

var expectedDiagnostic = Verify.Diagnostic()
.WithSpan(8, 52, 8, 58)
.WithArguments("Sender");

return Verify.VerifyCodeFix(before, after, MustCloseOverSenderWhenUsingPipeToFixer.Key_FixPipeToSender,
expectedDiagnostic);
}

[Fact]
public Task AddClosureInsideUntypedActor()
{
Expand Down

0 comments on commit d053368

Please sign in to comment.