Skip to content

Commit

Permalink
Improve trivia handling. Not fully correct yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jan 9, 2025
1 parent 8c2d825 commit 7c0c9f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ private static async Task<Document> WrapLastStatementWithAssertThrowsExceptionAs

expressionToUseInLambda = expressionToUseInLambda switch
{
ThrowStatementSyntax throwStatement => generator.ThrowExpression(throwStatement.Expression),
ThrowStatementSyntax { Expression: not null } throwStatement => generator.ThrowExpression(throwStatement.Expression),
// This is the case when the last statement of the method body is a loop for example (e.g, for, foreach, while, do while).
// It can also happen for using statement, or switch statement.
// In that case, we need to wrap in a block syntax (i.e, curly braces)
StatementSyntax expressionToUseAsStatement => SyntaxFactory.Block(expressionToUseAsStatement),
_ => expressionToUseInLambda,
_ => expressionToUseInLambda.WithoutTrivia(),
};

SyntaxNode newLambdaExpression = generator.VoidReturningLambdaExpression(expressionToUseInLambda);
Expand Down Expand Up @@ -211,7 +211,7 @@ private static async Task<Document> WrapLastStatementWithAssertThrowsExceptionAs
newStatement = generator.ExpressionStatement(newStatement);
}

editor.ReplaceNode(expressionAndNodeToReplace.Value.NodeToReplace, newStatement);
editor.ReplaceNode(expressionAndNodeToReplace.Value.NodeToReplace, newStatement.WithTriviaFrom(expressionAndNodeToReplace.Value.NodeToReplace));
return editor.GetChangedDocument();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,10 @@ public class TestClass
public async Task TestMethod()
{
Console.WriteLine("Hello, world!");
await Assert.ThrowsExactlyAsync<Exception>(async () =>
// In ideal world, it's best if the codefix can separate await M() to a
// variable, then only wrap M(someVariable) in Assert.ThrowsException
// Let's also have this comment serve as a test for trivia ;)
M(await M()));
// In ideal world, it's best if the codefix can separate await M() to a
// variable, then only wrap M(someVariable) in Assert.ThrowsException
// Let's also have this comment serve as a test for trivia ;)
await Assert.ThrowsExactlyAsync<Exception>(async () => M(await M()));
}
private static Task<int> M() => Task.FromResult(0);
Expand Down Expand Up @@ -488,7 +487,7 @@ void M2() { }
}
}
""";
// TODO: Trivia be preserved in the fixed code.

string fixedCode = """
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -501,6 +500,7 @@ public void TestMethod()
{
M1();
Assert.ThrowsExactly<Exception>(() => M2());
void M1() { }
void M2() { }
}
Expand Down Expand Up @@ -685,8 +685,7 @@ public class TestClass
[TestMethod]
public void TestMethod()
{
Assert.ThrowsExactly<Exception>(() => Console.WriteLine());
;
Assert.ThrowsExactly<Exception>(() => Console.WriteLine()); ;
}
}
""";
Expand Down Expand Up @@ -733,7 +732,7 @@ public void TestMethod()
{
Console.WriteLine(x);
}
});
});
}
}
""";
Expand Down

0 comments on commit 7c0c9f9

Please sign in to comment.