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

fix: enable static user methods for instance mappers #364

Merged
merged 1 commit into from
Apr 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.