-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathUseExecuteAsyncOverrideFixer.cs
More file actions
181 lines (152 loc) · 7.8 KB
/
UseExecuteAsyncOverrideFixer.cs
File metadata and controls
181 lines (152 loc) · 7.8 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Immutable;
using System.Composition;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Simplification;
namespace MSTest.Analyzers;
/// <summary>
/// Code fixer for CS0115: Transform 'Execute' override to 'ExecuteAsync' when overriding TestMethodAttribute.
/// </summary>
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(UseExecuteAsyncOverrideFixer))]
[Shared]
public sealed class UseExecuteAsyncOverrideFixer : CodeFixProvider
{
/// <inheritdoc />
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
= ImmutableArray.Create("CS0115");
/// <inheritdoc />
public override FixAllProvider GetFixAllProvider()
// See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
=> WellKnownFixAllProviders.BatchFixer;
/// <inheritdoc />
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetRequiredSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
SyntaxToken identifierToken = root.FindToken(context.Span.Start);
if (identifierToken.Parent is MethodDeclarationSyntax methodDeclarationSyntax &&
IsExecuteMethodOverride(methodDeclarationSyntax))
{
context.RegisterCodeFix(
CodeAction.Create(
title: CodeFixResources.TransformExecuteToExecuteAsyncFix,
createChangedDocument: ct => TransformExecuteToExecuteAsyncAsync(context.Document, root, methodDeclarationSyntax),
equivalenceKey: nameof(UseExecuteAsyncOverrideFixer)),
context.Diagnostics);
}
}
private static bool IsExecuteMethodOverride(MethodDeclarationSyntax methodDeclaration)
{
// Check if method is named "Execute" and has override modifier
if (methodDeclaration.Identifier.ValueText != "Execute" ||
!methodDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword) ||
!methodDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword))
{
return false;
}
// Check if it has the expected signature, return type must be TestResult[]
if (methodDeclaration.ReturnType is not ArrayTypeSyntax arrayType ||
GetRightmostName(arrayType.ElementType) is not IdentifierNameSyntax { Identifier.ValueText: "TestResult" })
{
return false;
}
// It should have exactly one parameter.
if (methodDeclaration.ParameterList.Parameters.Count != 1)
{
return false;
}
// The parameter should be of type ITestMethod
ParameterSyntax parameter = methodDeclaration.ParameterList.Parameters[0];
if (GetRightmostName(parameter.Type) is not IdentifierNameSyntax { Identifier.ValueText: "ITestMethod" })
{
return false;
}
// We passed all the checks.
// The method signature is:
// public override TestResult[] Execute(ITestMethod)
return true;
}
private static SimpleNameSyntax? GetRightmostName(TypeSyntax? node)
=> node switch
{
QualifiedNameSyntax qualified when qualified.Right != null => qualified.Right,
SimpleNameSyntax simple => simple,
AliasQualifiedNameSyntax aliasQualifiedName when aliasQualifiedName.Name != null => aliasQualifiedName.Name,
_ => null,
};
private static Task<Document> TransformExecuteToExecuteAsyncAsync(Document document, SyntaxNode root, MethodDeclarationSyntax methodDeclaration)
{
// Change method name from Execute to ExecuteAsync
MethodDeclarationSyntax newMethod = methodDeclaration
.WithIdentifier(SyntaxFactory.Identifier("ExecuteAsync").WithTriviaFrom(methodDeclaration.Identifier))
.WithReturnType(WrapTypeWithGenericTask(methodDeclaration.ReturnType));
// Transform the method body to wrap return statements with Task.FromResult
if (methodDeclaration.Body is not null)
{
BlockSyntax newBody = TransformMethodBody(methodDeclaration.Body);
newMethod = newMethod.WithBody(newBody);
}
else if (methodDeclaration.ExpressionBody is not null)
{
// Handle expression body members
ArrowExpressionClauseSyntax newExpressionBody = TransformExpressionBody(methodDeclaration.ExpressionBody);
newMethod = newMethod.WithExpressionBody(newExpressionBody);
}
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(methodDeclaration, newMethod)));
}
private static GenericNameSyntax WrapTypeWithGenericTask(TypeSyntax type)
=> SyntaxFactory.GenericName(
SyntaxFactory.Identifier("Task"),
SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList(type))).WithAdditionalAnnotations(Simplifier.Annotation, Simplifier.AddImportsAnnotation, new SyntaxAnnotation("SymbolId", "System.Threading.Tasks.Task"));
private static BlockSyntax TransformMethodBody(BlockSyntax body)
{
// Transform all return statements to return Task.FromResult<TestResult[]>(...)
var transformer = new ReturnStatementTransformer();
return (BlockSyntax)transformer.Visit(body);
}
private static ArrowExpressionClauseSyntax TransformExpressionBody(ArrowExpressionClauseSyntax expressionBody)
{
ExpressionSyntax taskFromResultExpression = SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("Task"),
SyntaxFactory.IdentifierName("FromResult")),
SyntaxFactory.ArgumentList(
SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Argument(expressionBody.Expression))));
return expressionBody.WithExpression(taskFromResultExpression);
}
private sealed class ReturnStatementTransformer : CSharpSyntaxRewriter
{
public override SyntaxNode? VisitReturnStatement(ReturnStatementSyntax node)
{
if (node.Expression is null)
{
// Error scenario. We don't expect a return statement without an expression (return;)
return node;
}
ExpressionSyntax taskFromResultExpression = SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("Task"),
SyntaxFactory.IdentifierName("FromResult")),
SyntaxFactory.ArgumentList(
SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Argument(node.Expression))));
return node.WithExpression(taskFromResultExpression);
}
public override SyntaxNode? VisitLocalFunctionStatement(LocalFunctionStatementSyntax node)
=> node;
public override SyntaxNode? VisitAnonymousMethodExpression(AnonymousMethodExpressionSyntax node)
=> node;
public override SyntaxNode? VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax node)
=> node;
public override SyntaxNode? VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
=> node;
}
}