Skip to content

Commit 34db99f

Browse files
authored
Bugfix/14 (#15)
* bug fixed
1 parent 158f723 commit 34db99f

File tree

3 files changed

+80
-58
lines changed

3 files changed

+80
-58
lines changed

src/Amusoft.CodeAnalysis.Analyzers.Test/Tests/ACA0001/FixByForwardingToCollectionChildrenTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public Task Method1(object p1)
7878

7979
var expected = new DiagnosticResult[]
8080
{
81-
82-
};
81+
// Test0.cs(20,21): info ACA0001: Forward execution of "Method1" to member "_disposables"
82+
Verifier.Diagnostic().WithSpan(20, 21, 20, 28).WithArguments("Method1", "_disposables")
83+
};
8384

8485
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
8586
}
@@ -140,8 +141,9 @@ public async Task Method1(object p1)
140141

141142
var expected = new DiagnosticResult[]
142143
{
143-
144-
};
144+
// Test0.cs(20,27): info ACA0001: Forward execution of "Method1" to member "_disposables"
145+
Verifier.Diagnostic().WithSpan(20, 27, 20, 34).WithArguments("Method1", "_disposables")
146+
};
145147

146148
await Verifier.VerifyCodeFixAsync(test, expected, fixtest);
147149
}

src/Amusoft.CodeAnalysis.Analyzers/ACA0001/Analyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public static bool TryAnalyzeMethod(SemanticModel semanticModel, IMethodSymbol m
156156
}
157157

158158
var taskGenericType = semanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1");
159-
if (methodSymbol.ReturnType is INamedTypeSymbol ntMethodSymbol && ntMethodSymbol.ConstructedFrom.Equals(taskGenericType))
159+
var taskType = semanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
160+
if (methodSymbol.ReturnType is INamedTypeSymbol ntMethodSymbol && ntMethodSymbol.Equals(taskType))
160161
{
161162
returnTask = true;
162-
returnBool = ntMethodSymbol.TypeArguments[0].Equals(boolType);
163-
return returnBool;
163+
return true;
164164
}
165165

166166
return false;

src/Amusoft.CodeAnalysis.Analyzers/ACA0001/FixByForwardingToCollectionChildren.cs

Lines changed: 71 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace Amusoft.CodeAnalysis.Analyzers.ACA0001
2020
{
2121
[ExportCodeFixProvider(LanguageNames.CSharp, Name = "ACA0001-FixByForwardingToCollectionChildren"), Shared]
22-
public class FixByForwardingToCollectionChildren : CodeFixProvider
22+
public class FixByForwardingToCollectionChildren : CodeFixProvider
2323
{
2424
private const string CodeFixUniqueKey = "ACA0001-FixByForwardingToCollectionChildren";
2525

@@ -40,13 +40,16 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
4040
{
4141
var title = diagnostic.Descriptor.MessageFormat.ToString();
4242
if (diagnostic.Properties.TryGetValue(Analyzer.Properties.MemberName, out var memberName))
43-
context.RegisterCodeFix(CodeAction.Create(title, c => CreateChangedDocument(context, c, diagnostic), CodeFixUniqueKey + memberName), diagnostic);
43+
context.RegisterCodeFix(
44+
CodeAction.Create(title, c => CreateChangedDocument(context, c, diagnostic),
45+
CodeFixUniqueKey + memberName), diagnostic);
4446
}
4547

4648
return Task.CompletedTask;
4749
}
4850

49-
private async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToken, Diagnostic diagnostic)
51+
private async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToken,
52+
Diagnostic diagnostic)
5053
{
5154
var root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5255
var node = root.FindNode(diagnostic.Location.SourceSpan);
@@ -59,75 +62,92 @@ private async Task<Document> CreateChangedDocument(CodeFixContext context, Cance
5962
var semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken);
6063
var newMethod = RewriteMethod(semanticModel, methodNode, memberName);
6164

62-
var replacedRoot = root.ReplaceNode(methodNode.Body, newMethod.Body.WithAdditionalAnnotations(Formatter.Annotation));
65+
var replacedRoot = root.ReplaceNode(methodNode.Body,
66+
newMethod.Body.WithAdditionalAnnotations(Formatter.Annotation));
6367

6468
return context.Document.WithSyntaxRoot(replacedRoot);
6569
}
6670

6771
return context.Document;
6872
}
6973

70-
private static MethodDeclarationSyntax RewriteMethod(SemanticModel semanticModel, MethodDeclarationSyntax methodNode, string memberName)
74+
private static MethodDeclarationSyntax RewriteMethod(SemanticModel semanticModel,
75+
MethodDeclarationSyntax methodNode, string memberName)
7176
{
7277
if (!(semanticModel.GetDeclaredSymbol(methodNode) is IMethodSymbol methodSymbol))
7378
return methodNode;
7479

75-
if (!Analyzer.DiagnosticHelper.TryAnalyzeMethod(semanticModel, methodSymbol, out var returnTask, out var returnBool))
80+
if (!Analyzer.DiagnosticHelper.TryAnalyzeMethod(semanticModel, methodSymbol, out var returnTask,
81+
out var returnBool))
7682
return methodNode;
7783

7884
if (returnBool)
7985
{
80-
if (returnTask)
81-
{
82-
return RewriteAsTaskMethod(methodNode, memberName);
83-
}
84-
else
85-
{
86-
return RewriteAsBoolMethod(methodNode, memberName);
87-
}
86+
return RewriteAsBoolMethod(methodNode, memberName);
8887
}
89-
else
88+
89+
if (returnTask)
9090
{
91-
return RewriteAsVoidMethod(methodNode, memberName);
91+
return RewriteAsTaskMethod(methodNode, memberName);
9292
}
93+
94+
return RewriteAsVoidMethod(methodNode, memberName);
9395
}
9496

