Skip to content

Commit

Permalink
refactor(website): remove unneccessary addPackageToModel function (#9983
Browse files Browse the repository at this point in the history
)

* types: fix links in @deprecated tags

* Merge branch 'main' into fix/deprecated-links-d.ts

* fix: searchIndices

* refactor: apply review suggestions

* refactor: remove addPackageToModel function

* fix: event links in search index

* fix: wrong overload condition
  • Loading branch information
Qjuh authored Nov 21, 2023
1 parent ce0be39 commit 3d1c884
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ApiVariable,
ApiFunction,
} from '@discordjs/api-extractor-model';
import { ApiItemKind, ApiModel } from '@discordjs/api-extractor-model';
import { ApiItemKind, ApiModel, ApiPackage } from '@discordjs/api-extractor-model';
import { tryResolveSummaryText } from '@discordjs/scripts';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
Expand All @@ -24,7 +24,6 @@ import { TypeAlias } from '~/components/model/TypeAlias';
import { Variable } from '~/components/model/Variable';
import { Enum } from '~/components/model/enum/Enum';
import { Function } from '~/components/model/function/Function';
import { addPackageToModel } from '~/util/addPackageToModel';
import { OVERLOAD_SEPARATOR } from '~/util/constants';
import { fetchMember } from '~/util/fetchMember';
import { findMember } from '~/util/model';
Expand All @@ -44,7 +43,8 @@ async function fetchHeadMember({ package: packageName, version, item }: ItemRout
return undefined;
}

const model = addPackageToModel(new ApiModel(), modelJSON);
const model = new ApiModel();
model.addMember(ApiPackage.loadFromJson(modelJSON));
const pkg = model.tryGetPackageByName(packageName);
const entry = pkg?.entryPoints[0];

Expand Down Expand Up @@ -129,7 +129,8 @@ export async function generateStaticParams({ params: { package: packageName, ver
return [];
}

const model = addPackageToModel(new ApiModel(), modelJSON);
const model = new ApiModel();
model.addMember(ApiPackage.loadFromJson(modelJSON));

const pkg = model.tryGetPackageByName(packageName);
const entry = pkg?.entryPoints[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ApiFunction, ApiItem } from '@discordjs/api-extractor-model';
import { ApiModel } from '@discordjs/api-extractor-model';
import { ApiModel, ApiPackage } from '@discordjs/api-extractor-model';
import dynamic from 'next/dynamic';
import { notFound } from 'next/navigation';
import type { PropsWithChildren } from 'react';
Expand All @@ -9,7 +9,6 @@ import { Nav } from '~/components/Nav';
import { Outline } from '~/components/Outline';
import type { SidebarSectionItemData } from '~/components/Sidebar';
import { resolveItemURI } from '~/components/documentation/util';
import { addPackageToModel } from '~/util/addPackageToModel';
import { N_RECENT_VERSIONS, PACKAGES } from '~/util/constants';
import { Providers } from './providers';

Expand Down Expand Up @@ -57,7 +56,8 @@ export default async function PackageLayout({ children, params }: PropsWithChild
notFound();
}

const model = addPackageToModel(new ApiModel(), modelJSON);
const model = new ApiModel();
model.addMember(ApiPackage.loadFromJson(modelJSON));

const pkg = model.tryGetPackageByName(params.package);

Expand Down
6 changes: 0 additions & 6 deletions apps/website/src/util/addPackageToModel.ts

This file was deleted.

5 changes: 2 additions & 3 deletions apps/website/src/util/fetchMember.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ApiModel, ApiFunction } from '@discordjs/api-extractor-model';
import { ApiModel, ApiFunction, ApiPackage } from '@discordjs/api-extractor-model';
import { fetchModelJSON } from '~/app/docAPI';
import { addPackageToModel } from './addPackageToModel';
import { OVERLOAD_SEPARATOR, PACKAGES } from './constants';
import { findMember, findMemberByKey } from './model';

Expand All @@ -21,7 +20,7 @@ export const fetchMember = async (packageName: string, branchName: string, item?
return null;
}

addPackageToModel(model, modelJSON);
model.addMember(ApiPackage.loadFromJson(modelJSON));

const [memberName, overloadIndex] = decodeURIComponent(item).split(OVERLOAD_SEPARATOR);

Expand Down
24 changes: 16 additions & 8 deletions packages/api-extractor-utils/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
type Parameter,
type ApiFunction,
ApiDeclaredItem,
type ApiMethod,
type ApiMethodSignature,
} from '@discordjs/api-extractor-model';
import type { DocNode, DocParagraph, DocPlainText } from '@microsoft/tsdoc';
import { type Meaning, ModuleSource } from '@microsoft/tsdoc/lib-commonjs/beta/DeclarationReference.js';
Expand All @@ -23,6 +25,10 @@ export function findPackage(model: ApiModel, name: string): ApiPackage | undefin
| undefined;
}

