Skip to content

Commit

Permalink
feat: report a diagnostic for invalid MapPropertyAttribute usages (#1130
Browse files Browse the repository at this point in the history
)
  • Loading branch information
latonz committed Feb 26, 2024
1 parent 732cec4 commit b8b9e2a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Riok.Mapperly/AnalyzerReleases.Shipped.md
Expand Up @@ -151,3 +151,4 @@ RMG063 | Mapper | Error | Cannot configure an enum mapping on a non-enum m
RMG064 | Mapper | Error | Cannot configure an object mapping on a non-object mapping
RMG065 | Mapper | Warning | Cannot configure an object mapping on a queryable projection mapping, apply the configurations to an object mapping method instead
RMG066 | Mapper | Warning | No members are mapped in an object mapping
RMG067 | Mapper | Error | Invalid usage of the MapPropertyAttribute
5 changes: 5 additions & 0 deletions src/Riok.Mapperly/Configuration/MapperConfigurationReader.cs
Expand Up @@ -106,6 +106,11 @@ private MembersMappingConfiguration BuildMembersConfig(MappingConfigurationRefer
return MapperConfiguration.Members;
}

foreach (var invalidMemberConfigs in memberConfigurations.Where(x => !x.IsValid))
{
diagnostics.ReportDiagnostic(DiagnosticDescriptors.InvalidMapPropertyAttributeUsage, invalidMemberConfigs.Location);
}

return new MembersMappingConfiguration(
ignoredSourceMembers,
ignoredTargetMembers,
Expand Down
2 changes: 2 additions & 0 deletions src/Riok.Mapperly/Configuration/MemberMappingConfiguration.cs
Expand Up @@ -10,5 +10,7 @@ public record MemberMappingConfiguration(StringMemberPath Source, StringMemberPa

public string? Use { get; set; }

public bool IsValid => Use == null || FormatProvider == null && StringFormat == null;

public TypeMappingConfiguration ToTypeMappingConfiguration() => new(StringFormat, FormatProvider, Use);
}
9 changes: 9 additions & 0 deletions src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs
Expand Up @@ -583,6 +583,15 @@ public static class DiagnosticDescriptors
helpLinkUri: BuildHelpUri("RMG066")
);

public static readonly DiagnosticDescriptor InvalidMapPropertyAttributeUsage = new DiagnosticDescriptor(
"RMG067",
"Invalid usage of the MapPropertyAttribute",
"Invalid usage of the MapPropertyAttribute",
DiagnosticCategories.Mapper,
DiagnosticSeverity.Error,
true
);

private static string BuildHelpUri(string id)
{
#if ENV_NEXT
Expand Down
21 changes: 21 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs
Expand Up @@ -496,4 +496,25 @@ public void ModifyingPathIfClassPrecedesShouldNotDiagnostic()
"""
);
}

[Fact]
public void InvalidMapPropertyAttributeUsageShouldDiagnostic()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapProperty("IntValue", "StringValue", StringFormat = "D", Use = nameof(IntToString))]
partial B Map(A src);

string IntToString(int x) => x.ToString();
""",
"class A { public int IntValue { get; set; } }",
"class B { public string StringValue { get; set; } }"
);

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(DiagnosticDescriptors.InvalidMapPropertyAttributeUsage, "Invalid usage of the MapPropertyAttribute")
.HaveAssertedAllDiagnostics();
}
}

0 comments on commit b8b9e2a

Please sign in to comment.