Skip to content

Commit

Permalink
Merge pull request microsoft#171 from Microsoft/dev/andarno/fix168
Browse files Browse the repository at this point in the history
Preserve leading trivia in VSTHRD103 code fix
  • Loading branch information
AArnott authored Jul 31, 2017
2 parents 43ea43b + 92825b0 commit a6e4c82
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,94 @@ internal static void Foo<T>() { }
this.VerifyCSharpFix(test, withFix);
}

[Fact]
public void AsyncAlternative_CodeFixRespectsTrivia()
{
var test = @"
using System;
using System.Threading.Tasks;
class Test {
void Foo() { }
Task FooAsync() => Task.CompletedTask;
async Task DoWorkAsync()
{
await Task.Yield();
Console.WriteLine(""Foo"");
// Some comment
Foo(/*argcomment*/); // another comment
}
}
";
var withFix = @"
using System;
using System.Threading.Tasks;
class Test {
void Foo() { }
Task FooAsync() => Task.CompletedTask;
async Task DoWorkAsync()
{
await Task.Yield();
Console.WriteLine(""Foo"");
// Some comment
await FooAsync(/*argcomment*/); // another comment
}
}
";
this.expect.Locations = new[] { new DiagnosticResultLocation("Test0.cs", 15, 9, 15, 12) };
this.VerifyCSharpDiagnostic(test, this.expect);
this.VerifyCSharpFix(test, withFix);
}

[Fact]
public void AwaitRatherThanWait_CodeFixRespectsTrivia()
{
var test = @"
using System;
using System.Threading.Tasks;
class Test {
void Foo() { }
Task FooAsync() => Task.CompletedTask;
async Task DoWorkAsync()
{
await Task.Yield();
Console.WriteLine(""Foo"");
// Some comment
FooAsync(/*argcomment*/).Wait(); // another comment
}
}
";
var withFix = @"
using System;
using System.Threading.Tasks;
class Test {
void Foo() { }
Task FooAsync() => Task.CompletedTask;
async Task DoWorkAsync()
{
await Task.Yield();
Console.WriteLine(""Foo"");
// Some comment
await FooAsync(/*argcomment*/); // another comment
}
}
";
this.expect.Locations = new[] { new DiagnosticResultLocation("Test0.cs", 15, 34, 15, 38) };
this.VerifyCSharpDiagnostic(test, this.expect);
this.VerifyCSharpFix(test, withFix);
}

[Fact]
public void XunitThrowAsyncNotSuggestedInAsyncTestMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ protected override async Task<Solution> GetChangedSolutionAsync(CancellationToke
if (this.AlternativeAsyncMethod != string.Empty)
{
// Replace the member being called and await the invocation expression.
// While doing so, move leading trivia to the surrounding await expression.
var asyncMethodName = syncMethodName.WithIdentifier(SyntaxFactory.Identifier(this.diagnostic.Properties[AsyncMethodKeyName]));
awaitExpression = SyntaxFactory.AwaitExpression(syncExpression.ReplaceNode(syncMethodName, asyncMethodName));
awaitExpression = SyntaxFactory.AwaitExpression(
syncExpression.ReplaceNode(syncMethodName, asyncMethodName).WithoutLeadingTrivia())
.WithLeadingTrivia(syncExpression.GetLeadingTrivia());
if (!(syncExpression.Parent is ExpressionStatementSyntax))
{
awaitExpression = SyntaxFactory.ParenthesizedExpression(awaitExpression)
Expand All @@ -156,7 +159,8 @@ protected override async Task<Solution> GetChangedSolutionAsync(CancellationToke
syncMemberStrippedExpression = expressionMethodCall.Expression;
}

awaitExpression = SyntaxFactory.AwaitExpression(syncMemberStrippedExpression);
awaitExpression = SyntaxFactory.AwaitExpression(syncMemberStrippedExpression.WithoutLeadingTrivia())
.WithLeadingTrivia(syncMemberStrippedExpression.GetLeadingTrivia());
}

updatedMethod = updatedMethod
Expand Down

0 comments on commit a6e4c82

Please sign in to comment.