Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AK1001 code fix for lambda expression with no block body #56

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

Arkatufus
Copy link
Contributor

@Arkatufus Arkatufus commented Jan 17, 2024

Fixes #55

Changes

  • Check to see if the original invocation expression was inside a lambda expression with no block body
  • Convert the lamda expression body to a block before inserting the new sender var

Converts

Receive<string>(str => MessageHandler(str).PipeTo(Sender));

to

Receive<string>(str =>
{
    var sender = this.Sender;
    MessageHandler(str).PipeTo(sender);
});

Copy link
Contributor Author

@Arkatufus Arkatufus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self review

Comment on lines -63 to -72
// 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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to reshuffle this code to the end

Comment on lines +90 to +103
// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insert a block body if the original invocation was not enclosed inside a code block

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch

Copy link
Member

@Aaronontheweb Aaronontheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - approve

Comment on lines +90 to +103
// 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch

@Aaronontheweb Aaronontheweb merged commit d053368 into akkadotnet:dev Jan 17, 2024
2 checks passed
@Arkatufus Arkatufus deleted the AK1001-OneLiner branch January 25, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AK1001 - giving wrong code fix and warning? when using with ReceiveAsync?
2 participants