Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 16 additions & 14 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,6 @@ private static string MakeType(ISymbol symbol)
=> symbol switch
{
INamedTypeSymbol { Name: "AsyncEnumerable" } => Global("System.Linq.Enumerable"),
INamedTypeSymbol { Name: "EntityFrameworkQueryableExtensions" } => Global("System.Linq.Queryable"),
INamedTypeSymbol => symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
_ => symbol.Name,
};
Expand Down Expand Up @@ -2067,19 +2066,22 @@ private InvocationExpressionSyntax UnwrapExtension(InvocationExpressionSyntax ie
var newName = reducedFrom.Name;
newName = changeMemoryToSpan ? ReplaceWithSpan(reducedFrom) : RemoveAsync(newName);

var membersWithNewNameInContainingType = semanticModel.Compilation.References
.Select(semanticModel.Compilation.GetAssemblyOrModuleSymbol)
.Append(semanticModel.Compilation.Assembly)
.OfType<IAssemblySymbol>()
.Select(assemblySymbol => assemblySymbol.GetTypeByMetadataName(containingType.ToString()))
.OfType<INamedTypeSymbol>()
.SelectMany(symbol => symbol.GetMembers(newName));

// When the method is an AsyncEnumerable extension it must be converted to the corresponding Enumerable extension
// regardless of the containing type featuring members with compatible names
var fullyQualifiedName = !containingType.Name.Equals("AsyncEnumerable", StringComparison.Ordinal) && membersWithNewNameInContainingType.Any()
? $"{containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{newName}"
: $"{MakeType(containingType)}.{newName}";
var (enumerableMembers, queryableMembers) = semanticModel.Compilation.GetLinqMembers();

var fullyQualifiedName = $"{MakeType(containingType)}.{newName}";

// Supports EntityFrameworkQueryableExtensions and potentially other queryable extensions
if (containingType.Name.EndsWith("QueryableExtensions", StringComparison.OrdinalIgnoreCase))
{
if (queryableMembers.Contains(newName))
{
fullyQualifiedName = $"{Global("System.Linq.Queryable")}.{newName}";
}
else if (enumerableMembers.Contains(newName))
{
fullyQualifiedName = $"{Global("System.Linq.Enumerable")}.{newName}";
}
}

var es = (ies.Expression switch
{
Expand Down
22 changes: 22 additions & 0 deletions src/Zomp.SyncMethodGenerator/Helpers/CompilationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Zomp.SyncMethodGenerator.Helpers;

internal static class CompilationExtensions
{
public static (ISet<string> EnumerableMembers, ISet<string> QueryableMembers) GetLinqMembers(this Compilation compilation)
{
INamedTypeSymbol? linqEnumerable = null;
INamedTypeSymbol? linqQueryable = null;
foreach (var reference in compilation.References)
{
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(reference) as IAssemblySymbol;
linqEnumerable ??= assemblySymbol?.GetTypeByMetadataName("System.Linq.Enumerable");
linqQueryable ??= assemblySymbol?.GetTypeByMetadataName("System.Linq.Queryable");
if (linqEnumerable != null && linqQueryable != null)
{
break;
}
}

return (new HashSet<string>(linqEnumerable?.MemberNames ?? []), new HashSet<string>(linqQueryable?.MemberNames ?? []));
}
}
22 changes: 22 additions & 0 deletions tests/Generator.Tests/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ public async Task<int> QueryableExtensionAsync(DbContext dbContext, Cancellation
}
}
}
""".Verify(sourceType: SourceType.Full);

[Fact]
public Task EntityFrameworkQueryableToListAsync() => """
using System.Threading;
using System.Threading.Tasks;

namespace Zomp.SyncMethodGenerator.IntegrationTests
{
using Microsoft.EntityFrameworkCore;

public partial class EntityFrameworkQueryableExtensions
{
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public async Task<int> QueryableToListAsync(DbContext dbContext, CancellationToken cancellationToken)
{
var dbSet = dbContext.Set<object>();
var result = await dbSet.ToListAsync(cancellationToken);
return result.Count;
}
}
}
""".Verify(sourceType: SourceType.Full);
#endif

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//HintName: Zomp.SyncMethodGenerator.IntegrationTests.EntityFrameworkQueryabl_b3d48c88.QueryableToListAsync.g.cs
// <auto-generated/>
#nullable enable

using System.Threading;
using System.Threading.Tasks;

namespace Zomp.SyncMethodGenerator.IntegrationTests
{
using Microsoft.EntityFrameworkCore;

public partial class EntityFrameworkQueryableExtensions
{
public int QueryableToList(global::Microsoft.EntityFrameworkCore.DbContext dbContext)
{
var dbSet = dbContext.Set<object>();
var result = global::System.Linq.Enumerable.ToList(dbSet);
return result.Count;
}
}
}
Loading