Skip to content
Open
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
13 changes: 5 additions & 8 deletions src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,13 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
return node;
}

public override SyntaxNode? VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
public override SyntaxNode VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
{
// the lambda symbol keeps the nullable annotation of an explicit return type,
// the type info of the return type syntax does not.
var returnType = node.ReturnType == null ? null : (semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol)?.ReturnType;
node = (ParenthesizedLambdaExpressionSyntax)base.VisitParenthesizedLambdaExpression(node)!;
if (returnType == null || node.ReturnType == null)
return node;
var rewritten = (ParenthesizedLambdaExpressionSyntax)base.VisitParenthesizedLambdaExpression(node)!;
if (node.ReturnType == null || semanticModel.GetSymbolInfo(node).Symbol is not IMethodSymbol lambda)
return rewritten;

return node.WithReturnType(FullyQualifiedIdentifier(returnType).WithTriviaFrom(node.ReturnType));
return rewritten.WithReturnType(FullyQualifiedIdentifier(lambda.ReturnType).WithTriviaFrom(node.ReturnType));
}

public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, UserDefinedExpres
var expressionMapping = new ExpressionMapping(
sourceType,
mapping.TargetType, // The return type (Expression<Func<TSource, TTarget>>)
delegateMapping,
ctx.Configuration.SupportedFeatures.NullableAttributes
delegateMapping
);
mapping.SetDelegateMapping(expressionMapping);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public static class QueryableMappingBuilder
if (mapping == null)
return null;

return new QueryableProjectionMapping(ctx.Source, ctx.Target, mapping, ctx.Configuration.SupportedFeatures.NullableAttributes);
return new QueryableProjectionMapping(ctx.Source, ctx.Target, mapping);
}
}
23 changes: 6 additions & 17 deletions src/Riok.Mapperly/Descriptors/Mappings/ExpressionMapping.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq.Expressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper;

namespace Riok.Mapperly.Descriptors.Mappings;
Expand All @@ -10,30 +9,20 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// An expression mapping that returns an <see cref="Expression{TDelegate}"/>
/// where TDelegate is a <see cref="Func{TSource, TTarget}"/>.
/// </summary>
public class ExpressionMapping(
ITypeSymbol sourceType,
ITypeSymbol targetType,
INewInstanceMapping delegateMapping,
bool supportsNullableAttributes
) : NewInstanceMethodMapping(sourceType, targetType)
public class ExpressionMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping delegateMapping)
: NewInstanceMethodMapping(sourceType, targetType)
{
public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext ctx)
{
// disable nullable reference types for expressions, as for ORMs nullables usually don't apply
// #nullable disable
// disable nullable reference type warnings for expressions, as for ORMs nullables usually don't apply
// #nullable disable warnings
// return (TSource x) => new TTarget { ... };
// #nullable enable
// #nullable enable warnings
var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource();

var delegateMappingSyntax = delegateMapping.Build(lambdaCtx);
var projectionLambda = Lambda(lambdaSourceName, delegateMappingSyntax);
var returnStatement = ctx.SyntaxFactory.Return(projectionLambda);
var leadingTrivia = returnStatement.GetLeadingTrivia().Insert(0, ElasticCarriageReturnLineFeed).Insert(1, Nullable(false));
var trailingTrivia = returnStatement
.GetTrailingTrivia()
.Insert(0, ElasticCarriageReturnLineFeed)
.Insert(1, Nullable(true, !supportsNullableAttributes));
returnStatement = returnStatement.WithLeadingTrivia(leadingTrivia).WithTrailingTrivia(trailingTrivia);
return [returnStatement];
return [ctx.SyntaxFactory.DisableNullableWarnings(returnStatement)];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Riok.Mapperly.Emit.Syntax.SyntaxFactoryHelper;

namespace Riok.Mapperly.Descriptors.Mappings;
Expand All @@ -9,34 +8,24 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// A projections queryable mapping
/// to map from one generic <see cref="IQueryable{T}"/> to another.
/// </summary>
public class QueryableProjectionMapping(
ITypeSymbol sourceType,
ITypeSymbol targetType,
INewInstanceMapping delegateMapping,
bool supportsNullableAttributes
) : NewInstanceMethodMapping(sourceType, targetType)
public class QueryableProjectionMapping(ITypeSymbol sourceType, ITypeSymbol targetType, INewInstanceMapping delegateMapping)
: NewInstanceMethodMapping(sourceType, targetType)
{
private const string QueryableReceiverName = "global::System.Linq.Queryable";
private const string SelectMethodName = nameof(Queryable.Select);

public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext ctx)
{
// disable nullable reference types for expressions, as for ORMs nullables usually don't apply
// #nullable disable
// return System.Linq.Enumerable.Select(source, x => ...);
// #nullable enable
// disable nullable reference type warnings for expressions, as for ORMs nullables usually don't apply
// #nullable disable warnings
// return global::System.Linq.Queryable.Select(source, x => ...);
// #nullable enable warnings
var (lambdaCtx, lambdaSourceName) = ctx.WithNewScopedSource();

var delegateMappingSyntax = delegateMapping.Build(lambdaCtx);
var projectionLambda = Lambda(lambdaSourceName, delegateMappingSyntax);
var select = ctx.SyntaxFactory.StaticInvocation(QueryableReceiverName, SelectMethodName, ctx.Source, projectionLambda);
var returnStatement = ctx.SyntaxFactory.Return(select);
var leadingTrivia = returnStatement.GetLeadingTrivia().Insert(0, ElasticCarriageReturnLineFeed).Insert(1, Nullable(false));
var trailingTrivia = returnStatement
.GetTrailingTrivia()
.Insert(0, ElasticCarriageReturnLineFeed)
.Insert(1, Nullable(true, !supportsNullableAttributes));
returnStatement = returnStatement.WithLeadingTrivia(leadingTrivia).WithTrailingTrivia(trailingTrivia);
return [returnStatement];
return [ctx.SyntaxFactory.DisableNullableWarnings(returnStatement)];
}
}
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Emit/SourceEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static CompilationUnitSyntax Build(MapperDescriptor descriptor, Cancellat
.WithLeadingTrivia(
Comment(AutoGeneratedComment),
ElasticCarriageReturnLineFeed,
Nullable(true, !descriptor.SupportedFeatures.NullableAttributes),
Nullable(true, descriptor.SupportedFeatures.NullableAttributes ? NullableTarget.All : NullableTarget.Annotations),
ElasticCarriageReturnLineFeed
);
}
Expand Down
33 changes: 29 additions & 4 deletions src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace Riok.Mapperly.Emit.Syntax;

