Skip to content

Commit

Permalink
fix: prevent duplicates attributes and base types for nested mappers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Jul 10, 2023
1 parent 4dc9788 commit 44667c8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Riok.Mapperly/Emit/SourceEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ private static MemberDeclarationSyntax WrapInClassesAsNeeded(INamedTypeSymbol sy
if (containingType.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not ClassDeclarationSyntax containingTypeSyntax)
return syntax;

syntax = containingTypeSyntax.WithMembers(SingletonList(syntax));
syntax = containingTypeSyntax
.WithMembers(SingletonList(syntax))
.WithAttributeLists(List<AttributeListSyntax>())
.WithBaseList(null);
containingType = containingType.ContainingType;
}

Expand Down
52 changes: 52 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/MapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,56 @@ public partial class CarMapper

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task MapperInNestedClassesWithAttributesShouldWork()
{
var source = TestSourceBuilder.CSharp(
"""
using Riok.Mapperly.Abstractions;

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static partial class CarFeature
{
[Obsolete]
public static partial class Mappers
{
[Mapper]
public partial class CarMapper
{
public partial int ToInt(double value);
}
}
}
"""
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task MapperInNestedClassesWithBaseTypeShouldWork()
{
var source = TestSourceBuilder.CSharp(
"""
using Riok.Mapperly.Abstractions;

public abstract class BaseClass { }

public static partial class CarFeature : BaseClass
{
public static partial class Mappers : BaseClass
{
[Mapper]
public partial class CarMapper
{
public partial int ToInt(double value);
}
}
}
"""
);

return TestHelper.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//HintName: CarFeature.Mappers.CarMapper.g.cs
// <auto-generated />
#nullable enable
public static partial class CarFeature
{
public static partial class Mappers
{
public partial class CarMapper
{
public partial int ToInt(double value)
{
return (int)value;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//HintName: CarFeature.Mappers.CarMapper.g.cs
// <auto-generated />
#nullable enable
public static partial class CarFeature
{
public static partial class Mappers
{
public partial class CarMapper
{
public partial int ToInt(double value)
{
return (int)value;
}
}
}
}

0 comments on commit 44667c8

Please sign in to comment.