Skip to content

Commit

Permalink
fix: add fully qualified Names for method parameters, return types, n…
Browse files Browse the repository at this point in the history
…ew constructor call and generic types (#318)
  • Loading branch information
chIsmaeel authored Apr 13, 2023
1 parent c092049 commit 36830e8
Show file tree
Hide file tree
Showing 122 changed files with 1,192 additions and 1,177 deletions.
3 changes: 2 additions & 1 deletion src/Riok.Mapperly/Descriptors/Mappings/ArrayCloneMapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand All @@ -22,7 +23,7 @@ public ArrayCloneMapping(
public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
return CastExpression(
IdentifierName(TargetType.ToDisplayString()),
FullyQualifiedIdentifier(TargetType),
InvocationExpression(MemberAccess(ctx.Source, CloneMethodName)));
}
}
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/Mappings/ArrayForMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
// var target = new T[source.Length];
var sourceLengthArrayRank = ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(MemberAccess(ctx.Source, ArrayLengthProperty)));
var targetInitializationValue = ArrayCreationExpression(
ArrayType(IdentifierName(_targetArrayElementType.ToDisplayString()))
ArrayType(FullyQualifiedIdentifier(_targetArrayElementType))
.WithRankSpecifiers(SingletonList(sourceLengthArrayRank)));
yield return DeclareLocalVariable(targetVariableName, targetInitializationValue);

Expand Down
4 changes: 3 additions & 1 deletion src/Riok.Mapperly/Descriptors/Mappings/CastMapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using Riok.Mapperly.Emit;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Riok.Mapperly.Descriptors.Mappings;
Expand All @@ -22,6 +24,6 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
var objToCast = _delegateMapping != null
? _delegateMapping.Build(ctx)
: ctx.Source;
return CastExpression(IdentifierName(TargetType.ToDisplayString()), objToCast);
return CastExpression(SyntaxFactoryHelper.FullyQualifiedIdentifier(TargetType), objToCast);
}
}
5 changes: 2 additions & 3 deletions src/Riok.Mapperly/Descriptors/Mappings/CtorMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Riok.Mapperly.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand All @@ -18,7 +17,7 @@ public CtorMapping(ITypeSymbol sourceType, ITypeSymbol targetType)

public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
{
var type = IdentifierName(TargetType.NonNullable().ToDisplayString());
return ObjectCreationExpression(type).WithArgumentList(ArgumentList(ctx.Source));
return ObjectCreationExpression(FullyQualifiedIdentifier(TargetType)).WithArgumentList(ArgumentList(ctx.Source));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
return GenericInvocation(
EnumClassName,
ParseMethodName,
new[] { IdentifierName(TargetType.ToDisplayString()) },
new[] { FullyQualifiedIdentifier(TargetType) },
ctx.Source,
BooleanLiteral(_ignoreCase));
}

