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
@@ -6,11 +6,11 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace EntityFrameworkCore.Projectables.Generator namespace EntityFrameworkCore.Projectables.Generator
{ {
public class ParameterSyntaxRewriter : CSharpSyntaxRewriter public class DeclarationSyntaxRewriter : CSharpSyntaxRewriter
{ {
readonly SemanticModel _semanticModel; readonly SemanticModel _semanticModel;
public ParameterSyntaxRewriter(SemanticModel semanticModel) public DeclarationSyntaxRewriter(SemanticModel semanticModel)
{ {
_semanticModel = semanticModel; _semanticModel = semanticModel;
} }
@@ -19,11 +19,12 @@ namespace EntityFrameworkCore.Projectables.Generator
{ {
var visitedNode = base.VisitIdentifierName(node); 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; return visitedNode;
@@ -53,8 +54,7 @@ namespace EntityFrameworkCore.Projectables.Generator
if (typeInfo.Type.TypeKind is not TypeKind.Struct) if (typeInfo.Type.TypeKind is not TypeKind.Struct)
{ {
return Visit(node.ElementType) return Visit(node.ElementType)
.WithLeadingTrivia(node.GetLeadingTrivia()) .WithTriviaFrom(node);
.WithTrailingTrivia(node.GetTrailingTrivia());
} }
} }
@@ -52,8 +52,7 @@ namespace EntityFrameworkCore.Projectables.Generator
.FirstOrDefault(); .FirstOrDefault();
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel, nullConditionalRewriteSupport, context); var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel, nullConditionalRewriteSupport, context);
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel); var declarationSyntaxRewriter = new DeclarationSyntaxRewriter(semanticModel);
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
var descriptor = new ProjectableDescriptor { var descriptor = new ProjectableDescriptor {
ClassName = memberSymbol.ContainingType.Name, ClassName = memberSymbol.ContainingType.Name,
@@ -100,11 +99,11 @@ namespace EntityFrameworkCore.Projectables.Generator
return null; return null;
} }
var returnType = returnTypeSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType); var returnType = declarationSyntaxRewriter.Visit(methodDeclarationSyntax.ReturnType);
descriptor.ReturnTypeName = returnType.ToString(); descriptor.ReturnTypeName = returnType.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(methodDeclarationSyntax.ExpressionBody.Expression); 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); descriptor.ParametersList = descriptor.ParametersList.AddParameters(additionalParameter);
} }
@@ -118,7 +117,7 @@ namespace EntityFrameworkCore.Projectables.Generator
return null; return null;
} }
var returnType = returnTypeSyntaxRewriter.Visit(propertyDeclarationSyntax.Type); var returnType = declarationSyntaxRewriter.Visit(propertyDeclarationSyntax.Type);
descriptor.ReturnTypeName = returnType.ToString(); descriptor.ReturnTypeName = returnType.ToString();
descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression); descriptor.Body = expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);
@@ -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);
}
}
}
@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{ {
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
{ {
public static System.Linq.Expressions.Expression<System.Func<Entity, Entity>> Expression => public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression =>
(Entity entity) => entity.RelatedEntities[0]; (global::Foo.EntityExtensions.Entity entity) => entity.RelatedEntities[0];
} }
} }
@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{ {
public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls public static class Foo_EntityExtensions_GetFirstRelatedIgnoreNulls
{ {
public static System.Linq.Expressions.Expression<System.Func<Entity, Entity>> Expression => public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression =>
(Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null; (global::Foo.EntityExtensions.Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : null) : null;
} }
} }
@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{ {
public static class Foo_EntityExtensions_GetFirstName public static class Foo_EntityExtensions_GetFirstName
{ {
public static System.Linq.Expressions.Expression<System.Func<Entity, string>> Expression => public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, string>> Expression =>
(Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ') ) : null?? 0)) : null; (global::Foo.EntityExtensions.Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ') ) : null?? 0)) : null;
} }
} }
@@ -8,7 +8,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{ {
public static class Foo_C_Foo public static class Foo_C_Foo
{ {
public static System.Linq.Expressions.Expression<System.Func<D, int>> Expression => public static System.Linq.Expressions.Expression<System.Func<global::Foo.D, int>> Expression =>
(D d) => 1; (global::Foo.D d) => 1;
} }
} }