95-
private static MethodDeclarationSyntax RewriteAsBoolMethod(MethodDeclarationSyntax methodNode, string memberName)
97+
private static MethodDeclarationSyntax RewriteAsBoolMethod(MethodDeclarationSyntax methodNode,
98+
string memberName)
9699
{
97100
return MethodDeclaration(methodNode.ReturnType, methodNode.Identifier).WithBody(
98101
Block(
99102
SingletonList<StatementSyntax>(
100103
ReturnStatement(
101-
InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName(memberName), IdentifierName("All")))
104+
InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
105+
IdentifierName(memberName), IdentifierName("All")))
102106
.WithArgumentList(
103107
ArgumentList(
104-
SingletonSeparatedList(Argument(SimpleLambdaExpression(Parameter(Identifier("item")), CreateIterationCall(methodNode))))))
105-
)
108+
SingletonSeparatedList(Argument(
109+
SimpleLambdaExpression(Parameter(Identifier("item")),
110+
CreateIterationCall(methodNode))))))
111+
)
106112
)));
107113
}
108114

109-
private static MethodDeclarationSyntax RewriteAsTaskMethod(MethodDeclarationSyntax methodNode, string memberName)
115+
private static MethodDeclarationSyntax RewriteAsTaskMethod(MethodDeclarationSyntax methodNode,
116+
string memberName)
110117
{
111118
if (methodNode.Modifiers.Any(SyntaxKind.AsyncKeyword))
112119
{
113-
return MethodDeclaration(methodNode.ReturnType, methodNode.Identifier).WithBody(
114-
Block(
115-
SingletonList<StatementSyntax>(
116-
ReturnStatement(
117-
InvocationExpression(
118-
MemberAccessExpression(
119-
SyntaxKind.SimpleMemberAccessExpression,
120-
IdentifierName(memberName),
121-
IdentifierName("All")
122-
),
123-
ArgumentList(
124-
SingletonSeparatedList(
125-
Argument(
126-
SimpleLambdaExpression(
127-
Parameter(
128-
Identifier("item")
129-
),
130-
CreateIterationCall(methodNode)
120+
return methodNode.WithBody(
121+
Block(SingletonList<StatementSyntax>(
122+
ExpressionStatement(
123+
AwaitExpression(
124+
InvocationExpression(
125+
MemberAccessExpression(
126+
SyntaxKind.SimpleMemberAccessExpression,
127+
IdentifierName("Task"),
128+
IdentifierName("WhenAll")
129+
),
130+
ArgumentList(
131+
SingletonSeparatedList(
132+
Argument(
133+
InvocationExpression(
134+
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
135+
IdentifierName(memberName),
136+
IdentifierName("Select")
137+
),
138+
ArgumentList(
139+
SingletonSeparatedList(
140+
Argument(
141+
SimpleLambdaExpression(
142+
Parameter(
143+
Identifier("item")
144+
),
145+
CreateIterationCall(methodNode)
146+
)
147+
)
148+
)
149+
)
150+
)
131151
)
132152
)
133153
)
@@ -140,24 +160,23 @@ private static MethodDeclarationSyntax RewriteAsTaskMethod(MethodDeclarationSynt
140160
}
141161
else
142162
{
143-
return MethodDeclaration(methodNode.ReturnType, methodNode.Identifier).WithBody(
144-
Block(
145-
SingletonList<StatementSyntax>(
163+
return methodNode.WithBody(
164+
Block(SingletonList<StatementSyntax>(
146165
ReturnStatement(
166+
147167
InvocationExpression(
148168
MemberAccessExpression(
149169
SyntaxKind.SimpleMemberAccessExpression,
150170
IdentifierName("Task"),
151-
IdentifierName("FromResult")
171+
IdentifierName("WhenAll")
152172
),
153173
ArgumentList(
154174
SingletonSeparatedList(
155175
Argument(
156176
InvocationExpression(
157-
MemberAccessExpression(
158-
SyntaxKind.SimpleMemberAccessExpression,
177+
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
159178
IdentifierName(memberName),
160-
IdentifierName("All")
179+
IdentifierName("Select")
161180
),
162181
ArgumentList(
163182
SingletonSeparatedList(
@@ -183,7 +202,8 @@ private static MethodDeclarationSyntax RewriteAsTaskMethod(MethodDeclarationSynt
183202
}
184203
}
185204

186-
private static MethodDeclarationSyntax RewriteAsVoidMethod(MethodDeclarationSyntax methodNode, string memberName)
205+
private static MethodDeclarationSyntax RewriteAsVoidMethod(MethodDeclarationSyntax methodNode,
206+
string memberName)
187207
{
188208
return MethodDeclaration(methodNode.ReturnType, methodNode.Identifier).WithBody(
189209
Block(
@@ -210,9 +230,9 @@ private static InvocationExpressionSyntax CreateIterationCall(MethodDeclarationS
210230
IdentifierName("item"),
211231
IdentifierName(methodNode.Identifier.Text)
212232
)).WithArgumentList(
213-
ArgumentList(
214-
SeparatedList<ArgumentSyntax>(CreateMethodArguments(methodNode)
215-
)));
233+
ArgumentList(
234+
SeparatedList<ArgumentSyntax>(CreateMethodArguments(methodNode)
235+
)));
216236
}
217237
else
218238
{
@@ -230,4 +250,4 @@ private static IEnumerable<ArgumentSyntax> CreateMethodArguments(MethodDeclarati
230250
return methodNode.ParameterList.Parameters.Select(d => Argument(IdentifierName(d.Identifier.Text)));
231251
}
232252
}
233-
}
253+
}

0 commit comments

Comments
 (0)