Ensure that parameters are written with their fully qualified type name

This commit is contained in:
Koen Bekkenutte
2021-10-26 19:16:26 +08:00
parent fe2750e49f
commit 14a8f1ebd2
7 changed files with 19 additions and 71 deletions

View File

@@ -6,11 +6,11 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace EntityFrameworkCore.Projectables.Generator
{
public class ParameterSyntaxRewriter : CSharpSyntaxRewriter
public class DeclarationSyntaxRewriter : CSharpSyntaxRewriter
{
readonly SemanticModel _semanticModel;
public ParameterSyntaxRewriter(SemanticModel semanticModel)
public DeclarationSyntaxRewriter(SemanticModel semanticModel)
{
_semanticModel = semanticModel;
}
@@ -19,11 +19,12 @@ namespace EntityFrameworkCore.Projectables.Generator
{
var visitedNode = base.VisitIdentifierName(node);
var symbol = _semanticModel.GetDeclaredSymbol(visitedNode);
var symbolInfo = _semanticModel.GetSymbolInfo(visitedNode);
if (symbol is not null)
if (symbolInfo.Symbol is not null)
{
return SyntaxFactory.IdentifierName(symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
return SyntaxFactory.IdentifierName(symbolInfo.Symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat))
.WithTriviaFrom(node);
}
return visitedNode;
@@ -53,8 +54,7 @@ namespace EntityFrameworkCore.Projectables.Generator
if (typeInfo.Type.TypeKind is not TypeKind.Struct)
{
return Visit(node.ElementType)
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
.WithTriviaFrom(node);
}
}

View File

@@ -52,8 +52,7 @@ namespace EntityFrameworkCore.Projectables.Generator
.FirstOrDefault();
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel, nullConditionalRewriteSupport, context);
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel);
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
var declarationSyntaxRewriter = new DeclarationSyntaxRewriter(semanticModel);
var descriptor = new ProjectableDescriptor {
ClassName = memberSymbol.ContainingType.Name,
@@ -100,11 +99,11 @@ namespace EntityFrameworkCore.Projectables.Generator
return null;
}
var returnType = returnTypeSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType);
var returnType = declarationSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType);
descriptor.ReturnTypeName = returnType.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression);
foreach (var additionalParameter in ((ParameterListSyntax)parameterSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
foreach (var additionalParameter in ((ParameterListSyntax)declarationSyntaxRewriter.Visit(methodDeclarationSyntax.ParameterList)).Parameters)
{
descriptor.ParametersList = descriptor.ParametersList.AddParameters(additionalParameter);
}
@@ -118,7 +117,7 @@ namespace EntityFrameworkCore.Projectables.Generator
return null;
}
var returnType = returnTypeSyntaxRewriter.Visit(propertyDeclarationSyntax.Type);
var returnType = declarationSyntaxRewriter.Visit(propertyDeclarationSyntax.Type);
descriptor.ReturnTypeName = returnType.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);

View File

@@ -1,51 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace EntityFrameworkCore.Projectables.Generator
{
public class ReturnTypeSyntaxRewriter : CSharpSyntaxRewriter
{
readonly SemanticModel _semanticModel;
public ReturnTypeSyntaxRewriter(SemanticModel semanticModel)
{
_semanticModel = semanticModel;
}
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
var visitedNode = base.VisitIdentifierName(node);
var symbolInfo = _semanticModel.GetSymbolInfo(visitedNode);
if (symbolInfo.Symbol is not null)
{
return SyntaxFactory.IdentifierName(symbolInfo.Symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
}
return visitedNode;
}
public override SyntaxNode? VisitNullableType(NullableTypeSyntax node)
{
var typeInfo = _semanticModel.GetTypeInfo(node);
if (typeInfo.Type is not null)
{
if (typeInfo.Type.TypeKind is not TypeKind.Struct)
{
return Visit(node.ElementType)
.WithLeadingTrivia(node.GetLeadingTrivia())
.WithTrailingTrivia(node.GetTrailingTrivia());
}
}
return base.VisitNullableType(node);
}
}
}

View File

@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
{
public static System.Linq.Expressions.Expression<System.Func<Entity, Entity>> Expression =>
(Entity entity) => entity.RelatedEntities[0];
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression =>
(global::Foo.EntityExtensions.Entity entity) => entity.RelatedEntities[0];
}
}

View File

@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
{
public static System.Linq.Expressions.Expression<System.Func<Entity, Entity>> Expression =>
(Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null;
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression =>
(global::Foo.EntityExtensions.Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null;
}
}

View File

@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
public static class Foo_EntityExtensions_GetFirstName
{
public static System.Linq.Expressions.Expression<System.Func<Entity, string>> Expression =>
(Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ') ) : null?? 0)) : null;
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, string>> Expression =>
(global::Foo.EntityExtensions.Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ') ) : null?? 0)) : null;
}
}

View File

@@ -8,7 +8,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<D, int>> Expression =>
(D d) => 1;
public static System.Linq.Expressions.Expression<System.Func<global::Foo.D, int>> Expression =>
(global::Foo.D d) => 1;
}
}