Skip to content

Commit

Permalink
fix(api-markdown-documenter): Include missing property/variable type …
Browse files Browse the repository at this point in the history
…information to "details" sections (#20782)

Affects default transformation implementations.

Adds content like the following to the "Signature" section of
property/variable details sections:
```markdown
Type: boolean
```

These sections will include hyperlinks to the relevant type details for
types defined in the API model.
  • Loading branch information
Josmithr committed Apr 22, 2024
1 parent 0a9b9ab commit 6967b02
Show file tree
Hide file tree
Showing 49 changed files with 275 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/api-markdown-documenter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 0.12.2

- Fixed an issue where variable item tables did not include type information.
- Fixed an issue where variable item tables did not include type information in default ApiItem transformations.
- Add variable and property type information to associated details sections in default ApiItem transformations.
- Further improved error messages when an unexpected child kind is encountered when iterating over children in default ApiItem transformations.

## 0.12.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
type HeritageType,
type IResolveDeclarationReferenceResult,
type TypeParameter,
ApiPropertyItem,
ApiVariable,
} from "@microsoft/api-extractor-model";
import {
type DocNode,
Expand Down Expand Up @@ -198,6 +200,17 @@ export function createHeritageTypesParagraph(
}
}

// Render type information for properties and variables
let renderedTypeSpan: SpanNode | undefined;
if (apiItem instanceof ApiPropertyItem) {
renderedTypeSpan = createTypeSpan(apiItem.propertyTypeExcerpt, config);
} else if (apiItem instanceof ApiVariable) {
renderedTypeSpan = createTypeSpan(apiItem.variableTypeExcerpt, config);
}
if (renderedTypeSpan !== undefined) {
contents.push(new ParagraphNode([renderedTypeSpan]));
}

