1919namespace 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