Skip to content

Commit

Permalink
Add support for resolving utility types
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
Gerrit0 committed Mar 10, 2023
1 parent bf66864 commit 56d01c8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ This is probably missing links! If you set `--logLevel Verbose`, it will print o

Supports TypeDoc 0.23.x and 0.24.x

| Option | Default | Description |
| ------------------- | ------- | ------------------------------------------------------------------------------------------ |
| resolveUtilityTypes | `true` | Resolve links to `Partial`, `Omit`, etc. to their documentation on the TypeScript website. |

## Changelog

### v3.0.2 (2023-03-09)

- Add support for resolving TypeScript utility types (`Partial`, `Omit`, etc.) to links on the TypeScript website.
This can be turned off with the new `resolveUtilityTypes` option.

### v3.0.1 (2023-03-09)

- Add support for `{@link !NaN}` to link to global symbols, #4
Expand Down
51 changes: 40 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
import { Application, ExternalResolveResult } from "typedoc";
import {
Application,
Converter,
ExternalResolveResult,
ParameterType,
} from "typedoc";
import { resolveCanvasName } from "./canvas";
import { resolveCssName } from "./css";
import { resolveDomName } from "./dom";
import { resolveGlobalName } from "./globalObjects";
import { resolveTsType } from "./typescript";
import { resolveWebAudioName } from "./webaudio";

function resolveName(name: string) {
return (
resolveGlobalName(name) ??
resolveDomName(name) ??
resolveCssName(name) ??
resolveCanvasName(name) ??
resolveWebAudioName(name)
);
}

const version = Application.VERSION.split(/[\.-]/);
const supportsObjectReturn = +version[1] > 23 || +version[2] >= 26;

declare module "typedoc" {
export interface TypeDocOptionMap {
resolveUtilityTypes: boolean;
}
}

export function load(app: Application) {
const failed = new Set<string>();

app.options.addDeclaration({
name: "resolveUtilityTypes",
defaultValue: true,
help: "[typedoc-plugin-mdn-links]: Resolve references to Partial, Omit, etc to the TypeScript website.",
type: ParameterType.Boolean,
});

const resolvers = [
resolveGlobalName,
resolveDomName,
resolveCssName,
resolveCanvasName,
resolveWebAudioName,
];
function resolveName(name: string) {
for (const res of resolvers) {
const result = res(name);
if (result) return result;
}
}

app.converter.on(Converter.EVENT_BEGIN, () => {
if (app.options.getValue("resolveUtilityTypes")) {
resolvers.push(resolveTsType);
}
});

app.converter.addUnknownSymbolResolver((declaration) => {
if (
declaration.moduleSource === "typescript" ||
Expand Down
38 changes: 38 additions & 0 deletions src/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const utilityTypes = new Map([
["Awaited", "awaitedtype"],
["Partial", "partialtype"],
["Required", "requiredtype"],
["Readonly", "readonlytype"],
["Record", "recordkeys-type"],
["Pick", "picktype-keys"],
["Omit", "omittype-keys"],
["Exclude", "excludeuniontype-excludedmembers"],
["Extract", "extracttype-union"],
["NonNullable", "nonnullabletype"],
["Parameters", "parameterstype"],
["ConstructorParameters", "constructorparameterstype"],
["ReturnType", "returntypetype"],
["InstanceType", "instancetypetype"],
["ThisParameterType", "thisparametertypetype"],
["OmitThisParameter", "omitthisparametertype"],
["ThisType", "thistypetype"],
]);

const templateLiteralTypes = new Map([
["Uppercase", "uppercasestringtype"],
["Lowercase", "lowercasestringtype"],
["Capitalize", "capitalizestringtype"],
["Uncapitalize", "uncapitalizestringtype"],
]);

export function resolveTsType(name: string) {
const utilHash = utilityTypes.get(name);
if (utilHash) {
return `https://www.typescriptlang.org/docs/handbook/utility-types.html#${utilHash}`;
}

const templateHash = templateLiteralTypes.get(name);
if (templateHash) {
return `https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html#${templateHash}`;
}
}

0 comments on commit 56d01c8

Please sign in to comment.