Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#149 fix bug when map method not working on elvis operator #156

Merged
merged 2 commits into from
Jul 21, 2024
Merged
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
86 changes: 67 additions & 19 deletions NextGenMapper/CodeAnalysis/SourceCodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class SourceCodeAnalyzer
public static bool IsMapMethodInvocationSyntaxNode(SyntaxNode node)
=> node is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax
Expression: MemberAccessExpressionSyntax or MemberBindingExpressionSyntax
{
Name: GenericNameSyntax
{
Expand All @@ -24,7 +24,7 @@ public static bool IsMapMethodInvocationSyntaxNode(SyntaxNode node)
public static bool IsConfiguredMapMethodInvocationSynaxNode(SyntaxNode node)
=> node is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax
Expression: MemberAccessExpressionSyntax or MemberBindingExpressionSyntax
{
Name: GenericNameSyntax
{
Expand All @@ -36,7 +36,7 @@ public static bool IsConfiguredMapMethodInvocationSynaxNode(SyntaxNode node)
public static bool IsProjectionMethodInvocationSyntaxNode(SyntaxNode node)
=> node is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax
Expression: MemberAccessExpressionSyntax or MemberBindingExpressionSyntax
{
Name: GenericNameSyntax
{
Expand All @@ -48,7 +48,7 @@ public static bool IsProjectionMethodInvocationSyntaxNode(SyntaxNode node)
public static bool IsConfiguredProjectionMethodInvocationSyntaxNode(SyntaxNode node)
=> node is InvocationExpressionSyntax
{
Expression: MemberAccessExpressionSyntax
Expression: MemberAccessExpressionSyntax or MemberBindingExpressionSyntax
{
Name: GenericNameSyntax
{
Expand Down Expand Up @@ -88,14 +88,26 @@ public static MapMethodAnalysisResult AnalyzeMapMethod(
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (mapMethodInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression
&& semanticModel.GetSymbolInfo(memberAccessExpression, cancellationToken).Symbol is IMethodSymbol
var sourceTypeExpression = mapMethodInvocation.Expression switch
{
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression,
MemberBindingExpressionSyntax => (mapMethodInvocation.Parent as ConditionalAccessExpressionSyntax)?.Expression,
_ => null
};

if (sourceTypeExpression is null)
{
return MapMethodAnalysisResult.Fail();
}

if (mapMethodInvocation.Expression is MemberAccessExpressionSyntax or MemberBindingExpressionSyntax
&& semanticModel.GetSymbolInfo(mapMethodInvocation.Expression, cancellationToken).Symbol is IMethodSymbol
{
IsExtensionMethod: true,
MethodKind: MethodKind.ReducedExtension
} method
&& method.ReducedFrom?.ToDisplayString() == StartMapperSource.MapMethodFullName
&& semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken).Type is ITypeSymbol
&& semanticModel.GetTypeInfo(sourceTypeExpression, cancellationToken).Type is ITypeSymbol
{
TypeKind: not TypeKind.Error
} source
Expand All @@ -115,12 +127,19 @@ public static ConfiguredMapMethodAnalysisResult AnalyzeConfiguredMapMethod(
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (configuredMapMethodInvocation.Expression is not MemberAccessExpressionSyntax memberAccessExpression)
var sourceTypeExpression = configuredMapMethodInvocation.Expression switch
{
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression,
MemberBindingExpressionSyntax => (configuredMapMethodInvocation.Parent as ConditionalAccessExpressionSyntax)?.Expression,
_ => null
};

if (sourceTypeExpression is null)
{
return ConfiguredMapMethodAnalysisResult.Fail();
}

var invocationMethodSymbolInfo = semanticModel.GetSymbolInfo(memberAccessExpression, cancellationToken);
var invocationMethodSymbolInfo = semanticModel.GetSymbolInfo(configuredMapMethodInvocation.Expression, cancellationToken);
var invocationMethodSymbol = invocationMethodSymbolInfo.Symbol;

var isCompleteMethod = false;
Expand All @@ -142,7 +161,7 @@ public static ConfiguredMapMethodAnalysisResult AnalyzeConfiguredMapMethod(
&& method.IsExtensionMethod
&& method.MethodKind == MethodKind.ReducedExtension
&& method.ReducedFrom?.ToDisplayString() == StartMapperSource.MapWithMethodFullName
&& semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken).Type is ITypeSymbol { TypeKind: not TypeKind.Error } source
&& semanticModel.GetTypeInfo(sourceTypeExpression, cancellationToken).Type is ITypeSymbol { TypeKind: not TypeKind.Error } source
&& method.ReturnType is ITypeSymbol { TypeKind: not TypeKind.Error } destination)
{
return ConfiguredMapMethodAnalysisResult.Success(source, destination, isCompleteMethod);
Expand Down Expand Up @@ -180,16 +199,27 @@ public static bool IsProjectionWithNonGenericIQueryable(
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (mapMethodInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression
&& semanticModel.GetSymbolInfo(memberAccessExpression, cancellationToken).Symbol is IMethodSymbol
var sourceTypeExpression = mapMethodInvocation.Expression switch
{
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression,
MemberBindingExpressionSyntax => (mapMethodInvocation.Parent as ConditionalAccessExpressionSyntax)?.Expression,
_ => null
};

if (sourceTypeExpression is null)
{
return false;
}

if (semanticModel.GetSymbolInfo(mapMethodInvocation.Expression, cancellationToken).Symbol is IMethodSymbol
{
IsExtensionMethod: true,
MethodKind: MethodKind.ReducedExtension
} method
&& (method.ReducedFrom?.ToDisplayString()
is StartMapperSource.NonGenericIQueryableProjectionMethodFullName
or StartMapperSource.NonGenericIQueryableConfiguredProjectionMethodFullName)
&& semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken).Type is INamedTypeSymbol
&& semanticModel.GetTypeInfo(sourceTypeExpression, cancellationToken).Type is INamedTypeSymbol
{
TypeKind: not TypeKind.Error,
IsGenericType: false,
Expand All @@ -208,16 +238,27 @@ public static MapMethodAnalysisResult AnalyzeProjectionMethod(
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (mapMethodInvocation.Expression is MemberAccessExpressionSyntax memberAccessExpression
&& semanticModel.GetSymbolInfo(memberAccessExpression, cancellationToken).Symbol is IMethodSymbol
var sourceTypeExpression = mapMethodInvocation.Expression switch
{
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression,
MemberBindingExpressionSyntax => (mapMethodInvocation.Parent as ConditionalAccessExpressionSyntax)?.Expression,
_ => null
};

if (sourceTypeExpression is null)
{
return MapMethodAnalysisResult.Fail();
}

if (semanticModel.GetSymbolInfo(mapMethodInvocation.Expression, cancellationToken).Symbol is IMethodSymbol
{
IsExtensionMethod: true,
MethodKind: MethodKind.ReducedExtension
} method
&& (method.ReducedFrom?.ToDisplayString()
is StartMapperSource.ProjectionMethodFullName
or StartMapperSource.NonGenericIQueryableProjectionMethodFullName)
&& semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken).Type is INamedTypeSymbol
&& semanticModel.GetTypeInfo(sourceTypeExpression, cancellationToken).Type is INamedTypeSymbol
{
TypeKind: not TypeKind.Error,
IsGenericType: true,
Expand All @@ -239,12 +280,19 @@ public static ConfiguredMapMethodAnalysisResult AnalyzeConfiguredProjectionMetho
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (configuredMapMethodInvocation.Expression is not MemberAccessExpressionSyntax memberAccessExpression)
var sourceTypeExpression = configuredMapMethodInvocation.Expression switch
{
MemberAccessExpressionSyntax memberAccessExpression => memberAccessExpression.Expression,
MemberBindingExpressionSyntax => (configuredMapMethodInvocation.Parent as ConditionalAccessExpressionSyntax)?.Expression,
_ => null
};

if (sourceTypeExpression is null)
{
return ConfiguredMapMethodAnalysisResult.Fail();
}

var invocationMethodSymbolInfo = semanticModel.GetSymbolInfo(memberAccessExpression, cancellationToken);
var invocationMethodSymbolInfo = semanticModel.GetSymbolInfo(configuredMapMethodInvocation.Expression, cancellationToken);
var invocationMethodSymbol = invocationMethodSymbolInfo.Symbol;

var isCompleteMethod = false;
Expand All @@ -268,7 +316,7 @@ public static ConfiguredMapMethodAnalysisResult AnalyzeConfiguredProjectionMetho
&& (method.ReducedFrom?.ToDisplayString()
is StartMapperSource.ConfiguredProjectionMethodFullName
or StartMapperSource.NonGenericIQueryableConfiguredProjectionMethodFullName)
&& semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken).Type is INamedTypeSymbol
&& semanticModel.GetTypeInfo(sourceTypeExpression, cancellationToken).Type is INamedTypeSymbol
{
TypeKind: not TypeKind.Error,
IsGenericType: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
Diagnostics: [
{
Id: NGM040,
Title: Non generic IQueryable used for projection,
Severity: Error,
WarningLevel: 0,
Location: : (7,31)-(7,128),
Description: ,
HelpLink: ,
MessageFormat: For projection you must use only generic version of IQueryable. Non generic version is not supported,
Message: For projection you must use only generic version of IQueryable. Non generic version is not supported,
Category: NextGenMapper,
CustomTags: []
},
{
Id: NGM040,
Title: Non generic IQueryable used for projection,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//HintName: Mapper_ClassMaps.g.cs
#nullable enable
using NextGenMapper.Extensions;

namespace NextGenMapper
{
internal static partial class Mapper
{
internal static Test.Destination Map<To>(this Test.Source source) => new Test.Destination()
{
Property = source.Property
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
Property: 1
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
{
Diagnostics: [
{
Id: NGM040,
Title: Non generic IQueryable used for projection,
Severity: Error,
WarningLevel: 0,
Location: : (7,31)-(7,124),
Description: ,
HelpLink: ,
MessageFormat: For projection you must use only generic version of IQueryable. Non generic version is not supported,
Message: For projection you must use only generic version of IQueryable. Non generic version is not supported,
Category: NextGenMapper,
CustomTags: []
},
{
Id: NGM040,
Title: Non generic IQueryable used for projection,
Expand Down
32 changes: 32 additions & 0 deletions NextGenMapperTests/Tests/MapMethodInvocation/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,38 @@ public class Source
public int Property { get; set; } = 1;
}

public class Destination
{
public int Property { get; set; }
}
";

return VerifyAndRun(source);
}

[TestMethod]
public Task InvocateOnElvisOperator_ShouldMap()
{
var source =
@"#nullable enable
using NextGenMapper;

namespace Test;

public class Program
{
public object? RunTest()
{
Source? source = new Source();
return source?.Map<Destination>();
}
}

public class Source
{
public int Property { get; set; } = 1;
}

public class Destination
{
public int Property { get; set; }
Expand Down
Loading