Skip to content

Commit

Permalink
Added exclude support to the inherit generator. Fixed a bug where the…
Browse files Browse the repository at this point in the history
… generator would also copy nested type declarations (#1908)
  • Loading branch information
david-driscoll authored Mar 31, 2024
1 parent 32203f1 commit 1b484ec
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 144 deletions.
9 changes: 4 additions & 5 deletions src/Analyzers/GraphqlMutationActionBodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,10 @@ private void GenerateMethods(
.Where(z => z is { symbol: { }, method: { }, })
.ToImmutableArray();

var newClass = syntax
.WithMembers(List<MemberDeclarationSyntax>())
.WithConstraintClauses(List<TypeParameterConstraintClauseSyntax>())
.WithAttributeLists(List<AttributeListSyntax>())
.WithBaseList(null)
var newClass = ClassDeclaration(syntax.Identifier)
.WithModifiers(TokenList(syntax.Modifiers.Select(z => z.WithoutTrivia())))
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken))
.WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken))
;


Expand Down
311 changes: 185 additions & 126 deletions src/Analyzers/InheritFromGenerator.cs

Large diffs are not rendered by default.

32 changes: 23 additions & 9 deletions src/Foundation/InheritFromAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@ namespace Rocket.Surgery.LaunchPad.Foundation;
/// <summary>
/// Allows you copy the properties of the another class or interface, and include a helper method to copy content to the given record.
/// </summary>
/// <remarks>
/// This attribute works on record classes only.
/// </remarks>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
[PublicAPI]
[Conditional("CodeGeneration")]
public sealed class InheritFromAttribute : Attribute
{
/// <summary>
/// Create the inherit from attribute
/// </summary>
/// <param name="classToCopy"></param>
public InheritFromAttribute(Type classToCopy)
{
ClassToCopy = classToCopy;
}
public InheritFromAttribute(Type classToCopy) { }

/// <summary>
/// The class that is being copied from
/// Exclude the given properties from the copy
/// </summary>
public Type ClassToCopy { get; }
public string[] Exclude { get; init; } = Array.Empty<string>();
}

/// <summary>
/// Allows you copy the properties of the another class or interface, and include a helper method to copy content to the given record.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
[PublicAPI]
[Conditional("CodeGeneration")]
public sealed class InheritFromAttribute<T> : Attribute where T : notnull
{
/// <summary>
/// Create the inherit from attribute
/// </summary>
public InheritFromAttribute() { }

/// <summary>
/// Exclude the given properties from the copy
/// </summary>
public string[] Exclude { get; init; } = Array.Empty<string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<_Analyzer_Roslyn_Version_>$(MSBuildProjectName.Replace("Analyzers.Tests.", ""))</_Analyzer_Roslyn_Version_>
<DefineConstants>$(DefineConstants);ROSLYN_CURRENT;ROSLYN4_0</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN4_0</DefineConstants>
<RootNamespace>Analyzers.Tests</RootNamespace>
<AssemblyName>Analyzers.Tests</AssemblyName>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<_Analyzer_Roslyn_Version_>$(MSBuildProjectName.Replace("Analyzers.Tests.", ""))</_Analyzer_Roslyn_Version_>
<DefineConstants>$(DefineConstants);ROSLYN_CURRENT;ROSLYN4_0;ROSLYN4_4</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN4_4</DefineConstants>
<RootNamespace>Analyzers.Tests</RootNamespace>
<AssemblyName>Analyzers.Tests</AssemblyName>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<_Analyzer_Roslyn_Version_>$(MSBuildProjectName.Replace("Analyzers.Tests.", ""))</_Analyzer_Roslyn_Version_>
<DefineConstants>$(DefineConstants);ROSLYN_CURRENT;ROSLYN4_0;ROSLYN4_4;ROSLYN4_6</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN4_6</DefineConstants>
<RootNamespace>Analyzers.Tests</RootNamespace>
<AssemblyName>Analyzers.Tests</AssemblyName>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/Analyzers.Tests/Analyzers.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);ROSLYN_CURRENT;ROSLYN4_0;ROSLYN4_4;ROSLYN4_6;ROSLYN4_8</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN_CURRENT;ROSLYN4_8</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
Expand Down
83 changes: 83 additions & 0 deletions test/Analyzers.Tests/InheritFromGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,44 @@ public static partial class CreateRocket
public partial record Model
{
public string SerialNumber { get; set; }
public string Excluded { get; set; }
}
[InheritFrom(typeof(Model), Exclude = new[] { nameof(Model.Excluded) })]
public partial record Request : IRequest<Response>
{
public Guid Id { get; init; }
}
public partial record Response {}
}
}
"
)
.Build()
.GenerateAsync();
await Verify(result);
}