// (TargetType)System.Enum.Parse(typeof(TargetType), source, ignoreCase)
var enumParseInvocation = Invocation(
MemberAccess(EnumClassName, ParseMethodName),
TypeOfExpression(IdentifierName(TargetType.ToDisplayString())), ctx.Source, BooleanLiteral(_ignoreCase));
return CastExpression(IdentifierName(TargetType.ToDisplayString()), enumParseInvocation);
TypeOfExpression(FullyQualifiedIdentifier(TargetType)), ctx.Source, BooleanLiteral(_ignoreCase));
return CastExpression(FullyQualifiedIdentifier(TargetType), enumParseInvocation);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using Riok.Mapperly.Helpers;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand Down Expand Up @@ -68,7 +70,7 @@ private SwitchExpressionArmSyntax BuildArmIgnoreCase(string ignoreCaseSwitchDesi

// source.Value1
var typeMemberAccess = MemberAccess(
IdentifierName(field.ContainingType.WithNullableAnnotation(NullableAnnotation.None).ToDisplayString()),
FullyQualifiedIdentifierName(field.ContainingType.NonNullable()),
field.Name);

// when s.Equals(nameof(source.Value1), StringComparison.OrdinalIgnoreCase)
Expand All @@ -87,7 +89,7 @@ private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field)
{
// nameof(source.Value1) => source.Value1;
var typeMemberAccess = MemberAccess(
IdentifierName(field.ContainingType.NonNullable().ToDisplayString()),
FullyQualifiedIdentifier(field.ContainingType),
field.Name);
var pattern = ConstantPattern(NameOf(typeMemberAccess));
return SwitchExpressionArm(pattern, typeMemberAccess);
Expand Down
4 changes: 2 additions & 2 deletions src/Riok.Mapperly/Descriptors/Mappings/EnumNameMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c

private SwitchExpressionArmSyntax BuildArm(KeyValuePair<string, string> sourceTargetField)
{
var sourceMember = MemberAccess(SourceType.ToDisplayString(), sourceTargetField.Key);
var targetMember = MemberAccess(TargetType.ToDisplayString(), sourceTargetField.Value);
var sourceMember = MemberAccess(FullyQualifiedIdentifier(SourceType), sourceTargetField.Key);
var targetMember = MemberAccess(FullyQualifiedIdentifier(TargetType), sourceTargetField.Value);
var pattern = ConstantPattern(sourceMember);
return SwitchExpressionArm(pattern, targetMember);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using Riok.Mapperly.Helpers;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.SyntaxFactoryHelper;

Expand Down Expand Up @@ -48,7 +50,7 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c
private SwitchExpressionArmSyntax BuildArm(IFieldSymbol field)
{
var typeMemberAccess = MemberAccess(
IdentifierName(field.ContainingType.NonNullable().ToDisplayString()),
FullyQualifiedIdentifier(field.ContainingType.NonNullable()),
field.Name);
var pattern = ConstantPattern(typeMemberAccess);
var nameOf = NameOf(typeMemberAccess);
Expand Down
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/Mappings/MethodMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public MethodDeclarationSyntax BuildMethod(SourceEmitterContext ctx)
{
TypeSyntax returnType = ReturnType == null
? PredefinedType(Token(SyntaxKind.VoidKeyword))
: IdentifierName(TargetType.ToDisplayString());
: FullyQualifiedIdentifier(TargetType);

var typeMappingBuildContext = new TypeMappingBuildContext(
SourceParameter.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
// if the target type is a nullable value type, there needs to be an additional cast in some cases
// (eg. in a linq expression, int => int?)
return TargetType.IsNullableValueType()
? CastExpression(IdentifierName(TargetType.ToDisplayString()), _delegateMapping.Build(ctx))
? CastExpression(FullyQualifiedIdentifier(TargetType), _delegateMapping.Build(ctx))
: _delegateMapping.Build(ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
if (Method.ReceiverType?.TypeKind != TypeKind.Interface)
return Invocation(Method.Name, _sourceParameter.WithArgument(ctx.Source), _referenceHandlerParameter?.WithArgument(ctx.ReferenceHandler));

var castedThis = CastExpression(IdentifierName(Method.ReceiverType!.ToDisplayString()), ThisExpression());
var castedThis = CastExpression(FullyQualifiedIdentifier(Method.ReceiverType!), ThisExpression());
var method = MemberAccess(ParenthesizedExpression(castedThis), Method.Name);
return Invocation(method, _sourceParameter.WithArgument(ctx.Source), _referenceHandlerParameter?.WithArgument(ctx.ReferenceHandler));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IfStatementSyntax TryGetReference(
// GetReference<TSource, TTarget>
var refHandler = ctx.ReferenceHandler ?? throw new ArgumentNullException(nameof(ctx.ReferenceHandler));
var methodName = GenericName(Identifier(nameof(IReferenceHandler.TryGetReference)))
.WithTypeArgumentList(TypeArgumentList(IdentifierName(mapping.SourceType.ToDisplayString()), IdentifierName(mapping.TargetType.ToDisplayString())));
.WithTypeArgumentList(TypeArgumentList(FullyQualifiedIdentifier(mapping.SourceType), FullyQualifiedIdentifier(mapping.TargetType)));
var method = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, refHandler, methodName);

// out var target
Expand Down Expand Up @@ -46,7 +46,7 @@ public static ExpressionSyntax SetReference(
// SetReference<TSource, TTarget>
var refHandler = ctx.ReferenceHandler ?? throw new ArgumentNullException(nameof(ctx.ReferenceHandler));
var methodName = GenericName(Identifier(nameof(IReferenceHandler.SetReference)))
.WithTypeArgumentList(TypeArgumentList(IdentifierName(mapping.SourceType.ToDisplayString()), IdentifierName(mapping.TargetType.ToDisplayString())));
.WithTypeArgumentList(TypeArgumentList(FullyQualifiedIdentifier(mapping.SourceType), (FullyQualifiedIdentifier(mapping.TargetType))));
var method = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, refHandler, methodName);

return Invocation(method, ctx.Source, target);
Expand Down
20 changes: 14 additions & 6 deletions src/Riok.Mapperly/Emit/SyntaxFactoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using Riok.Mapperly.Descriptors.Mappings;
using Riok.Mapperly.Helpers;
using Riok.Mapperly.Symbols;

using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Riok.Mapperly.Emit;
Expand All @@ -17,7 +19,7 @@ public static class SyntaxFactoryHelper
private const string NullReferenceExceptionClassName = "System.NullReferenceException";

public static readonly IdentifierNameSyntax VarIdentifier = IdentifierName("var");

private static readonly SymbolDisplayFormat _fullyQualifiedNullableFormat = SymbolDisplayFormat.FullyQualifiedFormat.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
private static readonly IdentifierNameSyntax _nameofIdentifier = IdentifierName("nameof");

public static SyntaxToken Accessibility(Accessibility accessibility)
Expand Down Expand Up @@ -103,8 +105,8 @@ public static LiteralExpressionSyntax DefaultLiteral()
public static LiteralExpressionSyntax NullLiteral()
=> LiteralExpression(SyntaxKind.NullLiteralExpression);

public static LiteralExpressionSyntax StringLiteral(string content) =>
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(content));
public static LiteralExpressionSyntax StringLiteral(string content)
=> LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(content));

public static LiteralExpressionSyntax BooleanLiteral(bool b)
=> LiteralExpression(b ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression);
Expand Down Expand Up @@ -215,7 +217,7 @@ public static ParameterListSyntax ParameterList(bool extensionMethod, params Met
public static ParameterSyntax Parameter(bool addThisKeyword, MethodParameter parameter)
{
var param = SyntaxFactory.Parameter(Identifier(parameter.Name))
.WithType(IdentifierName(parameter.Type.ToDisplayString()));
.WithType(FullyQualifiedIdentifier(parameter.Type));

if (addThisKeyword && parameter.Ordinal == 0)
{
Expand All @@ -234,7 +236,7 @@ public static InvocationExpressionSyntax StaticInvocation(string receiverType, s

public static InvocationExpressionSyntax StaticInvocation(IMethodSymbol method, params ExpressionSyntax[] arguments)
=> StaticInvocation(
method.ReceiverType?.NonNullable().ToDisplayString() ?? throw new ArgumentNullException(nameof(method.ReceiverType)),
FullyQualifiedIdentifierName(method.ReceiverType?.NonNullable()!) ?? throw new ArgumentNullException(nameof(method.ReceiverType)),
method.Name,
arguments);

Expand Down Expand Up @@ -323,7 +325,13 @@ public static SeparatedSyntaxList<T> CommaSeparatedList<T>(IEnumerable<T> nodes,
=> SeparatedList<T>(JoinByComma(nodes, insertTrailingComma));

public static IdentifierNameSyntax NonNullableIdentifier(ITypeSymbol t)
=> IdentifierName(t.NonNullable().ToDisplayString());
=> FullyQualifiedIdentifier(t.NonNullable());

public static IdentifierNameSyntax FullyQualifiedIdentifier(ITypeSymbol typeSymbol)
=> IdentifierName(FullyQualifiedIdentifierName(typeSymbol));

public static string FullyQualifiedIdentifierName(ITypeSymbol typeSymbol)
=> typeSymbol.ToDisplayString(_fullyQualifiedNullableFormat);

private static IEnumerable<SyntaxNodeOrToken> JoinByComma(IEnumerable<SyntaxNode> nodes, bool insertTrailingComma = false)
=> Join(Token(SyntaxKind.CommaToken), insertTrailingComma, nodes);
Expand Down
1 change: 1 addition & 0 deletions src/Riok.Mapperly/Helpers/SymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ namespace Riok.Mapperly.IntegrationTests.Mapper
{
public static partial class CircularReferenceMapper
{
public static partial Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto ToDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject obj)
public static partial global::Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto ToDto(global::Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject obj)
{
return MapToCircularReferenceDto(obj, new Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler());
return MapToCircularReferenceDto(obj, new global::Riok.Mapperly.Abstractions.ReferenceHandling.Internal.PreserveReferenceHandler());
}

private static Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto MapToCircularReferenceDto(Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject source, Riok.Mapperly.Abstractions.ReferenceHandling.IReferenceHandler refHandler)
private static global::Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto MapToCircularReferenceDto(global::Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject source, global::Riok.Mapperly.Abstractions.ReferenceHandling.IReferenceHandler refHandler)
{
if (refHandler.TryGetReference<Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, out var existingTargetReference))
if (refHandler.TryGetReference<global::Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, global::Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, out var existingTargetReference))
return existingTargetReference;
var target = new Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto();
refHandler.SetReference<Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, target);
var target = new global::Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto();
refHandler.SetReference<global::Riok.Mapperly.IntegrationTests.Models.CircularReferenceObject, global::Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto>(source, target);
if (source.Parent != null)
{
target.Parent = MapToCircularReferenceDto(source.Parent, refHandler);
Expand All @@ -23,4 +23,4 @@ private static Riok.Mapperly.IntegrationTests.Dto.CircularReferenceDto MapToCirc
return target;
}
}
}
}
Loading

0 comments on commit 36830e8

Please sign in to comment.