Skip to content

Commit 2e76348

Browse files
committed
Preserve /// <inheritdoc/> in top level types, add unit test
1 parent 3b2d287 commit 2e76348

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Models/HierarchyInfo.Syntax.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public CompilationUnitSyntax GetCompilationUnit(
6868

6969
if (Namespace is "")
7070
{
71+
// Special case for top level types with no namespace: we need to re-add the
72+
// inheritdoc XML comment, as otherwise the call below would remove it.
73+
syntaxTriviaList = syntaxTriviaList.Add(Comment("/// <inheritdoc/>"));
74+
7175
// If there is no namespace, attach the pragma directly to the declared type,
7276
// and skip the namespace declaration. This will produce code as follows:
7377
//

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsCodegen.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,41 @@ public string? A
15381538
#endif
15391539
}
15401540

1541+
[TestMethod]
1542+
public void RelayCommandMethodInTopLevelTypeWithNoNamespace_PreservesInheritdoc()
1543+
{
1544+
string source = """
1545+
using CommunityToolkit.Mvvm.Input;
1546+
1547+
partial class MyViewModel
1548+
{
1549+
[RelayCommand]
1550+
private void Test()
1551+
{
1552+
}
1553+
}
1554+
""";
1555+
1556+
string result = """
1557+
// <auto-generated/>
1558+
#pragma warning disable
1559+
#nullable enable
1560+
/// <inheritdoc/>
1561+
partial class MyViewModel
1562+
{
1563+
/// <summary>The backing field for <see cref="TestCommand"/>.</summary>
1564+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
1565+
private global::CommunityToolkit.Mvvm.Input.RelayCommand? testCommand;
1566+
/// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IRelayCommand"/> instance wrapping <see cref="Test"/>.</summary>
1567+
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
1568+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
1569+
public global::CommunityToolkit.Mvvm.Input.IRelayCommand TestCommand => testCommand ??= new global::CommunityToolkit.Mvvm.Input.RelayCommand(new global::System.Action(Test));
1570+
}
1571+
""";
1572+
1573+
VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyViewModel.Test.g.cs", result));
1574+
}
1575+
15411576
/// <summary>
15421577
/// Generates the requested sources
15431578
/// </summary>

0 commit comments

Comments
 (0)