forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVSTHRD002UseJtfRunCodeFixWithAwait.cs
More file actions
145 lines (129 loc) · 7.45 KB
/
Copy pathVSTHRD002UseJtfRunCodeFixWithAwait.cs
File metadata and controls
145 lines (129 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.Threading.Analyzers
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.VisualStudio.Threading;
[ExportCodeFixProvider(LanguageNames.CSharp)]
public class VSTHRD002UseJtfRunCodeFixWithAwait : CodeFixProvider
{
private static readonly ImmutableArray<string> ReusableFixableDiagnosticIds = ImmutableArray.Create(
VSTHRD002UseJtfRunAnalyzer.Id);
public override ImmutableArray<string> FixableDiagnosticIds => ReusableFixableDiagnosticIds;
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
Diagnostic? diagnostic = context.Diagnostics.First();
SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
if (TryFindNodeAtSource(diagnostic, root, out _, out _))
{
context.RegisterCodeFix(
CodeAction.Create(
Strings.VSTHRD002_CodeFix_Await_Title,
async ct =>
{
Document? document = context.Document;
if (TryFindNodeAtSource(diagnostic, root, out ExpressionSyntax? node, out Func<ExpressionSyntax, CancellationToken, ExpressionSyntax>? transform))
{
(document, node, _) = await FixUtils.UpdateDocumentAsync(
document,
node,
n => SyntaxFactory.AwaitExpression(transform(n, ct)),
ct).ConfigureAwait(false);
MethodDeclarationSyntax? method = node.FirstAncestorOrSelf<MethodDeclarationSyntax>();
if (method is object)
{
(document, method) = await FixUtils.MakeMethodAsync(method, document, ct).ConfigureAwait(false);
}
}
return document.Project.Solution;
},
"only action"),
diagnostic);
}
}
/// <inheritdoc />
public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;
private static bool TryFindNodeAtSource(Diagnostic diagnostic, SyntaxNode root, [NotNullWhen(true)] out ExpressionSyntax? target, [NotNullWhen(true)] out Func<ExpressionSyntax, CancellationToken, ExpressionSyntax>? transform)
{
transform = null;
target = null;
var syntaxNode = (ExpressionSyntax)root.FindNode(diagnostic.Location.SourceSpan);
if (syntaxNode.FirstAncestorOrSelf<AnonymousFunctionExpressionSyntax>() is object)
{
// We don't support converting anonymous delegates to async.
return false;
}
SimpleNameSyntax? FindStaticWaitInvocation(ExpressionSyntax from)
{
SimpleNameSyntax? name = ((from as InvocationExpressionSyntax)?.Expression as MemberAccessExpressionSyntax)?.Name;
return name?.Identifier.ValueText switch
{
nameof(Task.WaitAny) => name,
nameof(Task.WaitAll) => name,
_ => null,
};
}
ExpressionSyntax? TransformStaticWhatInvocation(ExpressionSyntax from, CancellationToken cancellationToken = default(CancellationToken))
{
SimpleNameSyntax? name = FindStaticWaitInvocation(from);
var newIdentifier = name!.Identifier.ValueText switch
{
nameof(Task.WaitAny) => nameof(Task.WhenAny),
nameof(Task.WaitAll) => nameof(Task.WhenAll),
_ => throw new InvalidOperationException(),
};
return from.ReplaceToken(name.Identifier, SyntaxFactory.Identifier(newIdentifier)).WithoutAnnotations(FixUtils.BookmarkAnnotationName);
}
ExpressionSyntax? FindTwoLevelDeepIdentifierInvocation(ExpressionSyntax from, CancellationToken cancellationToken = default(CancellationToken)) =>
((((from as InvocationExpressionSyntax)?.Expression as MemberAccessExpressionSyntax)?.Expression as InvocationExpressionSyntax)?.Expression as MemberAccessExpressionSyntax)?.Expression;
ExpressionSyntax? FindOneLevelDeepIdentifierInvocation(ExpressionSyntax from, CancellationToken cancellationToken = default(CancellationToken)) =>
((from as InvocationExpressionSyntax)?.Expression as MemberAccessExpressionSyntax)?.Expression;
ExpressionSyntax? FindParentMemberAccess(ExpressionSyntax from, CancellationToken cancellationToken = default(CancellationToken)) =>
(from as MemberAccessExpressionSyntax)?.Expression;
InvocationExpressionSyntax? parentInvocation = syntaxNode.FirstAncestorOrSelf<InvocationExpressionSyntax>();
MemberAccessExpressionSyntax? parentMemberAccess = syntaxNode.FirstAncestorOrSelf<MemberAccessExpressionSyntax>();
if (FindTwoLevelDeepIdentifierInvocation(parentInvocation) is object)
{
// This method will not return null for the provided 'target' argument
transform = NullableHelpers.AsNonNullReturnUnchecked<ExpressionSyntax, CancellationToken, ExpressionSyntax>(FindTwoLevelDeepIdentifierInvocation);
target = parentInvocation;
}
else if (FindStaticWaitInvocation(parentInvocation) is object)
{
// This method will not return null for the provided 'target' argument
transform = NullableHelpers.AsNonNullReturnUnchecked<ExpressionSyntax, CancellationToken, ExpressionSyntax>(TransformStaticWhatInvocation);
target = parentInvocation;
}
else if (FindOneLevelDeepIdentifierInvocation(parentInvocation) is object)
{
// This method will not return null for the provided 'target' argument
transform = NullableHelpers.AsNonNullReturnUnchecked<ExpressionSyntax, CancellationToken, ExpressionSyntax>(FindOneLevelDeepIdentifierInvocation);
target = parentInvocation;
}
else if (FindParentMemberAccess(parentMemberAccess) is object)
{
// This method will not return null for the provided 'target' argument
transform = NullableHelpers.AsNonNullReturnUnchecked<ExpressionSyntax, CancellationToken, ExpressionSyntax>(FindParentMemberAccess);
target = parentMemberAccess;
}
else
{
return false;
}
return true;
}
}
}