Skip to content

Commit

Permalink
Merge pull request DotNetAnalyzers#65 from sharwell/configureawait-true
Browse files Browse the repository at this point in the history
Add ConfigureAwait(true) as a second fix for UseConfigureAwaitCodeFixProvider
  • Loading branch information
sharwell authored Jul 27, 2017
2 parents aa124aa + 4260a4c commit 68e64c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ namespace AsyncUsageAnalyzers.Usage
[Shared]
internal class UseConfigureAwaitCodeFixProvider : CodeFixProvider
{
private const string UseConfigureAwaitFalseEquivalenceId = nameof(UseConfigureAwaitCodeFixProvider) + "_False";
private const string UseConfigureAwaitTrueEquivalenceId = nameof(UseConfigureAwaitCodeFixProvider) + "_True";

private static readonly ImmutableArray<string> FixableDiagnostics =
ImmutableArray.Create(UseConfigureAwaitAnalyzer.DiagnosticId);

Expand All @@ -47,15 +50,21 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
"Use ConfigureAwait(false)",
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
nameof(UseConfigureAwaitCodeFixProvider) + "_False"),
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, SyntaxKind.FalseLiteralExpression, cancellationToken),
UseConfigureAwaitFalseEquivalenceId),
diagnostic);
context.RegisterCodeFix(
CodeAction.Create(
"Use ConfigureAwait(true)",
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, SyntaxKind.TrueLiteralExpression, cancellationToken),
UseConfigureAwaitTrueEquivalenceId),
diagnostic);
}

return Task.FromResult(true);
}

private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxKind literalKind, CancellationToken cancellationToken)
{
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
ExpressionSyntax expression = (ExpressionSyntax)root.FindNode(diagnostic.Location.SourceSpan);
Expand All @@ -67,7 +76,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
SyntaxFactory.ArgumentList(
SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Argument(
SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)))))
SyntaxFactory.LiteralExpression(literalKind)))))
.WithAdditionalAnnotations(Formatter.Annotation);

SyntaxNode newRoot = root.ReplaceNode(expression, newExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace AsyncUsageAnalyzers.Test.Usage

public class UseConfigureAwaitUnitTests : CodeFixVerifier
{
[Fact]
public async Task TestSimpleExpressionAsync()
[Theory]
[InlineData(0, "false")]
[InlineData(1, "true")]
public async Task TestSimpleExpressionAsync(int codeFixIndex, string configureAwaitArgument)
{
string testCode = @"
using System.Threading.Tasks;
Expand All @@ -27,21 +29,21 @@ async Task MethodNameAsync()
}
}
";
string fixedCode = @"
string fixedCode = $@"
using System.Threading.Tasks;
class ClassName
{
{{
async Task MethodNameAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
}
}
{{
await Task.Delay(1000).ConfigureAwait({configureAwaitArgument});
}}
}}
";

DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 15);
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, codeFixIndex: codeFixIndex, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

[Fact]
Expand All @@ -61,8 +63,10 @@ async Task MethodNameAsync()
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestNestedExpressionsAsync()
[Theory]
[InlineData(0, "false")]
[InlineData(1, "true")]
public async Task TestNestedExpressionsAsync(int codeFixIndex, string configureAwaitArgument)
{
string testCode = @"
using System.Threading.Tasks;
Expand All @@ -79,20 +83,20 @@ async Task MethodNameAsync()
}
}
";
string fixedCode = @"
string fixedCode = $@"
using System.Threading.Tasks;
class ClassName
{
{{
async Task<Task> FirstMethodAsync()
{
{{
return Task.FromResult(true);
}
}}
async Task MethodNameAsync()
{
await (await FirstMethodAsync().ConfigureAwait(false)).ConfigureAwait(false);
}
}
{{
await (await FirstMethodAsync().ConfigureAwait({configureAwaitArgument})).ConfigureAwait({configureAwaitArgument});
}}
}}
";

DiagnosticResult[] expected =
Expand All @@ -102,7 +106,7 @@ async Task MethodNameAsync()
};
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedCode, codeFixIndex: codeFixIndex, cancellationToken: CancellationToken.None).ConfigureAwait(false);
}

protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
Expand Down

0 comments on commit 68e64c0

Please sign in to comment.