// Render type parameters if there are any.
if (ApiTypeParameterListMixin.isBaseClassOf(apiItem) && apiItem.typeParameters.length > 0) {
const renderedTypeParameters = createTypeParametersSection(
Expand All @@ -220,6 +233,28 @@ export function createHeritageTypesParagraph(
return new ParagraphNode(contents);
}

/**
* Renders a labeled type-information entry.
*
* @remarks Displayed as `Type: <type>`. Type excerpt will be rendered with the appropriate hyperlinks for other types in the API model.
*
* @param excerpt - The type excerpt to be displayed.
* @param config - See {@link ApiItemTransformationConfiguration}.
*/
function createTypeSpan(
excerpt: Excerpt,
config: Required<ApiItemTransformationConfiguration>,
): SpanNode | undefined {
if (!excerpt.isEmpty) {
const renderedLabel = SpanNode.createFromPlainText(`Type: `, { bold: true });
const renderedExcerpt = createExcerptSpanWithHyperlinks(excerpt, config);
if (renderedExcerpt !== undefined) {
return new SpanNode([renderedLabel, renderedExcerpt]);
}
}
return undefined;
}

/**
* Renders a labeled, comma-separated list of heritage types.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,15 +369,23 @@ describe("ApiItem to Documentation transformation tests", () => {
[
wrapInSection(
[
// Summary section
wrapInSection([
ParagraphNode.createFromPlainText("Test optional property"),
]),
// Signature section
wrapInSection(
[
FencedCodeBlockNode.createFromPlainText(
"testOptionalInterfaceProperty?: number;",
"typescript",
),
new ParagraphNode([
new SpanNode([
SpanNode.createFromPlainText("Type: ", { bold: true }),
SpanNode.createFromPlainText("number"),
]),
]),
],
{
title: "Signature",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,9 @@ <h4 id="testconstwithemptydeprecatedblock-signature">
<code>
testConstWithEmptyDeprecatedBlock: string
</code>
<p>
<span><span><b>Type:</b> </span><span>string</span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ <h4 id="abstractpropertygetter-signature">
<code>
abstract get abstractPropertyGetter(): TestMappedType;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='./simple-suite-test#testmappedtype-typealias'>TestMappedType</a></span></span>
</p>
</section>
</section>
<section>
Expand All @@ -276,6 +279,9 @@ <h4 id="protectedproperty-signature">
<code>
protected readonly protectedProperty: TestEnum;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='./simple-suite-test#testenum-enum'>TestEnum</a></span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ <h4 id="testclasseventproperty-signature">
<code>
readonly testClassEventProperty: () =&gt; void;
</code>
<p>
<span><span><b>Type:</b> </span><span>() =&gt; void</span></span>
</p>
</section>
<section>
<h4 id="testclasseventproperty-remarks">
Expand Down Expand Up @@ -494,6 +497,9 @@ <h4 id="abstractpropertygetter-signature">
<code>
get abstractPropertyGetter(): TestMappedType;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='./simple-suite-test#testmappedtype-typealias'>TestMappedType</a></span></span>
</p>
</section>
</section>
<section>
Expand All @@ -518,6 +524,9 @@ <h4 id="testclassgetterproperty-signature">
<br>
set testClassGetterProperty(newValue: number);
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
<section>
<h4 id="testclassgetterproperty-remarks">
Expand All @@ -544,6 +553,9 @@ <h4 id="testclassproperty-signature">
<code>
readonly testClassProperty: TTypeParameterB;
</code>
<p>
<span><span><b>Type:</b> </span><span>TTypeParameterB</span></span>
</p>
</section>
<section>
<h4 id="testclassproperty-remarks">
Expand All @@ -570,6 +582,9 @@ <h4 id="testclassstaticproperty-signature">
<code>
static testClassStaticProperty: (foo: number) =&gt; string;
</code>
<p>
<span><span><b>Type:</b> </span><span>(foo: number) =&gt; string</span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ <h4 id="testclasseventproperty-signature">
<code>
readonly testClassEventProperty: () =&gt; void;
</code>
<p>
<span><span><b>Type:</b> </span><span>() =&gt; void</span></span>
</p>
</section>
<section>
<h4 id="testclasseventproperty-remarks">
Expand Down Expand Up @@ -348,6 +351,9 @@ <h4 id="getterproperty-signature">
<code>
get getterProperty(): boolean;
</code>
<p>
<span><span><b>Type:</b> </span><span>boolean</span></span>
</p>
</section>
</section>
<section>
Expand All @@ -370,6 +376,9 @@ <h4 id="setterproperty-signature">
<br>
set setterProperty(newValue: boolean);
</code>
<p>
<span><span><b>Type:</b> </span><span>boolean</span></span>
</p>
</section>
</section>
<section>
Expand All @@ -388,6 +397,9 @@ <h4 id="testinterfaceproperty-signature">
<code>
testInterfaceProperty: number;
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
<section>
<h4 id="testinterfaceproperty-remarks">
Expand All @@ -414,6 +426,9 @@ <h4 id="testoptionalinterfaceproperty-signature">
<code>
testOptionalInterfaceProperty?: number;
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ <h4 id="testproperty-signature">
<code>
testProperty: T;
</code>
<p>
<span><span><b>Type:</b> </span><span>T</span></span>
</p>
</section>
<section>
<h4 id="testproperty-remarks">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ <h4 id="testclassproperty-signature">
<code>
readonly testClassProperty: string;
</code>
<p>
<span><span><b>Type:</b> </span><span>string</span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ <h4 id="testinterfaceproperty-signature">
<code>
testInterfaceProperty: boolean;
</code>
<p>
<span><span><b>Type:</b> </span><span>boolean</span></span>
</p>
</section>
</section>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ <h5 id="testclasseventproperty-signature">
<code>
readonly testClassEventProperty: () =&gt; void;
</code>
<p>
<span><span><b>Type:</b> </span><span>() =&gt; void</span></span>
</p>
</section>
<section>
<h5 id="testclasseventproperty-remarks">
Expand Down Expand Up @@ -747,6 +750,9 @@ <h5 id="getterproperty-signature">
<code>
get getterProperty(): boolean;
</code>
<p>
<span><span><b>Type:</b> </span><span>boolean</span></span>
</p>
</section>
</section>
<section>
Expand All @@ -769,6 +775,9 @@ <h5 id="setterproperty-signature">
<br>
set setterProperty(newValue: boolean);
</code>
<p>
<span><span><b>Type:</b> </span><span>boolean</span></span>
</p>
</section>
</section>
<section>
Expand All @@ -787,6 +796,9 @@ <h5 id="testinterfaceproperty-signature">
<code>
testInterfaceProperty: number;
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
<section>
<h5 id="testinterfaceproperty-remarks">
Expand All @@ -813,6 +825,9 @@ <h5 id="testoptionalinterfaceproperty-signature">
<code>
testOptionalInterfaceProperty?: number;
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
</section>
</section>
Expand Down Expand Up @@ -1254,6 +1269,9 @@ <h5 id="testproperty-signature">
<code>
testProperty: T;
</code>
<p>
<span><span><b>Type:</b> </span><span>T</span></span>
</p>
</section>
<section>
<h5 id="testproperty-remarks">
Expand Down Expand Up @@ -1519,6 +1537,9 @@ <h5 id="abstractpropertygetter-signature">
<code>
abstract get abstractPropertyGetter(): TestMappedType;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='docs/simple-suite-test#testmappedtype-typealias'>TestMappedType</a></span></span>
</p>
</section>
</section>
<section>
Expand All @@ -1537,6 +1558,9 @@ <h5 id="protectedproperty-signature">
<code>
protected readonly protectedProperty: TestEnum;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='docs/simple-suite-test#testenum-enum'>TestEnum</a></span></span>
</p>
</section>
</section>
</section>
Expand Down Expand Up @@ -2079,6 +2103,9 @@ <h5 id="testclasseventproperty-signature">
<code>
readonly testClassEventProperty: () =&gt; void;
</code>
<p>
<span><span><b>Type:</b> </span><span>() =&gt; void</span></span>
</p>
</section>
<section>
<h5 id="testclasseventproperty-remarks">
Expand Down Expand Up @@ -2110,6 +2137,9 @@ <h5 id="abstractpropertygetter-signature">
<code>
get abstractPropertyGetter(): TestMappedType;
</code>
<p>
<span><span><b>Type:</b> </span><span><a href='docs/simple-suite-test#testmappedtype-typealias'>TestMappedType</a></span></span>
</p>
</section>
</section>
<section>
Expand All @@ -2134,6 +2164,9 @@ <h5 id="testclassgetterproperty-signature">
<br>
set testClassGetterProperty(newValue: number);
</code>
<p>
<span><span><b>Type:</b> </span><span>number</span></span>
</p>
</section>
<section>
<h5 id="testclassgetterproperty-remarks">
Expand All @@ -2160,6 +2193,9 @@ <h5 id="testclassproperty-signature">
<code>
readonly testClassProperty: TTypeParameterB;
</code>
<p>
<span><span><b>Type:</b> </span><span>TTypeParameterB</span></span>
</p>
</section>
<section>
<h5 id="testclassproperty-remarks">
Expand All @@ -2186,6 +2222,9 @@ <h5 id="testclassstaticproperty-signature">
<code>
static testClassStaticProperty: (foo: number) =&gt; string;
</code>
<p>
<span><span><b>Type:</b> </span><span>(foo: number) =&gt; string</span></span>
</p>
</section>
</section>
</section>
Expand Down Expand Up @@ -2805,6 +2844,9 @@ <h3 id="testconstwithemptydeprecatedblock-signature">
<code>
testConstWithEmptyDeprecatedBlock: string
</code>
<p>
<span><span><b>Type:</b> </span><span>string</span></span>
</p>
</section>
</section>
</section>
Expand Down Expand Up @@ -3310,6 +3352,9 @@ <h6 id="testnamespace-testclass-testclassproperty-property">
<code>
readonly testClassProperty: string;
</code>
<p>
<span><span><b>Type:</b> </span><span>string</span></span>
</p>
</section>
</section>
</section>
Expand Down
Loading

0 comments on commit 6967b02

Please sign in to comment.