public partial struct SyntaxFactoryHelper
{
public enum NullableTarget
{
All,
Annotations,
Warnings,
}

public static AssignmentExpressionSyntax CoalesceAssignment(ExpressionSyntax target, ExpressionSyntax source)
{
return AssignmentExpression(
Expand All @@ -19,21 +26,39 @@ public static AssignmentExpressionSyntax CoalesceAssignment(ExpressionSyntax tar
);
}

public static SyntaxTrivia Nullable(bool enabled, bool annotationsOnly = false)
public static SyntaxTrivia Nullable(bool enabled, NullableTarget target = NullableTarget.All)
{
var nullableDirective = NullableDirectiveTrivia(
LeadingSpacedToken(enabled ? SyntaxKind.EnableKeyword : SyntaxKind.DisableKeyword),
true
);

if (annotationsOnly)
nullableDirective = target switch
{
nullableDirective = nullableDirective.WithTargetToken(LeadingSpacedToken(SyntaxKind.AnnotationsKeyword));
}
NullableTarget.Annotations => nullableDirective.WithTargetToken(LeadingSpacedToken(SyntaxKind.AnnotationsKeyword)),
NullableTarget.Warnings => nullableDirective.WithTargetToken(LeadingSpacedToken(SyntaxKind.WarningsKeyword)),
_ => nullableDirective,
};

return Trivia(nullableDirective);
}

public StatementSyntax DisableNullableWarnings(StatementSyntax statement)
{
if (!supportedFeatures.NullableAttributes)
return statement;

var leadingTrivia = statement
.GetLeadingTrivia()
.Insert(0, ElasticCarriageReturnLineFeed)
.Insert(1, Nullable(false, NullableTarget.Warnings));
var trailingTrivia = statement
.GetTrailingTrivia()
.Insert(0, ElasticCarriageReturnLineFeed)
.Insert(1, Nullable(true, NullableTarget.Warnings));
return statement.WithLeadingTrivia(leadingTrivia).WithTrailingTrivia(trailingTrivia);
}

public static BinaryExpressionSyntax Coalesce(ExpressionSyntax expr, ExpressionSyntax coalesceExpr) =>
BinaryExpression(SyntaxKind.CoalesceExpression, expr, coalesceExpr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static partial class ProjectionMapper
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection> ProjectToDto(this global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection> q)
{
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(
q,
x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue)
Expand Down Expand Up @@ -64,13 +64,13 @@ public static partial class ProjectionMapper
})),
}
);
#nullable enable
#nullable enable warnings
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType> ProjectToDto(this global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionBaseType> q)
{
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(
q,
x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA()
Expand All @@ -85,13 +85,13 @@ public static partial class ProjectionMapper
BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue,
} : default)
);
#nullable enable
#nullable enable warnings
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.Expressions.Expression<global::System.Func<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection>> ProjectToDtoExpression()
{
#nullable disable
#nullable disable warnings
return x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue)
{
IntValue = x.IntValue,
Expand Down Expand Up @@ -145,7 +145,7 @@ public static partial class ProjectionMapper
Value = x1.Value,
})),
};
#nullable enable
#nullable enable warnings
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public static partial class ProjectionMapper
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection> ProjectToDto(this global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection> q)
{
#nullable disable
return global::System.Linq.Queryable.Select(
q,
x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue)
Expand Down Expand Up @@ -68,13 +67,11 @@ public static partial class ProjectionMapper
})),
}
);
#nullable enable annotations
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType> ProjectToDto(this global::System.Linq.IQueryable<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionBaseType> q)
{
#nullable disable
return global::System.Linq.Queryable.Select(
q,
x => (global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionBaseType)(x is global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeA ? new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjectionTypeA()
Expand All @@ -89,13 +86,11 @@ public static partial class ProjectionMapper
BaseValue = ((global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjectionTypeB)x).BaseValue,
} : default)
);
#nullable enable annotations
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial global::System.Linq.Expressions.Expression<global::System.Func<global::Riok.Mapperly.IntegrationTests.Models.TestObjectProjection, global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection>> ProjectToDtoExpression()
{
#nullable disable
return x => new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoProjection(x.CtorValue)
{
IntValue = x.IntValue,
Expand Down Expand Up @@ -153,7 +148,6 @@ public static partial class ProjectionMapper
Value = x1.Value,
})),
};
#nullable enable annotations
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
Expand Down
8 changes: 4 additions & 4 deletions test/Riok.Mapperly.Tests/Mapping/ExpressionMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public void TopLevelEnumByNameWithMethodNoExpressionInliningShouldInvoke()
.Should()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return x => MapEnum(x);
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down Expand Up @@ -54,9 +54,9 @@ TestSourceBuilderOptions.Default with
.Should()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return x => MapEnum(x);
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down
20 changes: 10 additions & 10 deletions test/Riok.Mapperly.Tests/Mapping/QueryableProjectionEnumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public void EnumToAnotherEnumWithDifferentUnderlyingType()
.Should()
.HaveSingleMethodBody(
"""
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(
source,
x => new global::B()
{
Value = (global::D)x.Value,
}
);
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down Expand Up @@ -115,15 +115,15 @@ TestSourceBuilderOptions.Default with
.HaveAssertedAllDiagnostics()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(
q,
x => new global::B()
{
Value = MapEnum(x.Value),
}
);
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down Expand Up @@ -153,15 +153,15 @@ TestSourceBuilderOptions.Default with
.Should()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(
q,
x => new global::B()
{
Value = MapEnum(x.Value),
}
);
#nullable enable
#nullable enable warnings
"""
);
}
Expand All @@ -186,9 +186,9 @@ public void TopLevelEnumByNameWithMethodNoExpressionInliningShouldInvoke()
.Should()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(q, x => MapEnum(x));
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down Expand Up @@ -216,9 +216,9 @@ TestSourceBuilderOptions.Default with
.Should()
.HaveMapMethodBody(
"""
#nullable disable
#nullable disable warnings
return global::System.Linq.Queryable.Select(q, x => MapEnum(x));
#nullable enable
#nullable enable warnings
"""
);
}
Expand Down
Loading
Loading