Skip to content

Commit d43b5b5

Browse files
committed
handle inheritance with primary constructors
1 parent 391b8a2 commit d43b5b5

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

src/InheritDoc/InheritDoc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<VersionPrefix>1.4.0</VersionPrefix>
5-
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
66
</PropertyGroup>
77

88
<PropertyGroup>

src/InheritDoc/InheritDocProcessor.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,18 @@ private static IDictionary<string, IEnumerable<DocMatch>> generateDocMap(IList<T
230230
logger.Write(ILogger.Severity.Diag, "Processing DocID: " + memID);
231231

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

235-
var bases = (om is not null ? (new[] { om }) : [ ]).Concat(m.GetBaseCandidates());
235+
// With C# primary constructors, the constructor does not have docs of its own. If we don't have a base
236+
// or cref for a constructor, inject its declaring type as a cref so we don't warn about a missing base.
237+
if (m.IsConstructor && bases.Count == 0 && crefs.Count == 0)
238+
{
239+
crefs.Add(m.DeclaringType.GetDocID());
240+
foreach (var d in methDocs.Descendants(DocElementNames.InheritDoc))
241+
d.SetElementValue(DocAttributeNames.Cref, m.DeclaringType.GetDocID());
242+
}
243+
244+
var dml = new List<DocMatch>();
236245
foreach (var (bm, cref) in bases.SelectMany(bm => bm.GetDocID().Select(d => (bm, d))))
237246
{
238247
if (dml.Count == 0 || crefs.Contains(cref))

src/InheritDoc/package/build/SauceControl.InheritDoc.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33

44
<PropertyGroup>
5-
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'!='Core'">net462</_InheritDocTaskTfm>
5+
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'!='Core'">net46</_InheritDocTaskTfm>
66
<_InheritDocTaskTfm Condition="'$(MSBuildRuntimeType)'=='Core'">netstandard2.0</_InheritDocTaskTfm>
77
<_InheritDocTaskLib>$(MSBuildThisFileDirectory)..\tools\$(_InheritDocTaskTfm)\SauceControl.InheritDoc.dll</_InheritDocTaskLib>
88
<_InheritDocNetStandardFallback>$(MSBuildThisFileDirectory)..\tools\netstandard.xml.gz</_InheritDocNetStandardFallback>

tests/InheritDoc.Test/DocTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ private GII() { }
258258
unsafe public T[] M<T>(T* tp) where T : unmanaged => default;
259259
}
260260

261+
/// <inheritdoc />
262+
/// <param name="s">Param s</param>
263+
public class PC(string s) : GGI
264+
{
265+
new internal const string T_ID = nameof(PC);
266+
internal const string M_ID_ctor = T_ID + ".#ctor(" + nameof(System) + "." + nameof(String) + ")";
267+
268+
string S => s;
269+
}
270+
261271
/// <summary>
262272
/// Class W
263273
/// <see cref="B" />

tests/InheritDoc.Test/InheritDocTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ public void WhitespacePreserved()
265265
Assert.IsTrue(ele?.NextNode?.IsWhiteSpace() ?? false);
266266
}
267267

268+
[TestMethod]
269+
public void PrimaryConstructorInheritsFromType()
270+
{
271+
var ele = getDocElement("M:" + PC.M_ID_ctor, "summary");
272+
Assert.AreEqual("Class G ", ele?.Value);
273+
}
274+
268275
[TestMethod]
269276
public void InternalMembersTrimmed()
270277
{

0 commit comments

Comments
 (0)