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 'invert if' refactor to properly enclose #region/#endregion blocks #74145

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Features/CSharpTest/InvertIf/InvertIfTests.Elseless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,38 @@ void M()
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/73917")]
public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody4()
{
await TestAsync("""
public void SomeMethod()
{
object something = null;

[||]if (something == null)
{
return;
}

#region A region
something = new object();
#endregion
}
""", """
public void SomeMethod()
{
object something = null;

if (something != null)
{
#region A region
something = new object();
#endregion
}
}
""");
}
Copy link
Member

Choose a reason for hiding this comment

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

can you have a test where the parent scope is a case block in a switch?

also a test where we're inverting an if-statement as a top level statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the switch case test 😃.

I also checked for the top level if-statement but the refactor is not suggested. Am i missing something?


[Fact]
public async Task IfWithoutElse_SwapIfBodyWithSubsequentStatements1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,12 @@ private SyntaxNode GetRootWithInvertIfStatement(
var statementsAfterIf = statements.Skip(index + 1);
var ifBody = GetIfBody(ifNode);

var currentParentClosingBrace = currentParent.ChildTokens().Last();
var updatedLastStatement = statementsAfterIf.Last().WithTrailingTrivia(currentParentClosingBrace.LeadingTrivia);

statementsAfterIf = statementsAfterIf.Take(statementsAfterIf.Count() - 1);
statementsAfterIf = statementsAfterIf.Append(updatedLastStatement);
GiovanniBraconi marked this conversation as resolved.
Show resolved Hide resolved

var updatedIf = UpdateIf(
text,
ifNode: ifNode,
Expand All @@ -580,6 +586,9 @@ private SyntaxNode GetRootWithInvertIfStatement(
currentParent,
statementsBeforeIf.Concat(updatedIf));

var updatedParentClosingBrace = updatedParent.ChildTokens().Last();
updatedParent = updatedParent.ReplaceToken(updatedParentClosingBrace, updatedParentClosingBrace.WithoutLeadingTrivia());

return root.ReplaceNode(currentParent, updatedParent.WithAdditionalAnnotations(Formatter.Annotation));
}

Expand Down
Loading