Skip to content

Commit

Permalink
fix: enable static user methods for instance mappers (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Apr 20, 2023
1 parent 685d6ee commit 7e1cb8f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 65 deletions.
6 changes: 6 additions & 0 deletions src/Riok.Mapperly/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ RMG030 | Mapper | Error | Reference loop detected while mapping to an init
RMG031 | Mapper | Warning | Reference loop detected while mapping to a constructor member
RMG032 | Mapper | Warning | The enum mapping strategy ByName cannot be used in projection mappings
RMG033 | Mapper | Info | Object mapped to another object without deep clone

### Removed Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
RMG019 | Mapper | Disabled | Partial instance mapping method in a static mapper
13 changes: 4 additions & 9 deletions src/Riok.Mapperly/Descriptors/UserMethodMappingExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool isStatic
BuildParameters(ctx, method, out var parameters)
&& !method.ReturnsVoid
&& (allowPartial || !method.IsPartialDefinition)
&& isStatic == method.IsStatic;
&& (!isStatic || method.IsStatic);
return valid ? new UserImplementedMethodMapping(method, parameters.Source, parameters.ReferenceHandler) : null;
}

Expand All @@ -76,15 +76,10 @@ bool isStatic
if (!methodSymbol.IsPartialDefinition)
return null;

if (isStatic != methodSymbol.IsStatic)
if (!isStatic && methodSymbol.IsStatic)
{
ctx.ReportDiagnostic(
isStatic
? DiagnosticDescriptors.PartialInstanceMethodInStaticMapper
: DiagnosticDescriptors.PartialStaticMethodInInstanceMapper,
methodSymbol,
methodSymbol.Name
);
ctx.ReportDiagnostic(DiagnosticDescriptors.PartialStaticMethodInInstanceMapper, methodSymbol, methodSymbol.Name);

return null;
}

Expand Down
9 changes: 0 additions & 9 deletions src/Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,6 @@ internal static class DiagnosticDescriptors
true
);

public static readonly DiagnosticDescriptor PartialInstanceMethodInStaticMapper = new DiagnosticDescriptor(
"RMG019",
"Partial instance mapping method in a static mapper",
"{0} is a partial instance mapping method in a static mapper. Instance mapping methods are only supported in instance (non-static) mappers.",
DiagnosticCategories.Mapper,
DiagnosticSeverity.Error,
true
);

public static readonly DiagnosticDescriptor SourceMemberNotMapped = new DiagnosticDescriptor(
"RMG020",
"Source member is not mapped to any target member",
Expand Down
15 changes: 9 additions & 6 deletions test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Task WithNamespaceShouldWork()
}

[Fact]
public Task StaticMapperShouldEmitDiagnosticForInstanceMethods()
public Task InstanceMapperShouldEmitDiagnosticForPartialStaticMethods()
{
var source =
@"
Expand All @@ -22,9 +22,9 @@ public Task StaticMapperShouldEmitDiagnosticForInstanceMethods()
using Riok.Mapperly.Abstractions;
[Mapper]
public static partial class MyStaticMapper
public partial class MyMapper
{
public partial static object StaticToObject(string s);
public static object StaticToObject(string s);
public partial object InstanceToObject(string s);
}
Expand All @@ -33,7 +33,7 @@ public static partial class MyStaticMapper
}

[Fact]
public Task InstanceMapperShouldEmitDiagnosticForStaticMethods()
public Task InstanceMapperShouldSupportUserDefinedStaticMethods()
{
var source =
@"
Expand All @@ -44,10 +44,13 @@ public Task InstanceMapperShouldEmitDiagnosticForStaticMethods()
[Mapper]
public partial class MyMapper
{
public partial static object StaticToObject(string s);
public static int StaticMapper(int s) => s;
public partial object InstanceToObject(string s);
public partial B Map(A s);
}
public record A(int Value);
public record B(int Value);
";
return TestHelper.VerifyGenerator(source);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//HintName: MyMapper.g.cs
#nullable enable
public partial class MyMapper
{
public partial global::B Map(global::A s)
{
var target = new global::B(StaticMapper(s.Value));
return target;
}
}

This file was deleted.

This file was deleted.

0 comments on commit 7e1cb8f

Please sign in to comment.