Skip to content

Commit 7dcd777

Browse files
author
fabien.menager
committed
Code cleanup
1 parent 49aed4a commit 7dcd777

4 files changed

Lines changed: 18 additions & 60 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
44
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
55
<LangVersion>12.0</LangVersion>
6-
<LangVersion Condition="'$(TargetFramework)' == 'net10.0'">preview</LangVersion>
6+
<LangVersion Condition="'$(TargetFramework)' == 'net10.0'">14.0</LangVersion>
77
<Nullable>enable</Nullable>
88
<EnableNETAnalyzers>true</EnableNETAnalyzers>
99
<NoWarn>CS1591</NoWarn>

src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
287287
if (_extensionParameterName is not null && node.Identifier.Text == _extensionParameterName)
288288
{
289289
var symbol = _semanticModel.GetSymbolInfo(node).Symbol;
290+
290291
// Check if this identifier refers to the extension parameter
291-
if (symbol is IParameterSymbol paramSymbol &&
292-
paramSymbol.ContainingSymbol is INamedTypeSymbol { IsExtension: true })
292+
if (symbol is IParameterSymbol { ContainingSymbol: INamedTypeSymbol { IsExtension: true } })
293293
{
294294
return SyntaxFactory.IdentifierName("@this")
295295
.WithLeadingTrivia(node.GetLeadingTrivia())

src/EntityFrameworkCore.Projectables.Generator/ProjectableInterpreter.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ static IEnumerable<string> GetNestedInClassPathForExtensionMember(ITypeSymbol ex
3131
// For extension members, the ContainingType is the extension block,
3232
// and its ContainingType is the outer class (e.g., EntityExtensions)
3333
var outerType = extensionType.ContainingType;
34+
3435
if (outerType is not null)
3536
{
3637
return GetNestedInClassPath(outerType);
3738
}
38-
return Enumerable.Empty<string>();
39+
40+
return [];
3941
}
4042

4143
public static ProjectableDescriptor? GetDescriptor(Compilation compilation, MemberDeclarationSyntax member, SourceProductionContext context)
@@ -132,11 +134,11 @@ x is IPropertySymbol xProperty &&
132134
}
133135

134136
// Check if this member is inside a C# 14 extension block
135-
var isExtensionMember = memberSymbol.ContainingType is INamedTypeSymbol { IsExtension: true };
137+
var isExtensionMember = memberSymbol.ContainingType is { IsExtension: true };
136138
IParameterSymbol? extensionParameter = null;
137139
ITypeSymbol? extensionReceiverType = null;
138140

139-
if (isExtensionMember && memberSymbol.ContainingType is INamedTypeSymbol extensionType)
141+
if (isExtensionMember && memberSymbol.ContainingType is { } extensionType)
140142
{
141143
extensionParameter = extensionType.ExtensionParameter;
142144
extensionReceiverType = extensionParameter?.Type;
@@ -160,8 +162,8 @@ x is IPropertySymbol xProperty &&
160162
? memberSymbol.ContainingType.ContainingType
161163
: memberSymbol.ContainingType;
162164

163-
var descriptor = new ProjectableDescriptor {
164-
165+
var descriptor = new ProjectableDescriptor
166+
{
165167
UsingDirectives = member.SyntaxTree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>(),
166168
ClassName = classForNaming.Name,
167169
ClassNamespace = classForNaming.ContainingNamespace.IsGlobalNamespace ? null : classForNaming.ContainingNamespace.ToDisplayString(),
@@ -172,11 +174,11 @@ x is IPropertySymbol xProperty &&
172174
ParametersList = SyntaxFactory.ParameterList()
173175
};
174176

175-
if (classForNaming is INamedTypeSymbol { IsGenericType: true } containingNamedType)
177+
if (classForNaming is { IsGenericType: true })
176178
{
177179
descriptor.ClassTypeParameterList = SyntaxFactory.TypeParameterList();
178180

179-
foreach (var additionalClassTypeParameter in containingNamedType.TypeParameters)
181+
foreach (var additionalClassTypeParameter in classForNaming.TypeParameters)
180182
{
181183
descriptor.ClassTypeParameterList = descriptor.ClassTypeParameterList.AddParameters(
182184
SyntaxFactory.TypeParameter(additionalClassTypeParameter.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,11 +1902,11 @@ public Task GenericTypesWithConstraints()
19021902
return Verifier.Verify(result.GeneratedTrees[0].ToString());
19031903
}
19041904

1905-
#if NET10_0
1905+
#if NET10_0_OR_GREATER
19061906
[Fact]
19071907
public Task ExtensionMemberProperty()
19081908
{
1909-
var compilation = CreateCompilationWithPreviewLanguageVersion(@"
1909+
var compilation = CreateCompilation(@"
19101910
using System;
19111911
using EntityFrameworkCore.Projectables;
19121912
@@ -1935,7 +1935,7 @@ static class EntityExtensions {
19351935
[Fact]
19361936
public Task ExtensionMemberMethod()
19371937
{
1938-
var compilation = CreateCompilationWithPreviewLanguageVersion(@"
1938+
var compilation = CreateCompilation(@"
19391939
using System;
19401940
using EntityFrameworkCore.Projectables;
19411941
@@ -1964,7 +1964,7 @@ static class EntityExtensions {
19641964
[Fact]
19651965
public Task ExtensionMemberMethodWithParameters()
19661966
{
1967-
var compilation = CreateCompilationWithPreviewLanguageVersion(@"
1967+
var compilation = CreateCompilation(@"
19681968
using System;
19691969
using EntityFrameworkCore.Projectables;
19701970
@@ -1993,7 +1993,7 @@ static class EntityExtensions {
19931993
[Fact]
19941994
public Task ExtensionMemberOnPrimitive()
19951995
{
1996-
var compilation = CreateCompilationWithPreviewLanguageVersion(@"
1996+
var compilation = CreateCompilation(@"
19971997
using System;
19981998
using EntityFrameworkCore.Projectables;
19991999
@@ -2016,7 +2016,7 @@ static class IntExtensions {
20162016
[Fact]
20172017
public Task ExtensionMemberWithMemberAccess()
20182018
{
2019-
var compilation = CreateCompilationWithPreviewLanguageVersion(@"
2019+
var compilation = CreateCompilation(@"
20202020
using System;
20212021
using EntityFrameworkCore.Projectables;
20222022
@@ -2091,50 +2091,6 @@ Compilation CreateCompilation(string source, bool expectedToCompile = true)
20912091
return compilation;
20922092
}
20932093

2094-
#if NET10_0
2095-
/// <summary>
2096-
/// Creates a compilation with preview language version to support C# 14 features like extension members.
2097-
/// </summary>
2098-
Compilation CreateCompilationWithPreviewLanguageVersion(string source, bool expectedToCompile = true)
2099-
{
2100-
var references = Basic.Reference.Assemblies.Net100.References.All.ToList();
2101-
2102-
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
2103-
2104-
var parseOptions = new CSharpParseOptions(LanguageVersion.Preview);
2105-
2106-
var compilation = CSharpCompilation.Create("compilation",
2107-
new[] { CSharpSyntaxTree.ParseText(source, parseOptions) },
2108-
references,
2109-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
2110-
2111-
#if DEBUG
2112-
2113-
if (expectedToCompile)
2114-
{
2115-
var compilationDiagnostics = compilation.GetDiagnostics();
2116-
2117-
if (!compilationDiagnostics.IsEmpty)
2118-
{
2119-
_testOutputHelper.WriteLine($"Original compilation diagnostics produced:");
2120-
2121-
foreach (var diagnostic in compilationDiagnostics)
2122-
{
2123-
_testOutputHelper.WriteLine($" > " + diagnostic.ToString());
2124-
}
2125-
2126-
if (compilationDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
2127-
{
2128-
Debug.Fail("Compilation diagnostics produced");
2129-
}
2130-
}
2131-
}
2132-
#endif
2133-
2134-
return compilation;
2135-
}
2136-
#endif
2137-
21382094
private GeneratorDriverRunResult RunGenerator(Compilation compilation)
21392095
{
21402096
_testOutputHelper.WriteLine("Running generator and updating compilation...");

0 commit comments

Comments
 (0)