-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAsyncToSyncMethodTransformer.cs
More file actions
590 lines (470 loc) · 22.9 KB
/
AsyncToSyncMethodTransformer.cs
File metadata and controls
590 lines (470 loc) · 22.9 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
using D2L.CodeStyle.Analyzers.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;
namespace D2L.CodeStyle.Analyzers.Async.Generator;
internal sealed class AsyncToSyncMethodTransformer : SyntaxTransformer {
public AsyncToSyncMethodTransformer(
SemanticModel model,
CancellationToken token
) : base( model, token ) { }
// Need to disable D2L0018 in the method if we add Task.Run() to syncify something
private bool m_disableTaskRunWarningFlag;
// Need to modify return statement later on if function returns Task -> Void to prevent CS0127 (A method with a void return type cannot return a value)
private bool m_generatedFunctionReturnsVoid;
// Need to remove async not anywhere (not just suffix) in certain cases
private bool m_removeAsyncAnywhere;
public TransformResult<MethodDeclarationSyntax> Transform( MethodDeclarationSyntax decl ) {
decl = decl.WithAttributeLists( ReplaceGenerateSyncAttribute( decl.AttributeLists ) )
.WithModifiers( RemoveAsyncModifier( decl.Modifiers ) )
.WithIdentifier( RemoveAsyncSuffix( decl.Identifier ) )
.WithReturnType( TransformType( decl.ReturnType, isReturnType: true ) )
.WithExpressionBody( MaybeTransform( decl.ExpressionBody, Transform ) )
.WithBody( MaybeTransform( decl.Body, Transform ) )
.WithParameterList( Transform( decl.ParameterList ) );
if ( m_disableTaskRunWarningFlag ) {
PragmaWarningDirectiveTriviaSyntax restorePragma = SyntaxFactory.PragmaWarningDirectiveTrivia( SyntaxFactory.Token( SyntaxKind.RestoreKeyword ), true )
.AddErrorCodes( SyntaxFactory.IdentifierName( "D2L0018" ) ).NormalizeWhitespace().WithLeadingTrivia( SyntaxFactory.SyntaxTrivia( SyntaxKind.EndOfLineTrivia, "\n" ) ); ;
PragmaWarningDirectiveTriviaSyntax disablePragma = SyntaxFactory.PragmaWarningDirectiveTrivia( SyntaxFactory.Token( SyntaxKind.DisableKeyword ), true )
.AddErrorCodes( SyntaxFactory.IdentifierName( "D2L0018" ) ).NormalizeWhitespace();
decl = decl
.WithLeadingTrivia( decl.GetLeadingTrivia().Add( SyntaxFactory.Trivia( disablePragma ) ) )
.WithTrailingTrivia( decl.GetTrailingTrivia().Insert( 0, SyntaxFactory.Trivia( restorePragma ) ) );
m_disableTaskRunWarningFlag = false;
}
m_generatedFunctionReturnsVoid = false;
return GetResult( decl );
}
private SyntaxList<AttributeListSyntax> ReplaceGenerateSyncAttribute(
SyntaxList<AttributeListSyntax> attributeLists
) => TransformAll( attributeLists, ReplaceGenerateSyncAttribute );
private AttributeListSyntax? ReplaceGenerateSyncAttribute(
AttributeListSyntax attributeList
) => attributeList.WithAttributes(
TransformAll( attributeList.Attributes, ReplaceGenerateSyncAttribute )
);
private AttributeSyntax ReplaceGenerateSyncAttribute(
AttributeSyntax attribute
) {
// Attributes other than [GenerateSync] pass through
if( !IsGenerateSyncAttribute( attribute ) ) {
return attribute;
}
// Turn [GenerateSync] into [Blocking].
// This makes some assumptions about the ambient using directives.
return attribute.WithName(
SyntaxFactory.ParseName( "Blocking" ).WithTriviaFrom( attribute.Name )
);
}
private static SyntaxTokenList RemoveAsyncModifier( SyntaxTokenList modifiers ) =>
TransformAll(
modifiers,
static token => token.IsKind( SyntaxKind.AsyncKeyword ) ? null : token
);
private SyntaxToken RemoveAsyncSuffix( SyntaxToken ident, bool optional = false ) {
if( m_removeAsyncAnywhere && ident.ValueText.Contains( "Async" ) ) {
m_removeAsyncAnywhere = false;
return SyntaxFactory.Identifier(
ident.ValueText.Replace( "Async", "" )
).WithTriviaFrom( ident );
}
if( !ident.ValueText.EndsWith( "Async", StringComparison.Ordinal ) || ident.ValueText == "Async" ) {
if( optional ) {
return ident;
}
ReportDiagnostic(
Diagnostics.ExpectedAsyncSuffix,
ident.GetLocation(),
ident.ValueText
);
return ident;
}
const int AsyncSuffixLength = 5;
return SyntaxFactory.Identifier(
ident.ValueText.Substring( 0, ident.ValueText.Length - AsyncSuffixLength )
).WithTriviaFrom( ident );
}
private TypeSyntax TransformType( TypeSyntax typeSynt, bool isReturnType = false ) {
var returnTypeInfo = Model.GetTypeInfo( typeSynt, Token );
if( returnTypeInfo.Type == null ) {
GeneratorError( typeSynt.GetLocation(), "Couldn't resolve type" );
return typeSynt;
}
if( returnTypeInfo.Type.ContainingNamespace.ToString() == "System.Threading.Tasks" ) {
switch( returnTypeInfo.Type.MetadataName ) {
case "Task":
if( isReturnType ) {
m_generatedFunctionReturnsVoid = true;
return SyntaxFactory.ParseTypeName( "void" ).WithTriviaFrom( typeSynt );
} else {
return typeSynt;
}
case "Task`1":
return ( (GenericNameSyntax)typeSynt )
.TypeArgumentList.Arguments.First()
.WithTriviaFrom( typeSynt );
case "TaskCanceledException":
return typeSynt;
default:
GeneratorError(
typeSynt.GetLocation(),
$"Unexpected Task type: {returnTypeInfo.Type.MetadataName}"
);
return typeSynt;
}
} else if( returnTypeInfo.Type.MetadataName == "IAsyncEnumerable`1" && returnTypeInfo.Type.ContainingNamespace.ToString() == "System.Collections.Generic" ) {
return ( (GenericNameSyntax)typeSynt )
.WithIdentifier( SyntaxFactory.Identifier( "IEnumerable" ) )
.WithTriviaFrom( typeSynt );
}
if( isReturnType ) { ReportDiagnostic( Diagnostics.NonTaskReturnType, typeSynt.GetLocation() ); }
return typeSynt;
}
private ArrowExpressionClauseSyntax Transform( ArrowExpressionClauseSyntax body )
=> body.WithExpression( Transform( body.Expression ) );
private BlockSyntax Transform( BlockSyntax block )
=> block.WithStatements( TransformAll( block.Statements, Transform ) );
private SimpleNameSyntax Transform( SimpleNameSyntax simpleExpr )
=> simpleExpr.WithIdentifier( RemoveAsyncSuffix( simpleExpr.Identifier, optional: true ) );
private StatementSyntax Transform( StatementSyntax stmt )
=> stmt switch {
BlockSyntax blockStmt => Transform( blockStmt ),
BreakStatementSyntax => stmt,
ContinueStatementSyntax => stmt,
DoStatementSyntax doStmt => doStmt
.WithStatement( Transform( doStmt.Statement ) )
.WithCondition( Transform( doStmt.Condition ) ),
EmptyStatementSyntax => stmt,
ExpressionStatementSyntax exprStmt => Transform( exprStmt ),
GotoStatementSyntax => stmt,
IfStatementSyntax ifStmt => ifStmt
.WithCondition( Transform( ifStmt.Condition ) )
.WithStatement( Transform( ifStmt.Statement ) )
.WithElse( MaybeTransform( ifStmt.Else, Transform ) ),
LabeledStatementSyntax labeledStmt => labeledStmt
.WithStatement( Transform( labeledStmt.Statement ) ),
ThrowStatementSyntax throwStmt => throwStmt
.WithExpression( MaybeTransform( throwStmt.Expression, Transform ) ),
ReturnStatementSyntax returnStmt => Transform ( returnStmt ),
WhileStatementSyntax whileStmt => whileStmt
.WithCondition( Transform( whileStmt.Condition ) )
.WithStatement( Transform( whileStmt.Statement ) ),
LocalDeclarationStatementSyntax localDeclStmt => localDeclStmt
.WithDeclaration( Transform( localDeclStmt.Declaration ) ),
TryStatementSyntax tryStmt => Transform( tryStmt ),
ForEachStatementSyntax forEachStmt => forEachStmt
.WithType( TransformType( forEachStmt.Type ) )
.WithIdentifier( RemoveAsyncSuffix( forEachStmt.Identifier, optional: true ) )
.WithExpression( Transform( forEachStmt.Expression ) )
.WithStatement( Transform ( forEachStmt.Statement ) ),
UsingStatementSyntax usingStmt => usingStmt
.WithDeclaration( usingStmt.Declaration != null ? Transform( usingStmt.Declaration ) : null )
.WithStatement( Transform( usingStmt.Statement ) ),
_ => UnhandledSyntax( stmt )
};
private StatementSyntax Transform( ReturnStatementSyntax returnStmt ) {
var expr = returnStmt.Expression;
if( !m_generatedFunctionReturnsVoid || expr is null ) {
return returnStmt.WithExpression( MaybeTransform( returnStmt.Expression, Transform ) );
}
return IsStatementCompatibleExpression( expr ) switch {
Compatibility.Compatible => SyntaxFactory.Block(
SyntaxFactory.ExpressionStatement( Transform( expr ) ).WithLeadingTrivia( SyntaxFactory.Space ),
SyntaxFactory.ReturnStatement().WithLeadingTrivia( SyntaxFactory.Space ).WithTrailingTrivia( SyntaxFactory.Space )
).WithTriviaFrom( returnStmt ),
Compatibility.Incompatible => SyntaxFactory.ReturnStatement().WithTriviaFrom( returnStmt ),
Compatibility.Unsupported => UnhandledSyntax( returnStmt ),
_ => throw new NotImplementedException()
};
}
private enum Compatibility {
Compatible,
Incompatible,
/// <summary>
/// Can't be transformed yet by our code
/// </summary>
Unsupported
}
private static Compatibility IsStatementCompatibleExpression( ExpressionSyntax expr )
=> expr switch {
InvocationExpressionSyntax => Compatibility.Compatible,
PostfixUnaryExpressionSyntax => Compatibility.Compatible,
PrefixUnaryExpressionSyntax => Compatibility.Compatible,
AwaitExpressionSyntax => Compatibility.Compatible,
ObjectCreationExpressionSyntax => Compatibility.Unsupported,
AssignmentExpressionSyntax => Compatibility.Unsupported,
_ => Compatibility.Incompatible
};
private ExpressionSyntax Transform( ExpressionSyntax expr )
=> expr switch {
AssignmentExpressionSyntax asgnExpr => asgnExpr
.WithLeft( Transform( asgnExpr.Left ) )
.WithRight( Transform( asgnExpr.Right ) ),
AwaitExpressionSyntax awaitExpr =>
// Extract the inner expression, removing the "await"
Transform( awaitExpr.Expression ).ConcatLeadingTriviaFrom( awaitExpr ),
BinaryExpressionSyntax binExpr => binExpr
.WithLeft( Transform( binExpr.Left ) )
.WithRight( Transform( binExpr.Right ) ),
CollectionExpressionSyntax collectionExpr =>
collectionExpr.WithElements( Transform( collectionExpr.Elements ) ),
ConditionalExpressionSyntax condExpr => condExpr
.WithCondition( Transform( condExpr.Condition ) )
.WithWhenTrue( Transform( condExpr.WhenTrue ) )
.WithWhenFalse( Transform( condExpr.WhenFalse ) ),
DefaultExpressionSyntax => expr,
ElementAccessExpressionSyntax eaExpr => eaExpr
.WithExpression( Transform( eaExpr.Expression ) )
.WithArgumentList( TransformAll( eaExpr.ArgumentList, Transform ) ),
IdentifierNameSyntax identExpr => identExpr
.WithIdentifier( RemoveAsyncSuffix( identExpr.Identifier, optional: true ) ),
ImplicitObjectCreationExpressionSyntax implicitObjectCreationExpr => implicitObjectCreationExpr
.WithArgumentList( Transform( implicitObjectCreationExpr.ArgumentList ) )
.WithInitializer( MaybeTransform( implicitObjectCreationExpr.Initializer, Transform ) ),
InvocationExpressionSyntax invocationExpr => Transform(invocationExpr),
CastExpressionSyntax castExpr => castExpr
.WithType( TransformType( castExpr.Type ) )
.WithExpression( Transform( castExpr.Expression ) ),
LiteralExpressionSyntax => expr,
ObjectCreationExpressionSyntax newExpr => newExpr
.WithArgumentList( MaybeTransform( newExpr.ArgumentList, Transform ) )
.WithInitializer( MaybeTransform( newExpr.Initializer, Transform ) ),
ParenthesizedExpressionSyntax pExpr => pExpr
.WithExpression( Transform( pExpr.Expression ) ),
PostfixUnaryExpressionSyntax postfixExpr => postfixExpr
.WithOperand( Transform( postfixExpr.Operand ) ),
PrefixUnaryExpressionSyntax prefixExpr => prefixExpr
.WithOperand( Transform( prefixExpr.Operand ) ),
SizeOfExpressionSyntax => expr,
ThisExpressionSyntax => expr,
MemberAccessExpressionSyntax memberAccessExpr => Transform( memberAccessExpr ),
DeclarationExpressionSyntax declExpr => declExpr
.WithType( TransformType( declExpr.Type ) )
.WithDesignation( Transform( declExpr.Designation ) ),
PredefinedTypeSyntax => expr,
IsPatternExpressionSyntax pattExpr => pattExpr
.WithExpression( Transform( pattExpr.Expression ) )
.WithPattern( Transform( pattExpr.Pattern ) ),
SimpleLambdaExpressionSyntax lambExpr => lambExpr
.WithModifiers( RemoveAsyncModifier( lambExpr.Modifiers ) )
.WithParameter( Transform( lambExpr.Parameter ) )
.WithExpressionBody( lambExpr.ExpressionBody != null ? Transform( lambExpr.ExpressionBody ) : null )
.WithBlock( lambExpr.Block != null ? Transform( lambExpr.Block ) : null ),
GenericNameSyntax genExpr => genExpr
.WithIdentifier( RemoveAsyncSuffix( genExpr.Identifier, optional: true ) )
.WithTypeArgumentList( Transform( genExpr.TypeArgumentList ) ),
ParenthesizedLambdaExpressionSyntax parLambExpr => parLambExpr
.WithModifiers( RemoveAsyncModifier( parLambExpr.Modifiers ) )
.WithExpressionBody( parLambExpr.ExpressionBody != null ? Transform( parLambExpr.ExpressionBody ) : null )
.WithBlock( parLambExpr.Block != null ? Transform( parLambExpr.Block ) : null ),
AnonymousObjectCreationExpressionSyntax anonCreationExpr => anonCreationExpr
.WithInitializers( TransformAnonDecls( anonCreationExpr.Initializers ) ),
ConditionalAccessExpressionSyntax condAccessExpr => condAccessExpr
.WithExpression( Transform( condAccessExpr.Expression ) )
.WithWhenNotNull( Transform( condAccessExpr.WhenNotNull ) ),
MemberBindingExpressionSyntax memBindExpr => memBindExpr
.WithName( Transform( memBindExpr.Name ) ),
InterpolatedStringExpressionSyntax interStringExpr => interStringExpr
.WithContents( TransformInterpolations( interStringExpr.Contents ) ),
_ => UnhandledSyntax( expr )
};
private VariableDesignationSyntax Transform( VariableDesignationSyntax des )
=> des switch {
SingleVariableDesignationSyntax varDes => varDes
.WithIdentifier( RemoveAsyncSuffix( varDes.Identifier, optional: true ) ),
_ => UnhandledSyntax( des )
};
private SeparatedSyntaxList<CollectionElementSyntax> Transform(
SeparatedSyntaxList<CollectionElementSyntax> original
) => SyntaxFactory.SeparatedList(
original.Select( elem => elem switch {
ExpressionElementSyntax ee => ee.WithExpression( Transform( ee.Expression ) ),
SpreadElementSyntax sp => sp.WithExpression( Transform( sp.Expression ) ),
_ => UnhandledSyntax( elem )
} ),
original.GetSeparators()
);
private StatementSyntax Transform( ExpressionStatementSyntax exprStmt ) {
var result = Transform( exprStmt.Expression );
// "await foo;" is redundant in sync land, so turn it into an
// EmptyStatementSyntax (just a bare ";".)
if( exprStmt.Expression is AwaitExpressionSyntax && result is IdentifierNameSyntax ) {
return SyntaxFactory.EmptyStatement().WithTriviaFrom( exprStmt );
}
return exprStmt.WithExpression( result );
}
private ExpressionSyntax Transform( InvocationExpressionSyntax invocationExpr) {
ITypeSymbol? returnTypeInfo = Model.GetTypeInfo( invocationExpr ).Type;
bool prevRemoveAsyncAnywhereState = m_removeAsyncAnywhere;
if( returnTypeInfo?.MetadataName == "IAsyncEnumerable`1" && returnTypeInfo.ContainingNamespace.ToString() == "System.Collections.Generic" ) {
m_removeAsyncAnywhere = true;
}
ExpressionSyntax newExpr = invocationExpr;
var memberAccess = invocationExpr.Expression as MemberAccessExpressionSyntax;
try {
if( memberAccess is not null && ShouldRemoveInvocation( memberAccess ) ) {
if( memberAccess?.Expression is not null ) {
newExpr = memberAccess.Expression;
return Transform( newExpr );
}
} else if( memberAccess is not null && ShouldWrapMemberAccessInTaskRun( memberAccess ) ) {
m_disableTaskRunWarningFlag = true;
return SyntaxFactory.ParseExpression( $"Task.Run(() => {invocationExpr}).Result" );
}
return invocationExpr = invocationExpr
.WithExpression( Transform( invocationExpr.Expression ) )
.WithArgumentList( TransformAll( invocationExpr.ArgumentList, Transform ) ); ;
} finally {
m_removeAsyncAnywhere = prevRemoveAsyncAnywhereState;
}
}
bool ShouldRemoveReturnedMemberAccess( MemberAccessExpressionSyntax memberAccessExpr ) {
return (memberAccessExpr.Expression.ToString(), memberAccessExpr.Name.Identifier.ValueText) switch {
("Task", "FromResult") => true,
("Task", "CompletedTask") => true,
_ => false
};
}
bool ShouldWrapMemberAccessInTaskRun( MemberAccessExpressionSyntax memberAccessExpr ) {
return (memberAccessExpr.Expression.ToString(), memberAccessExpr.Name.Identifier.ValueText) switch {
(_, "ReadAsStringAsync") => true,
_ => false
};
}
bool ShouldRemoveInvocation( MemberAccessExpressionSyntax memberAccessExpr ) {
return (memberAccessExpr.Expression.ToString(), memberAccessExpr.Name.Identifier.ValueText) switch {
(_, "ConfigureAwait" ) => true,
(_, "SafeAsync" ) => true,
_ => false
};
}
private ExpressionSyntax Transform( MemberAccessExpressionSyntax memberAccessExpr ) {
if( memberAccessExpr.IsKind( SyntaxKind.SimpleMemberAccessExpression ) ) {
if( ShouldRemoveReturnedMemberAccess( memberAccessExpr ) &&
( memberAccessExpr.Parent.IsKind( SyntaxKind.ReturnStatement ) ||
( memberAccessExpr.Parent?.Parent?.IsKind( SyntaxKind.ReturnStatement ) ?? false ) ) ) {
return SyntaxFactory.ParseExpression( "" );
}
return memberAccessExpr
.WithExpression( Transform( memberAccessExpr.Expression ) )
.WithName( Transform( memberAccessExpr.Name ) );
}
return UnhandledSyntax( memberAccessExpr );
}
private VariableDeclarationSyntax Transform( VariableDeclarationSyntax varDecl ) {
return varDecl
.WithType( TransformType( varDecl.Type ) )
.WithVariables( TransformVariables( varDecl.Variables ) );
}
private StatementSyntax Transform( TryStatementSyntax tryStmt ) {
var block = Transform( tryStmt.Block );
var catches = TransformAll( tryStmt.Catches, Transform );
var finallyClause = tryStmt.Finally != null ? Transform( tryStmt.Finally ) : null;
if( finallyClause == null && catches.Count == 0 ) {
return block;
} else {
return tryStmt.WithBlock( block ).WithCatches( catches ).WithFinally( finallyClause );
}
}
private FinallyClauseSyntax Transform( FinallyClauseSyntax finallyClause )
=> finallyClause.WithBlock( Transform( finallyClause.Block ) );
private SeparatedSyntaxList<VariableDeclaratorSyntax> TransformVariables( SeparatedSyntaxList<VariableDeclaratorSyntax> varDecls ) {
return SyntaxFactory.SeparatedList(
varDecls.Select( varDecl =>
SyntaxFactory.VariableDeclarator(
RemoveAsyncSuffix( varDecl.Identifier, optional: true ),
MaybeTransform( varDecl.ArgumentList, Transform ),
MaybeTransform( varDecl.Initializer, Transform )
)
)
);
}
private SeparatedSyntaxList<TypeSyntax> TransformTypes( SeparatedSyntaxList<TypeSyntax> typeDecls ) {
return SyntaxFactory.SeparatedList(
typeDecls.Select( typeDecl => TransformType( typeDecl ) )
);
}
private SeparatedSyntaxList<AnonymousObjectMemberDeclaratorSyntax> TransformAnonDecls( SeparatedSyntaxList<AnonymousObjectMemberDeclaratorSyntax> anonDecls ) {
return SyntaxFactory.SeparatedList(
anonDecls.Select( anonDecl => anonDecl
.WithExpression( Transform( anonDecl.Expression ) )
)
);
}
private SyntaxList<InterpolatedStringContentSyntax> TransformInterpolations( SyntaxList<InterpolatedStringContentSyntax> interSyntaxes ) {
SyntaxList<InterpolatedStringContentSyntax> newInterSyntaxes = interSyntaxes;
for( int i = 0; i < interSyntaxes.Count; i++ ) {
if( interSyntaxes[i].IsKind( SyntaxKind.Interpolation ) ) {
InterpolationSyntax inter = (InterpolationSyntax)interSyntaxes[i];
newInterSyntaxes = newInterSyntaxes.Replace( newInterSyntaxes[i], inter.WithExpression( Transform( inter.Expression ) ) );
}
}
return newInterSyntaxes;
}
private EqualsValueClauseSyntax Transform( EqualsValueClauseSyntax arg )
=> arg.WithValue( Transform( arg.Value ) );
private BracketedArgumentListSyntax Transform( BracketedArgumentListSyntax argList )
=> TransformAll( argList, Transform );
private ElseClauseSyntax Transform( ElseClauseSyntax clause )
=> clause.WithStatement( Transform( clause.Statement ) );
private ArgumentListSyntax Transform( ArgumentListSyntax argList )
=> TransformAll( argList, Transform );
private TypeArgumentListSyntax Transform( TypeArgumentListSyntax typeArgList )
=> typeArgList.WithArguments( TransformTypes( typeArgList.Arguments ) );
private ParameterListSyntax Transform( ParameterListSyntax paramList )
=> paramList.WithParameters( TransformParams( paramList.Parameters ) );
private ArgumentSyntax? Transform( ArgumentSyntax argument ) {
if( Model.GetTypeInfo( argument.Expression ).Type?.Name == "CancellationToken" ) {
return null;
}
return argument.WithExpression( Transform( argument.Expression ) );
}
private SeparatedSyntaxList<ParameterSyntax> TransformParams( SeparatedSyntaxList<ParameterSyntax> paramSynts ) {
for( int i = 0; i < paramSynts.Count; i++ ) {
if( paramSynts[i].Type?.ToString() == "CancellationToken" ) {
paramSynts = paramSynts.RemoveAt( i );
i--;
} else {
paramSynts = paramSynts.Replace( paramSynts[i], Transform( paramSynts[i] ) );
}
}
return paramSynts;
}
private ParameterSyntax Transform( ParameterSyntax parameter ) {
return parameter.WithIdentifier( RemoveAsyncSuffix( parameter.Identifier, optional: true ) );
}
private CatchClauseSyntax? Transform( CatchClauseSyntax catchClause ) {
catchClause = catchClause
.WithDeclaration( catchClause.Declaration != null ? Transform( catchClause.Declaration ) : null )
.WithBlock( Transform( catchClause.Block ) );
if( !m_disableTaskRunWarningFlag && catchClause.Declaration?.Type.ToString() == "TaskCanceledException" ) {
return null;
}
return catchClause;
}
private CatchDeclarationSyntax Transform( CatchDeclarationSyntax catchDecl ) {
return catchDecl
.WithType( TransformType( catchDecl.Type ) )
.WithIdentifier( RemoveAsyncSuffix( catchDecl.Identifier, optional: true ) );
}
private InitializerExpressionSyntax Transform( InitializerExpressionSyntax initializer )
=> initializer.WithExpressions( TransformAll( initializer.Expressions, Transform ) );
private PatternSyntax Transform( PatternSyntax pattern )
=> pattern;
private bool IsGenerateSyncAttribute( AttributeSyntax attribute ) {
var attributeConstructorSymbol = Model.GetSymbolInfo( attribute, Token ).Symbol as IMethodSymbol;
if( attributeConstructorSymbol == null ) {
return false;
}
return attributeConstructorSymbol.ContainingType.ToDisplayString()
== "D2L.CodeStyle.Annotations.GenerateSyncAttribute";
}
public T UnhandledSyntax<T>( T node ) where T : SyntaxNode {
GeneratorError(
node.GetLocation(),
$"unhandled syntax kind: {node.Kind()}"
);
return node;
}
}