Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm "async" anywhere from IAsyncEnumerable & methods returning it #905

Merged
merged 15 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ private TypeSyntax TransformType( TypeSyntax typeSynt, bool isReturnType = false
);
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() ); }
Expand Down Expand Up @@ -329,6 +333,11 @@ private StatementSyntax Transform( ExpressionStatementSyntax exprStmt ) {
}

private ExpressionSyntax Transform( InvocationExpressionSyntax invocationExpr) {
ITypeSymbol? returnTypeInfo = Model.GetTypeInfo( invocationExpr ).Type;
if( returnTypeInfo?.MetadataName == "IAsyncEnumerable`1" && returnTypeInfo.ContainingNamespace.ToString() == "System.Collections.Generic" ) {
return invocationExpr.WithExpression( SyntaxFactory.ParseExpression( invocationExpr.Expression.ToString().Replace( "Async", "" ) ) );
AnaCoda marked this conversation as resolved.
Show resolved Hide resolved
}

ExpressionSyntax newExpr = invocationExpr;
var memberAccess = invocationExpr.Expression as MemberAccessExpressionSyntax;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ public void CancellationTokenVariableArgument() {
Assert.AreEqual( @"[Blocking] Foo Bar() { var ct = new CancellationToken(); return Foo( 1,""test"" ); }", actual.Value.ToFullString() );
}

[Test]
public void IAsyncEnumerable() {
var actual = TransformWithIAsyncEnumerable( @"[GenerateSync] async Task BarAsync() { IAsyncEnumerable<string> m_enum = MethodReturningIAsyncEnumerable(); }" );

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( @"[Blocking] void Bar() { IEnumerable<string> m_enum = MethodReturningIEnumerable(); }", actual.Value.ToFullString() );
}

AnaCoda marked this conversation as resolved.
Show resolved Hide resolved
[Test]
public void IAsyncEnumerable_WithParameterContainingAsyncInName() {
var actual = TransformWithIAsyncEnumerable( @"
[GenerateSync] async Task BarAsync() {
int parameterWithAsyncInName = 0;
IAsyncEnumerable<string> m_enum = MethodReturningIAsyncEnumerable( parameterWithAsyncInName );
}"
);

Assert.IsTrue( actual.Success );
Assert.IsEmpty( actual.Diagnostics );
Assert.AreEqual( @"
[Blocking] void Bar() {
int parameterWithAsyncInName = 0;
IEnumerable<string> m_enum = MethodReturningIEnumerable( parameterWithAsyncInName );
}",
actual.Value.ToFullString()
);
}

[Test]
public void Silly() {
var actual = Transform( @"[GenerateSync]
Expand Down Expand Up @@ -460,11 +489,22 @@ public static TransformResult<MethodDeclarationSyntax> Transform( string methodS
return transformer.Transform( methodDecl );
}

public static TransformResult<MethodDeclarationSyntax> TransformWithIAsyncEnumerable( string methodSource ) {
var (compilation, methodDecl) = ParseMethodWithIAsyncEnumerable( methodSource );

var transformer = new AsyncToSyncMethodTransformer(
compilation.GetSemanticModel( methodDecl.SyntaxTree ),
CancellationToken.None
);

return transformer.Transform( methodDecl );
}

public static (Compilation, MethodDeclarationSyntax) ParseMethod( string methodSource ) {
var wrappedAndParsed = CSharpSyntaxTree.ParseText( @$"
using System.Threading.Tasks;
using D2L.CodeStyle.Annotations;
using System.Threading;
using D2L.CodeStyle.Annotations;

class TestType{{{methodSource}}}" );

Expand All @@ -475,6 +515,28 @@ class TestType{{{methodSource}}}" );
return (compilation, methodDecl);
}


public static (Compilation, MethodDeclarationSyntax) ParseMethodWithIAsyncEnumerable( string methodSource ) {
var wrappedAndParsed = CSharpSyntaxTree.ParseText( @$"
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using D2L.CodeStyle.Annotations;

class TestType{{
{methodSource}async IAsyncEnumerable<string> MethodReturningIAsyncEnumerable( int p = 0 ) {{
await Task.Delay(1000);
yield return ""test"";
}}
}}" );

var methodDecl = wrappedAndParsed.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().First();

var compilation = CreateSyncGeneratorTestCompilation( wrappedAndParsed );

return (compilation, methodDecl);
}

internal static Compilation CreateSyncGeneratorTestCompilation( params SyntaxTree[] trees )
=> CSharpCompilation.Create(
assemblyName: "test",
Expand All @@ -493,7 +555,8 @@ internal static Compilation CreateSyncGeneratorTestCompilation( params SyntaxTre

typeof( object ),
typeof( Task ),
typeof( GenerateSyncAttribute )
typeof( GenerateSyncAttribute ),
typeof( IAsyncEnumerable<object> )

}.Select( t => t.Assembly.Location )
.Distinct()
Expand Down
Loading