-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self review
// 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); | ||
|
There was a problem hiding this comment.
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
// 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - approve
// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch
Fixes #55
Changes
Converts
to