From 7315c57950434bf9c23bb9e00a02c46d82a44148 Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Sat, 14 Oct 2023 13:51:33 +0100 Subject: [PATCH] feat: add diagnostic for nested ignores --- src/Riok.Mapperly/AnalyzerReleases.Shipped.md | 9 +++++++ .../MembersMappingBuilderContext.cs | 10 ++++++++ .../Diagnostics/DiagnosticDescriptors.cs | 18 ++++++++++++++ .../Mapping/ObjectPropertyIgnoreTest.cs | 24 +++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/src/Riok.Mapperly/AnalyzerReleases.Shipped.md b/src/Riok.Mapperly/AnalyzerReleases.Shipped.md index 05dfbeaad4..82ed09a6c4 100644 --- a/src/Riok.Mapperly/AnalyzerReleases.Shipped.md +++ b/src/Riok.Mapperly/AnalyzerReleases.Shipped.md @@ -116,3 +116,12 @@ Rule ID | Category | Severity | Notes RMG048 | Mapper | Error | Used mapper members cannot be nullable RMG049 | Mapper | Warning | Source member is ignored and also explicitly mapped RMG050 | Mapper | Warning | Target member is ignored and also explicitly mapped + +## Release 3.3 + +### New Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +RMG051 | Mapper | Warning | Invalid ignore source member found, nested ignores are not supported +RMG052 | Mapper | Warning | Invalid ignore target member found, nested ignores are not supported diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersMappingBuilderContext.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersMappingBuilderContext.cs index fe11e2cc21..a76785bbf0 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersMappingBuilderContext.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/MembersMappingBuilderContext.cs @@ -140,6 +140,11 @@ private void AddUnmatchedIgnoredTargetMembersDiagnostics() { foreach (var notFoundIgnoredMember in _ignoredUnmatchedTargetMemberNames) { + if (notFoundIgnoredMember.Contains(StringMemberPath.PropertyAccessSeparator, StringComparison.Ordinal)) + { + BuilderContext.ReportDiagnostic(DiagnosticDescriptors.NestedIgnoredTargetMember, notFoundIgnoredMember, Mapping.TargetType); + continue; + } BuilderContext.ReportDiagnostic(DiagnosticDescriptors.IgnoredTargetMemberNotFound, notFoundIgnoredMember, Mapping.TargetType); } } @@ -148,6 +153,11 @@ private void AddUnmatchedIgnoredSourceMembersDiagnostics() { foreach (var notFoundIgnoredMember in _ignoredUnmatchedSourceMemberNames) { + if (notFoundIgnoredMember.Contains(StringMemberPath.PropertyAccessSeparator, StringComparison.Ordinal)) + { + BuilderContext.ReportDiagnostic(DiagnosticDescriptors.NestedIgnoredSourceMember, notFoundIgnoredMember, Mapping.TargetType); + continue; + } BuilderContext.ReportDiagnostic(DiagnosticDescriptors.IgnoredSourceMemberNotFound, notFoundIgnoredMember, Mapping.SourceType); } } diff --git a/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs b/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs index 7d4d8ed160..c80826752f 100644 --- a/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs +++ b/src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs @@ -446,4 +446,22 @@ public static class DiagnosticDescriptors DiagnosticSeverity.Warning, true ); + + public static readonly DiagnosticDescriptor NestedIgnoredSourceMember = new DiagnosticDescriptor( + "RMG051", + "Invalid ignore source member found, nested ignores are not supported", + "Invalid ignore source member {0} found for type {1}, nested ignores are not supported", + DiagnosticCategories.Mapper, + DiagnosticSeverity.Warning, + true + ); + + public static readonly DiagnosticDescriptor NestedIgnoredTargetMember = new DiagnosticDescriptor( + "RMG052", + "Invalid ignore target member found, nested ignores are not supported", + "Invalid ignore target member {0} found for type {1}, nested ignores are not supported", + DiagnosticCategories.Mapper, + DiagnosticSeverity.Warning, + true + ); } diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyIgnoreTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyIgnoreTest.cs index 32fa6b2ac4..ea1d3c65b2 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyIgnoreTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyIgnoreTest.cs @@ -145,4 +145,28 @@ public void WithNotFoundIgnoredSourcePropertyShouldDiagnostic() """ ); } + + [Fact] + public void WithNestedIgnoredSourceAndTargetPropertyShouldDiagnostic() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapperIgnoreSource(\"StringValue.Value\")] [MapperIgnoreTarget(\"StringValue.Value\")] partial B Map(A source);", + "class A { public string StringValue { get; set; } }", + "class B { public string StringValue { get; set; } }" + ); + + TestHelper + .GenerateMapper(source, TestHelperOptions.AllowDiagnostics) + .Should() + .HaveDiagnostic(DiagnosticDescriptors.NestedIgnoredSourceMember) + .HaveDiagnostic(DiagnosticDescriptors.NestedIgnoredTargetMember) + .HaveAssertedAllDiagnostics() + .HaveSingleMethodBody( + """ + var target = new global::B(); + target.StringValue = source.StringValue; + return target; + """ + ); + } }