Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Commit 68e64c0

Browse files
authored
Merge pull request #65 from sharwell/configureawait-true
Add ConfigureAwait(true) as a second fix for UseConfigureAwaitCodeFixProvider
2 parents aa124aa + 4260a4c commit 68e64c0

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

AsyncUsageAnalyzers/AsyncUsageAnalyzers.CodeFixes/Usage/UseConfigureAwaitCodeFixProvider.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ namespace AsyncUsageAnalyzers.Usage
2222
[Shared]
2323
internal class UseConfigureAwaitCodeFixProvider : CodeFixProvider
2424
{
25+
private const string UseConfigureAwaitFalseEquivalenceId = nameof(UseConfigureAwaitCodeFixProvider) + "_False";
26+
private const string UseConfigureAwaitTrueEquivalenceId = nameof(UseConfigureAwaitCodeFixProvider) + "_True";
27+
2528
private static readonly ImmutableArray<string> FixableDiagnostics =
2629
ImmutableArray.Create(UseConfigureAwaitAnalyzer.DiagnosticId);
2730

@@ -47,15 +50,21 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4750
context.RegisterCodeFix(
4851
CodeAction.Create(
4952
"Use ConfigureAwait(false)",
50-
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
51-
nameof(UseConfigureAwaitCodeFixProvider) + "_False"),
53+
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, SyntaxKind.FalseLiteralExpression, cancellationToken),
54+
UseConfigureAwaitFalseEquivalenceId),
55+
diagnostic);
56+
context.RegisterCodeFix(
57+
CodeAction.Create(
58+
"Use ConfigureAwait(true)",
59+
cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, SyntaxKind.TrueLiteralExpression, cancellationToken),
60+
UseConfigureAwaitTrueEquivalenceId),
5261
diagnostic);
5362
}
5463

5564
return Task.FromResult(true);
5665
}
5766

58-
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
67+
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxKind literalKind, CancellationToken cancellationToken)
5968
{
6069
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
6170
ExpressionSyntax expression = (ExpressionSyntax)root.FindNode(diagnostic.Location.SourceSpan);
@@ -67,7 +76,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6776
SyntaxFactory.ArgumentList(
6877
SyntaxFactory.SingletonSeparatedList(
6978
SyntaxFactory.Argument(
70-
SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)))))
79+
SyntaxFactory.LiteralExpression(literalKind)))))
7180
.WithAdditionalAnnotations(Formatter.Annotation);
7281

7382
SyntaxNode newRoot = root.ReplaceNode(expression, newExpression);

AsyncUsageAnalyzers/AsyncUsageAnalyzers.Test/Usage/UseConfigureAwaitUnitTests.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ namespace AsyncUsageAnalyzers.Test.Usage
1414

1515
public class UseConfigureAwaitUnitTests : CodeFixVerifier
1616
{
17-
[Fact]
18-
public async Task TestSimpleExpressionAsync()
17+
[Theory]
18+
[InlineData(0, "false")]
19+
[InlineData(1, "true")]
20+
public async Task TestSimpleExpressionAsync(int codeFixIndex, string configureAwaitArgument)
1921
{
2022
string testCode = @"
2123
using System.Threading.Tasks;
@@ -27,21 +29,21 @@ async Task MethodNameAsync()
2729
}
2830
}
2931
";
30-
string fixedCode = @"
32+
string fixedCode = $@"
3133
using System.Threading.Tasks;
3234
class ClassName
33-
{
35+
{{
3436
async Task MethodNameAsync()
35-
{
36-
await Task.Delay(1000).ConfigureAwait(false);
37-
}
38-
}
37+
{{
38+
await Task.Delay(1000).ConfigureAwait({configureAwaitArgument});
39+
}}
40+
}}
3941
";
4042

4143
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(7, 15);
4244
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
4345
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
46+
await this.VerifyCSharpFixAsync(testCode, fixedCode, codeFixIndex: codeFixIndex, cancellationToken: CancellationToken.None).ConfigureAwait(false);
4547
}
4648

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

64-
[Fact]
65-
public async Task TestNestedExpressionsAsync()
66+
[Theory]
67+
[InlineData(0, "false")]
68+
[InlineData(1, "true")]
69+
public async Task TestNestedExpressionsAsync(int codeFixIndex, string configureAwaitArgument)
6670
{
6771
string testCode = @"
6872
using System.Threading.Tasks;
@@ -79,20 +83,20 @@ async Task MethodNameAsync()
7983
}
8084
}
8185
";
82-
string fixedCode = @"
86+
string fixedCode = $@"
8387
using System.Threading.Tasks;
8488
class ClassName
85-
{
89+
{{
8690
async Task<Task> FirstMethodAsync()
87-
{
91+
{{
8892
return Task.FromResult(true);
89-
}
93+
}}
9094
9195
async Task MethodNameAsync()
92-
{
93-
await (await FirstMethodAsync().ConfigureAwait(false)).ConfigureAwait(false);
94-
}
95-
}
96+
{{
97+
await (await FirstMethodAsync().ConfigureAwait({configureAwaitArgument})).ConfigureAwait({configureAwaitArgument});
98+
}}
99+
}}
96100
";
97101

98102
DiagnosticResult[] expected =
@@ -102,7 +106,7 @@ async Task MethodNameAsync()
102106
};
103107
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
104108
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
105-
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
109+
await this.VerifyCSharpFixAsync(testCode, fixedCode, codeFixIndex: codeFixIndex, cancellationToken: CancellationToken.None).ConfigureAwait(false);
106110
}
107111

108112
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()

0 commit comments

Comments
 (0)