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

feat: add Python-specific type helpers (Unpack, NotRequired etc.) #18

Merged
merged 2 commits into from
Oct 31, 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
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apify/docusaurus-plugin-typedoc-api",
"version": "4.2.6",
"version": "4.2.7",
"description": "Docusaurus plugin that provides source code API documentation powered by TypeDoc. ",
"keywords": [
"docusaurus",
Expand Down
49 changes: 46 additions & 3 deletions packages/plugin/src/components/MemberSignatureBody.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// https://github.com/TypeStrong/typedoc-default-themes/blob/master/src/default/partials/member.signature.body.hbs

import { Fragment } from 'react'
import { Fragment, useContext } from 'react'
import type { JSONOutput, Models } from 'typedoc';
import { type GlobalData } from '@docusaurus/types';
import { usePluginData } from '@docusaurus/useGlobalData';
import { useMinimalLayout } from '../hooks/useMinimalLayout';
import type { TSDSignatureReflection } from '../types';
import { ApiDataContext } from './ApiDataContext';
import { Comment, hasComment } from './Comment';
import { CommentBadges, isCommentWithModifiers } from './CommentBadges';
import { DefaultValue } from './DefaultValue';
Expand Down Expand Up @@ -61,6 +64,46 @@ export function MemberSignatureBody({ hideSources, sig }: MemberSignatureBodyPro
const showParams = !minimal && sig.parameters && sig.parameters.length > 0;
const showReturn = !minimal && sig.type;

const { reflections } = useContext(ApiDataContext);
const { isPython } = usePluginData('docusaurus-plugin-typedoc-api') as GlobalData;


if (isPython) {
// eslint-disable-next-line
sig.parameters = sig.parameters?.reduce<typeof sig.parameters>((acc, param) => {
// @ts-expect-error Silence ts errors
switch (param.type?.name) {
case 'Unpack':
// @ts-expect-error Silence ts errors
// eslint-disable-next-line
acc.push(...reflections[param.type.typeArguments[0].target].children.map(x => ({...x, flags: {'keyword-only': true}})));
break;
default:
acc.push(param);
break;
}

return acc;
}, []);

// eslint-disable-next-line
sig.parameters = sig.parameters?.reduce<typeof sig.parameters>((acc, param) => {
// @ts-expect-error Silence ts errors
switch (param.type?.name) {
case 'NotRequired':
// @ts-expect-error Silence ts errors
// eslint-disable-next-line
acc.push({...param, type: param.type.typeArguments[0]});
break;
default:
acc.push(param);
break;
}

return acc;
}, []);
}

return (
<>
{!hideSources && <MemberSources reflection={sig} />}
Expand Down Expand Up @@ -126,8 +169,8 @@ export function MemberSignatureBody({ hideSources, sig }: MemberSignatureBodyPro
)}

{param.type?.type === 'union' && (
(((param.type as unknown as Models.UnionType).types.filter(
(unionType) => unionType.type === 'reflection')) as Models.ReflectionType[]).map(
((param.type.types.filter(
(unionType) => unionType.type === 'reflection')) as unknown as Models.ReflectionType[]).map(
(unionReflectionType) => (
<ul key={unionReflectionType.declaration.id}>
{unionReflectionType.declaration?.children?.map((unionChild) => (
Expand Down
19 changes: 17 additions & 2 deletions packages/plugin/src/components/Type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { Fragment } from 'react';
import type { JSONOutput } from 'typedoc';
import Link from '@docusaurus/Link';
import { type GlobalData } from '@docusaurus/types';
import { usePluginData } from '@docusaurus/useGlobalData';
import { useReflectionMap } from '../hooks/useReflectionMap';
import type { TSDDeclarationReflection } from '../types';
import { MemberSignatureTitle } from './MemberSignatureTitle';
Expand Down Expand Up @@ -40,6 +42,7 @@ export interface TypeProps {
// eslint-disable-next-line complexity
export function Type({ needsParens = false, type: base }: TypeProps) {
const reflections = useReflectionMap();
const { isPython } = usePluginData('docusaurus-plugin-typedoc-api') as GlobalData;

if (!base) {
return null;
Expand Down Expand Up @@ -124,6 +127,10 @@ export function Type({ needsParens = false, type: base }: TypeProps) {
case 'literal': {
const type = base as JSONOutput.LiteralType;

if (isPython && type.value === null) {
return <span className="tsd-signature-type">None</span>;
}

return <span className="tsd-signature-type">{String(type.value)}</span>;
}

Expand Down Expand Up @@ -223,14 +230,22 @@ export function Type({ needsParens = false, type: base }: TypeProps) {
)}
{type.typeArguments && type.typeArguments.length > 0 && (
<>
<span className="tsd-signature-symbol">&lt;</span>
<span className="tsd-signature-symbol">
{
isPython ? '[' : '<'
}
</span>
{type.typeArguments.map((t, i) => (
<Fragment key={t.type + i}>
{i > 0 && <span className="tsd-signature-symbol">, </span>}
<Type type={t} />
</Fragment>
))}
<span className="tsd-signature-symbol">&gt;</span>
<span className="tsd-signature-symbol">
{
isPython ? ']' : '>'
}
</span>
</>
)}
</>
Expand Down
Loading