Skip to content

Commit

Permalink
Preserve /// <inheritdoc/> in top level types, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Mar 25, 2023
1 parent 3b2d287 commit 2e76348
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ partial record HierarchyInfo

if (Namespace is "")
{
// Special case for top level types with no namespace: we need to re-add the
// inheritdoc XML comment, as otherwise the call below would remove it.
syntaxTriviaList = syntaxTriviaList.Add(Comment("/// <inheritdoc/>"));

// If there is no namespace, attach the pragma directly to the declared type,
// and skip the namespace declaration. This will produce code as follows:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,41 @@ partial class MyViewModel<T>
#endif
}

[TestMethod]
public void RelayCommandMethodInTopLevelTypeWithNoNamespace_PreservesInheritdoc()
{
string source = """
using CommunityToolkit.Mvvm.Input;

partial class MyViewModel
{
[RelayCommand]
private void Test()
{
}
}
""";

string result = """
// <auto-generated/>
#pragma warning disable
#nullable enable
/// <inheritdoc/>
partial class MyViewModel
{
/// <summary>The backing field for <see cref="TestCommand"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
private global::CommunityToolkit.Mvvm.Input.RelayCommand? testCommand;
/// <summary>Gets an <see cref="global::CommunityToolkit.Mvvm.Input.IRelayCommand"/> instance wrapping <see cref="Test"/>.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.RelayCommandGenerator", "8.2.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public global::CommunityToolkit.Mvvm.Input.IRelayCommand TestCommand => testCommand ??= new global::CommunityToolkit.Mvvm.Input.RelayCommand(new global::System.Action(Test));
}
""";

VerifyGenerateSources(source, new[] { new RelayCommandGenerator() }, ("MyViewModel.Test.g.cs", result));
}

/// <summary>
/// Generates the requested sources
/// </summary>
Expand Down

0 comments on commit 2e76348

Please sign in to comment.