function hasOverloadIndex(item: ApiItem): item is ApiFunction | ApiMethod | ApiMethodSignature {
return 'overloadIndex' in item;
}

export function generatePath(items: readonly ApiItem[], version: string) {
let path = '/docs/packages';

Expand All @@ -36,17 +42,19 @@ export function generatePath(items: readonly ApiItem[], version: string) {
path += `/${item.displayName}`;
break;
case ApiItemKind.Function:
// eslint-disable-next-line no-case-declarations
const functionItem = item as ApiFunction;
path += `/${functionItem.displayName}${
functionItem.overloadIndex && functionItem.overloadIndex > 1 ? `:${functionItem.overloadIndex}` : ''
path += `/${item.displayName}${
hasOverloadIndex(item) && item.overloadIndex > 1 ? `:${item.overloadIndex}` : ''
}:${item.kind}`;
break;
case ApiItemKind.Property:
case ApiItemKind.Method:
case ApiItemKind.MethodSignature:
path += `#${item.displayName}${
hasOverloadIndex(item) && item.overloadIndex > 1 ? `:${item.overloadIndex}` : ''
}`;
break;
case ApiItemKind.Property:
case ApiItemKind.PropertySignature:
// TODO: Take overloads into account
case ApiItemKind.Event:
path += `#${item.displayName}`;
break;
default:
Expand All @@ -55,8 +63,8 @@ export function generatePath(items: readonly ApiItem[], version: string) {
}

return path.includes('@discordjs/')
? path.replace(/@discordjs\/(.*)\/(.*)?/, `$1/${version}/$2`)
: path.replace(/(.*)\/(.*)?/, `$1/${version}/$2`);
? path.replace(/@discordjs\/(?<package>.*)\/(?<member>.*)?/, `$<package>/${version}/$<member>`)
: path.replace(/(?<oackage>.*)\/(?<member>.*)?/, `$<package>/${version}/$<member>`);
}

export function resolveDocComment(item: ApiDeclaredItem) {
Expand Down
8 changes: 2 additions & 6 deletions packages/scripts/src/generateIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export const PACKAGES = [
];
let idx = 0;

export function addPackageToModel(model: ApiModel, data: any) {
model.addMember(ApiPackage.loadFromJson(data));
return model;
}

/**
* Attempts to resolve the summary text for the given item.
*
Expand Down Expand Up @@ -164,7 +159,8 @@ export async function generateAllIndices({
idx = 0;

const data = await fetchPackageVersionDocs(pkg, version);
const model = addPackageToModel(new ApiModel(), data);
const model = new ApiModel();
model.addMember(ApiPackage.loadFromJson(data));
const members = visitNodes(model.tryGetPackageByName(pkg)!.entryPoints[0]!, version);

const sanitizePackageName = pkg.replaceAll('.', '-');
Expand Down

1 comment on commit 3d1c884

@vercel
Copy link

@vercel vercel bot commented on 3d1c884 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.