[Fact]
public async Task Should_Generate_And_Ignore_Type_Declaration_Members()
{
var result = await Builder
.AddSources(
@"
using System;
using MediatR;
using Rocket.Surgery.LaunchPad.Foundation;
namespace Sample.Core.Operations.Rockets
{
public static partial class CreateRocket
{
public partial record Model
{
public string SerialNumber { get; set; }
private class Mapper;
}
[InheritFrom(typeof(Model))]
Expand Down Expand Up @@ -164,6 +202,51 @@ public partial record Response {}
await Verify(result);
}

#if !ROSLYN4_0
[Fact]
public async Task Should_Inherit_Using_Generic_Type_Arguments()
{
var result = await Builder
.AddSources(
@"
using System;
using MediatR;
using Rocket.Surgery.LaunchPad.Foundation;
namespace Sample.Core.Operations.Rockets
{
public static partial class CreateRocket
{
public partial record Model
{
public string SerialNumber { get; set; }
public string Excluded { get; set; }
}
public partial record Other
{
public string OtherNumber { get; set; }
}
[InheritFrom<Model>(Exclude = new[] { nameof(Model.Excluded) })]
[InheritFrom<Other>]
public partial record Request : IRequest<Response>
{
public Guid Id { get; init; }
}
public partial record Response {}
}
}
"
)
.Build()
.GenerateAsync();

await Verify(result);
}
#endif

[Fact]
public async Task Should_Generate_With_Method_For_Record_That_Inherits()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: Rocket.Surgery.LaunchPad.Analyzers/Rocket.Surgery.LaunchPad.Analyzers.InheritFromGenerator/Input1_Request.cs
#nullable enable
using System;
using MediatR;
using Rocket.Surgery.LaunchPad.Foundation;

namespace Sample.Core.Operations.Rockets
{
public static partial class CreateRocket
{
[System.Runtime.CompilerServices.CompilerGenerated]
public partial record Request
{
public string SerialNumber { get; set; }

public Request With(Model value) => this with
{
SerialNumber = value.SerialNumber
};
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
FinalDiagnostics: [],
GeneratorDiagnostics: {
Rocket.Surgery.LaunchPad.Analyzers.InheritFromGenerator: []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//HintName: Rocket.Surgery.LaunchPad.Analyzers/Rocket.Surgery.LaunchPad.Analyzers.InheritFromGenerator/Input1_Request.cs
#nullable enable
using System;
using MediatR;
using Rocket.Surgery.LaunchPad.Foundation;

namespace Sample.Core.Operations.Rockets
{
public static partial class CreateRocket
{
[System.Runtime.CompilerServices.CompilerGenerated]
public partial record Request
{
public string SerialNumber { get; set; }

public Request With(Model value) => this with
{
SerialNumber = value.SerialNumber
};
public string OtherNumber { get; set; }

public Request With(Other value) => this with
{
OtherNumber = value.OtherNumber
};
}
}
}
#nullable restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
FinalDiagnostics: [],
GeneratorDiagnostics: {
Rocket.Surgery.LaunchPad.Analyzers.InheritFromGenerator: []
}
}

0 comments on commit 1b484ec

Please sign in to comment.