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

feat: add diagnostic for nested ignores #818

Merged
merged 1 commit into from
Oct 20, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
TimothyMakkison marked this conversation as resolved.
Show resolved Hide resolved
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;
}
TimothyMakkison marked this conversation as resolved.
Show resolved Hide resolved
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;
"""
);
}
}