Skip to content

Commit 263cdbf

Browse files
committed
Revert "Revert "Replace Generics in PropertySignatureTable (#11527)" (#11549)"
This reverts commit f688036.
1 parent f688036 commit 263cdbf

File tree

7 files changed

+52
-39
lines changed

7 files changed

+52
-39
lines changed

docs/shared/ApiDoc/EnumDetails.js

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export function EnumDetails({
4747
mr="1"
4848
as={Text}
4949
since
50-
link
5150
id={`${item.displayName.toLowerCase()}-member-${member.displayName.toLowerCase()}`}
5251
/>
5352
<DocBlock

docs/shared/ApiDoc/Function.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
PropertySignatureTable,
1111
SourceLink,
1212
Example,
13-
getInterfaceReference,
1413
} from ".";
1514
import { GridItem } from "@chakra-ui/react";
1615
export function FunctionSignature({
@@ -22,7 +21,8 @@ export function FunctionSignature({
2221
}) {
2322
const MDX = useMDXComponents();
2423
const getItem = useApiDocContext();
25-
const { displayName, parameters, returnType } = getItem(canonicalReference);
24+
const { displayName, parameters, returnType, typeParameters } =
25+
getItem(canonicalReference);
2626

2727
let paramSignature = parameters
2828
.map((p) => {
@@ -41,9 +41,16 @@ export function FunctionSignature({
4141
paramSignature = "\n " + paramSignature + "\n";
4242
}
4343

44+
const genericSignature =
45+
typeParameters?.length ?
46+
`<${typeParameters.map((p) => p.name).join(", ")}>`
47+
: "";
48+
4449
const signature = `${arrow ? "" : "function "}${
4550
name ? displayName : ""
46-
}(${paramSignature})${arrow ? " =>" : ":"} ${returnType}`;
51+
}${genericSignature}(${paramSignature})${arrow ? " =>" : ":"} ${
52+
returnType.type
53+
}`;
4754

4855
return highlight ?
4956
<MDX.pre language="ts">
@@ -65,24 +72,20 @@ export function ReturnType({ canonicalReference }) {
6572
const getItem = useApiDocContext();
6673
const item = getItem(canonicalReference);
6774

68-
const interfaceReference = getInterfaceReference(
69-
item.returnType,
70-
item,
71-
getItem
72-
);
7375
return (
7476
<>
7577
{item.comment?.returns}
7678
<MDX.pre language="ts">
77-
<code className="language-ts">{item.returnType}</code>
79+
<code className="language-ts">{item.returnType.type}</code>
7880
</MDX.pre>
79-
{interfaceReference ?
81+
{item.returnType.primaryCanonicalReference?.endsWith(":interface") ?
8082
<details>
8183
<GridItem as="summary" className="row">
8284
Show/hide child attributes
8385
</GridItem>
8486
<PropertySignatureTable
85-
canonicalReference={interfaceReference.canonicalReference}
87+
canonicalReference={item.returnType.primaryCanonicalReference}
88+
genericNames={item.returnType.primaryGenericArguments}
8689
idPrefix={`${item.displayName.toLowerCase()}-result`}
8790
/>
8891
</details>
@@ -153,7 +156,8 @@ export function FunctionDetails({
153156
</>
154157
)}
155158
{(
156-
result === false || (result === undefined && item.returnType === "void")
159+
result === false ||
160+
(result === undefined && item.returnType.type === "void")
157161
) ?
158162
null
159163
: <>

docs/shared/ApiDoc/ParameterTable.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { useMDXComponents } from "@mdx-js/react";
33
import PropTypes from "prop-types";
44
import React from "react";
55
import { GridItem, Text } from "@chakra-ui/react";
6-
import {
7-
PropertySignatureTable,
8-
SectionHeading,
9-
getInterfaceReference,
10-
useApiDocContext,
11-
} from ".";
6+
import { PropertySignatureTable, useApiDocContext } from ".";
127
import { ResponsiveGrid } from "./ResponsiveGrid";
138

149
export function ParameterTable({ canonicalReference }) {
@@ -25,11 +20,10 @@ export function ParameterTable({ canonicalReference }) {
2520
<GridItem className="cell heading">Description</GridItem>
2621

2722
{item.parameters.map((parameter) => {
28-
const interfaceReference = getInterfaceReference(
29-
parameter.type,
30-
item,
31-
getItem
32-
);
23+
const interfaceReference =
24+
parameter.primaryCanonicalReference?.endsWith(":interface") ?
25+
parameter.primaryCanonicalReference
26+
: undefined;
3327
const id = `${item.displayName.toLowerCase()}-parameters-${parameter.name.toLowerCase()}`;
3428

3529
return (
@@ -68,7 +62,8 @@ export function ParameterTable({ canonicalReference }) {
6862
Show/hide child attributes
6963
</GridItem>
7064
<PropertySignatureTable
71-
canonicalReference={interfaceReference.canonicalReference}
65+
canonicalReference={interfaceReference}
66+
genericNames={parameter.primaryGenericArguments}
7267
display="child"
7368
idPrefix={id}
7469
/>

docs/shared/ApiDoc/PropertySignatureTable.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function PropertySignatureTable({
1919
display = "parent",
2020
customOrder = [],
2121
idPrefix = "",
22+
genericNames,
2223
}) {
2324
const MDX = useMDXComponents();
2425
const getItem = useApiDocContext();
@@ -36,6 +37,29 @@ export function PropertySignatureTable({
3637
);
3738
}
3839

40+
const replaceGenericNames = React.useMemo(() => {
41+
if (!genericNames) return (str) => str;
42+
43+
const replacements = {};
44+
item.typeParameters.forEach((p, i) => {
45+
if (genericNames[i] === p.name) return;
46+
replacements[p.name] = genericNames[i];
47+
});
48+
if (!Object.values(replacements).length) return (str) => str;
49+
50+
const genericReplacementRegex = new RegExp(
51+
`\\b(${Object.keys(replacements).join("|")})\\b`,
52+
"g"
53+
);
54+
function replace(match) {
55+
console.log({ match, replacement: replacements[match] });
56+
return replacements[match] || match;
57+
}
58+
return function replaceGenericNames(str) {
59+
return str.replace(genericReplacementRegex, replace);
60+
};
61+
});
62+
3963
return (
4064
<>
4165
{item.childrenIncomplete ?
@@ -55,7 +79,7 @@ export function PropertySignatureTable({
5579
: null}
5680
{Object.entries(groupedProperties).map(
5781
([groupName, sortedProperties]) => (
58-
<>
82+
<React.Fragment key={groupName}>
5983
{groupName ?
6084
<GridItem className="row heading">{groupName}</GridItem>
6185
: null}
@@ -79,7 +103,6 @@ export function PropertySignatureTable({
79103
: null
80104
}
81105
suffix={property.optional ? <em> (optional)</em> : null}
82-
link={!!idPrefix}
83106
id={
84107
idPrefix ?
85108
`${idPrefix}-${property.displayName.toLowerCase()}`
@@ -94,7 +117,7 @@ export function PropertySignatureTable({
94117
parameterTypes
95118
arrow
96119
/>
97-
: property.type}
120+
: replaceGenericNames(property.type)}
98121
</MDX.inlineCode>
99122
</GridItem>
100123
<GridItem className="cell" fontSize="md" lineHeight="base">
@@ -109,7 +132,7 @@ export function PropertySignatureTable({
109132
</GridItem>
110133
</React.Fragment>
111134
))}
112-
</>
135+
</React.Fragment>
113136
)
114137
)}
115138
</Wrapper>
@@ -124,4 +147,5 @@ PropertySignatureTable.propTypes = {
124147
display: PropTypes.oneOf(["parent", "child"]),
125148
customOrder: PropTypes.arrayOf(PropTypes.string),
126149
idPrefix: PropTypes.string,
150+
genericNames: PropTypes.arrayOf(PropTypes.string),
127151
};

docs/shared/ApiDoc/getInterfaceReference.js

-8
This file was deleted.

docs/shared/ApiDoc/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ export { ParameterTable } from "./ParameterTable";
1515
export { PropertyDetails } from "./PropertyDetails";
1616
export { EnumDetails } from "./EnumDetails";
1717
export { ManualTuple } from "./Tuple";
18-
export { getInterfaceReference } from "./getInterfaceReference";
1918
export { SourceLink } from "./SourceLink";

netlify.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
npm run docmodel
1414
cd ../
1515
rm -rf monodocs
16-
git clone https://github.com/apollographql/docs --branch main --single-branch monodocs
16+
git clone https://github.com/apollographql/docs --branch pr/parse-ts --single-branch monodocs
1717
cd monodocs
1818
npm i
1919
cp -r ../docs local

0 commit comments

Comments
 (0)