Skip to content

Commit

Permalink
Support SyncGen of Parenthesized Anonymous Object Creation Lambda (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnaCoda authored Aug 9, 2023
1 parent a5a1af4 commit bcda614
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,21 @@ private ExpressionSyntax Transform( ExpressionSyntax expr )
SimpleLambdaExpressionSyntax lambExpr => lambExpr
.WithModifiers( RemoveAsyncModifier( lambExpr.Modifiers ) )
.WithParameter( Transform( lambExpr.Parameter ) )
.WithExpressionBody( lambExpr.ExpressionBody != null ? Transform( lambExpr.ExpressionBody ) : null ),
.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 ) ),

_ => UnhandledSyntax( expr )
};

Expand Down Expand Up @@ -410,6 +419,14 @@ private SeparatedSyntaxList<TypeSyntax> TransformTypes( SeparatedSyntaxList<Type
);
}

private SeparatedSyntaxList<AnonymousObjectMemberDeclaratorSyntax> TransformAnonDecls( SeparatedSyntaxList<AnonymousObjectMemberDeclaratorSyntax> anonDecls ) {
return SyntaxFactory.SeparatedList(
anonDecls.Select( anonDecl => anonDecl
.WithExpression( Transform( anonDecl.Expression ) )
)
);
}

private EqualsValueClauseSyntax Transform( EqualsValueClauseSyntax arg )
=> arg.WithValue( Transform( arg.Value ) );

Expand All @@ -422,7 +439,7 @@ private ElseClauseSyntax Transform( ElseClauseSyntax clause )
private ArgumentListSyntax Transform( ArgumentListSyntax argList )
=> TransformAll( argList, Transform );

private TypeArgumentListSyntax Transform ( TypeArgumentListSyntax typeArgList )
private TypeArgumentListSyntax Transform( TypeArgumentListSyntax typeArgList )
=> typeArgList.WithArguments( TransformTypes( typeArgList.Arguments ) );

private ArgumentSyntax? Transform( ArgumentSyntax argument ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ public void SimpleLambda() {
Assert.AreEqual( "[Blocking] void Bar() { Func<int, Task> baz = quux => fred.Delay( 2*y ); }", actual.Value.ToFullString() );
}

[Test]
public void SimpleLambdaBlock() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { Action<int> bazAsync = async quuxAsync => { await fredAsync.Delay( 2*y ); await zoopAsync(); } }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( "[Blocking] void Bar() { Action<int> baz = quux => { fred.Delay( 2*y ); zoop(); } }", actual.Value.ToFullString() );
}

[Test]
public void WrapInTaskRun() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { string baz = await response.Content.ReadAsStringAsync(); }" );
Expand Down Expand Up @@ -328,6 +337,43 @@ public void CancellationTokenVariableArgument() {
Assert.AreEqual( @"[Blocking] Foo Bar() { var ct = new CancellationToken(); return Foo( 1,""test"" ); }", actual.Value.ToFullString() );
}

[Test]
public void ParenthesizedAnonymousCreationLambda() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { await BazAsync(() => new { BazAsync = 5, Quux = ""test"" } ); }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
// For now we are not transforming the variable names
Assert.AreEqual( @"[Blocking] void Bar() { Baz(() => new { BazAsync = 5,Quux = ""test"" } ); }", actual.Value.ToFullString() );
}

[Test]
public void AnonymousCreation() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { var options = new { size = new { width = GetCurrentWidthAsync(), height = 200 } }; await JsonSerializer.SerializeAsync(options); }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( @"[Blocking] void Bar() { var options = new { size = new { width = GetCurrentWidth(),height = 200 } }; JsonSerializer.Serialize(options); }", actual.Value.ToFullString() );
}

[Test]
public void ParenthesizedLambdaInvocation() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { await context.AddDeleteAction( () => Baz.DeleteAsync() ); }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( @"[Blocking] void Bar() { context.AddDeleteAction( () => Baz.Delete() ); }", actual.Value.ToFullString() );
}

[Test]
public void AsyncParenthesizedBlockLambda() {
var actual = Transform( @"[GenerateSync] async Task BarAsync() { await CreateAsync( async () => { using( new Context() ) { await BazAsync(); } } ); }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( @"[Blocking] void Bar() { Create( () => { using( new Context() ) { Baz(); } } ); }", actual.Value.ToFullString() );
}

[Test]
public void Silly() {
var actual = Transform( @"[GenerateSync]
Expand Down

0 comments on commit bcda614

Please sign in to comment.