Skip to content

Commit

Permalink
feat: add diagnostic for nested ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 18, 2023
1 parent eb7c389 commit 7315c57
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Riok.Mapperly/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
24 changes: 24 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyIgnoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
"""
);
}
}

0 comments on commit 7315c57

Please sign in to comment.