Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Replace Generics in PropertySignatureTable" #11549

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/shared/ApiDoc/EnumDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function EnumDetails({
mr="1"
as={Text}
since
link
id={`${item.displayName.toLowerCase()}-member-${member.displayName.toLowerCase()}`}
/>
<DocBlock
Expand Down
28 changes: 12 additions & 16 deletions docs/shared/ApiDoc/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
PropertySignatureTable,
SourceLink,
Example,
getInterfaceReference,
} from ".";
import { GridItem } from "@chakra-ui/react";
export function FunctionSignature({
Expand All @@ -21,8 +22,7 @@ export function FunctionSignature({
}) {
const MDX = useMDXComponents();
const getItem = useApiDocContext();
const { displayName, parameters, returnType, typeParameters } =
getItem(canonicalReference);
const { displayName, parameters, returnType } = getItem(canonicalReference);

let paramSignature = parameters
.map((p) => {
Expand All @@ -41,16 +41,9 @@ export function FunctionSignature({
paramSignature = "\n " + paramSignature + "\n";
}

const genericSignature =
typeParameters?.length ?
`<${typeParameters.map((p) => p.name).join(", ")}>`
: "";

const signature = `${arrow ? "" : "function "}${
name ? displayName : ""
}${genericSignature}(${paramSignature})${arrow ? " =>" : ":"} ${
returnType.type
}`;
}(${paramSignature})${arrow ? " =>" : ":"} ${returnType}`;

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

const interfaceReference = getInterfaceReference(
item.returnType,
item,
getItem
);
return (
<>
{item.comment?.returns}
<MDX.pre language="ts">
<code className="language-ts">{item.returnType.type}</code>
<code className="language-ts">{item.returnType}</code>
</MDX.pre>
{item.returnType.primaryCanonicalReference?.endsWith(":interface") ?
{interfaceReference ?
<details>
<GridItem as="summary" className="row">
Show/hide child attributes
</GridItem>
<PropertySignatureTable
canonicalReference={item.returnType.primaryCanonicalReference}
genericNames={item.returnType.primaryGenericArguments}
canonicalReference={interfaceReference.canonicalReference}
idPrefix={`${item.displayName.toLowerCase()}-result`}
/>
</details>
Expand Down Expand Up @@ -156,8 +153,7 @@ export function FunctionDetails({
</>
)}
{(
result === false ||
(result === undefined && item.returnType.type === "void")
result === false || (result === undefined && item.returnType === "void")
) ?
null
: <>
Expand Down
19 changes: 12 additions & 7 deletions docs/shared/ApiDoc/ParameterTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { useMDXComponents } from "@mdx-js/react";
import PropTypes from "prop-types";
import React from "react";
import { GridItem, Text } from "@chakra-ui/react";
import { PropertySignatureTable, useApiDocContext } from ".";
import {
PropertySignatureTable,
SectionHeading,
getInterfaceReference,
useApiDocContext,
} from ".";
import { ResponsiveGrid } from "./ResponsiveGrid";

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

{item.parameters.map((parameter) => {
const interfaceReference =
parameter.primaryCanonicalReference?.endsWith(":interface") ?
parameter.primaryCanonicalReference
: undefined;
const interfaceReference = getInterfaceReference(
parameter.type,
item,
getItem
);
const id = `${item.displayName.toLowerCase()}-parameters-${parameter.name.toLowerCase()}`;

return (
Expand Down Expand Up @@ -62,8 +68,7 @@ export function ParameterTable({ canonicalReference }) {
Show/hide child attributes
</GridItem>
<PropertySignatureTable
canonicalReference={interfaceReference}
genericNames={parameter.primaryGenericArguments}
canonicalReference={interfaceReference.canonicalReference}
display="child"
idPrefix={id}
/>
Expand Down
32 changes: 4 additions & 28 deletions docs/shared/ApiDoc/PropertySignatureTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function PropertySignatureTable({
display = "parent",
customOrder = [],
idPrefix = "",
genericNames,
}) {
const MDX = useMDXComponents();
const getItem = useApiDocContext();
Expand All @@ -37,29 +36,6 @@ export function PropertySignatureTable({
);
}

const replaceGenericNames = React.useMemo(() => {
if (!genericNames) return (str) => str;

const replacements = {};
item.typeParameters.forEach((p, i) => {
if (genericNames[i] === p.name) return;
replacements[p.name] = genericNames[i];
});
if (!Object.values(replacements).length) return (str) => str;

const genericReplacementRegex = new RegExp(
`\\b(${Object.keys(replacements).join("|")})\\b`,
"g"
);
function replace(match) {
console.log({ match, replacement: replacements[match] });
return replacements[match] || match;
}
return function replaceGenericNames(str) {
return str.replace(genericReplacementRegex, replace);
};
});

return (
<>
{item.childrenIncomplete ?
Expand All @@ -79,7 +55,7 @@ export function PropertySignatureTable({
: null}
{Object.entries(groupedProperties).map(
([groupName, sortedProperties]) => (
<React.Fragment key={groupName}>
<>
{groupName ?
<GridItem className="row heading">{groupName}</GridItem>
: null}
Expand All @@ -103,6 +79,7 @@ export function PropertySignatureTable({
: null
}
suffix={property.optional ? <em> (optional)</em> : null}
link={!!idPrefix}
id={
idPrefix ?
`${idPrefix}-${property.displayName.toLowerCase()}`
Expand All @@ -117,7 +94,7 @@ export function PropertySignatureTable({
parameterTypes
arrow
/>
: replaceGenericNames(property.type)}
: property.type}
</MDX.inlineCode>
</GridItem>
<GridItem className="cell" fontSize="md" lineHeight="base">
Expand All @@ -132,7 +109,7 @@ export function PropertySignatureTable({
</GridItem>
</React.Fragment>
))}
</React.Fragment>
</>
)
)}
</Wrapper>
Expand All @@ -147,5 +124,4 @@ PropertySignatureTable.propTypes = {
display: PropTypes.oneOf(["parent", "child"]),
customOrder: PropTypes.arrayOf(PropTypes.string),
idPrefix: PropTypes.string,
genericNames: PropTypes.arrayOf(PropTypes.string),
};
8 changes: 8 additions & 0 deletions docs/shared/ApiDoc/getInterfaceReference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getInterfaceReference(type, item, getItem) {
const baseType = type.replace(/\b(Partial|Omit|Promise)</g, "").split("<")[0];
const reference = getItem(
item.references?.find((r) => r.text === baseType)?.canonicalReference,
false
);
return reference?.kind === "Interface" ? reference : null;
}
1 change: 1 addition & 0 deletions docs/shared/ApiDoc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { ParameterTable } from "./ParameterTable";
export { PropertyDetails } from "./PropertyDetails";
export { EnumDetails } from "./EnumDetails";
export { ManualTuple } from "./Tuple";
export { getInterfaceReference } from "./getInterfaceReference";
export { SourceLink } from "./SourceLink";
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
npm run docmodel
cd ../
rm -rf monodocs
git clone https://github.com/apollographql/docs --branch pr/parse-ts --single-branch monodocs
git clone https://github.com/apollographql/docs --branch main --single-branch monodocs
cd monodocs
npm i
cp -r ../docs local
Expand Down
Loading