Skip to content

Commit

Permalink
handle inheritance with primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Nov 26, 2023
1 parent 391b8a2 commit d43b5b5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/InheritDoc/InheritDoc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<VersionPrefix>1.4.0</VersionPrefix>
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
13 changes: 11 additions & 2 deletions src/InheritDoc/InheritDocProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,18 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
logger.Write(ILogger.Severity.Diag, "Processing DocID: " + memID);

var crefs = methDocs.Descendants(DocElementNames.InheritDoc).Select(i => (string)i.Attribute(DocAttributeNames.Cref)).Where(c => !string.IsNullOrWhiteSpace(c)).ToHashSet();
var dml = new List<DocMatch>();
var bases = (om is not null ? (new[] { om }) : [ ]).Concat(m.GetBaseCandidates()).ToList();

var bases = (om is not null ? (new[] { om }) : [ ]).Concat(m.GetBaseCandidates());
// With C# primary constructors, the constructor does not have docs of its own. If we don't have a base
// or cref for a constructor, inject its declaring type as a cref so we don't warn about a missing base.
if (m.IsConstructor && bases.Count == 0 && crefs.Count == 0)
{
crefs.Add(m.DeclaringType.GetDocID());
foreach (var d in methDocs.Descendants(DocElementNames.InheritDoc))
d.SetElementValue(DocAttributeNames.Cref, m.DeclaringType.GetDocID());
}

var dml = new List<DocMatch>();
foreach (var (bm, cref) in bases.SelectMany(bm => bm.GetDocID().Select(d => (bm, d))))
{
if (dml.Count == 0 || crefs.Contains(cref))
Expand Down
2 changes: 1 addition & 1 deletion src/InheritDoc/package/build/SauceControl.InheritDoc.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>

<PropertyGroup>
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'!='Core'">net462</_InheritDocTaskTfm>
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'!='Core'">net46</_InheritDocTaskTfm>
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'=='Core'">netstandard2.0</_InheritDocTaskTfm>
<_InheritDocTaskLib>$(MSBuildThisFileDirectory)..\tools\$(_InheritDocTaskTfm)\SauceControl.InheritDoc.dll</_InheritDocTaskLib>
<_InheritDocNetStandardFallback>$(MSBuildThisFileDirectory)..\tools\netstandard.xml.gz</_InheritDocNetStandardFallback>
Expand Down
10 changes: 10 additions & 0 deletions tests/InheritDoc.Test/DocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ private GII() { }
unsafe public T[] M<T>(T* tp) where T : unmanaged => default;
}

/// <inheritdoc />
/// <param name="s">Param s</param>
public class PC(string s) : GGI
{
new internal const string T_ID = nameof(PC);
internal const string M_ID_ctor = T_ID + ".#ctor(" + nameof(System) + "." + nameof(String) + ")";

string S => s;
}

/// <summary>
/// Class W
/// <see cref="B" />
Expand Down
7 changes: 7 additions & 0 deletions tests/InheritDoc.Test/InheritDocTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ public void WhitespacePreserved()
Assert.IsTrue(ele?.NextNode?.IsWhiteSpace() ?? false);
}

[TestMethod]
public void PrimaryConstructorInheritsFromType()
{
var ele = getDocElement("M:" + PC.M_ID_ctor, "summary");
Assert.AreEqual("Class G ", ele?.Value);
}

[TestMethod]
public void InternalMembersTrimmed()
{
Expand Down

0 comments on commit d43b5b5

Please sign in to comment.