From 535f3f32164665647ad0f3008d4d4e89fe461c23 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Mon, 10 Oct 2022 14:48:16 -0600 Subject: [PATCH] Move depth to type.tsx, fix some bugs Closes #1793 --- CHANGELOG.md | 5 ++++ src/lib/output/renderer.ts | 2 ++ .../default/DefaultThemeRenderContext.ts | 13 --------- .../output/themes/default/partials/type.tsx | 28 ++++++++++++------- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb2ede003..eaeaf29aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,17 @@ ### Features - Added support for specifying the tsconfig.json file in packages mode with `{ "typedoc": { "tsconfig": "tsconfig.lib.json" }}` in package.json, #2061. +- Object types will now be pretty printed, #1793. - Added support for specifying the base file url for links to source code, #2068. ### Bug Fixes - Private parameter properties will no longer be ignored, #2064. +### Thanks! + +- @captainTorch + ## v0.23.15 (2022-09-18) ### Features diff --git a/src/lib/output/renderer.ts b/src/lib/output/renderer.ts index 2c8c05f92..d9196fbc0 100644 --- a/src/lib/output/renderer.ts +++ b/src/lib/output/renderer.ts @@ -25,6 +25,7 @@ import { Reflection } from "../models"; import type { JsxElement } from "../utils/jsx.elements"; import type { DefaultThemeRenderContext } from "./themes/default/DefaultThemeRenderContext"; import { clearSeenIconCache } from "./themes/default/partials/icon"; +import { validateStateIsClean } from "./themes/default/partials/type"; /** * Describes the hooks available to inject output in the default theme. @@ -241,6 +242,7 @@ export class Renderer extends ChildableComponent< output.urls.forEach((mapping: UrlMapping) => { clearSeenIconCache(); this.renderDocument(output.createPageEvent(mapping)); + validateStateIsClean(mapping.url); }); this.trigger(RendererEvent.END, output); diff --git a/src/lib/output/themes/default/DefaultThemeRenderContext.ts b/src/lib/output/themes/default/DefaultThemeRenderContext.ts index 26f3400a1..af3c4928f 100644 --- a/src/lib/output/themes/default/DefaultThemeRenderContext.ts +++ b/src/lib/output/themes/default/DefaultThemeRenderContext.ts @@ -46,7 +46,6 @@ function bind(fn: (f: F, ...a: L) => R, first: F) { export class DefaultThemeRenderContext { options: Options; - private currentDepth = 0; constructor(private theme: DefaultTheme, options: Options) { this.options = options; @@ -54,18 +53,6 @@ export class DefaultThemeRenderContext { icons = icons; - getCurrentDepth(): number { - return this.currentDepth; - } - - incrementCurrentDepth(): void { - this.currentDepth++; - } - - decrementCurrentDepth(): void { - this.currentDepth--; - } - hook = (name: keyof RendererHooks) => this.theme.owner.hooks.emit(name, this); diff --git a/src/lib/output/themes/default/partials/type.tsx b/src/lib/output/themes/default/partials/type.tsx index 9e3b24db9..6531aed74 100644 --- a/src/lib/output/themes/default/partials/type.tsx +++ b/src/lib/output/themes/default/partials/type.tsx @@ -12,6 +12,7 @@ import { } from "../../../../models"; import { JSX } from "../../../../utils"; import { join, stringify } from "../../lib"; +import { ok } from "assert"; const EXPORTABLE: ReflectionKind = ReflectionKind.Class | @@ -67,11 +68,16 @@ function renderUniquePath(context: DefaultThemeRenderContext, reflection: Reflec )); } -function includeIndentation(context: DefaultThemeRenderContext): JSX.Element { - return context.getCurrentDepth() > 0 ? ( - {new Array(context.getCurrentDepth() * 4).fill("\u00A0").join("")} - ) : ( - <> + +let indentationDepth = 0; +function includeIndentation(): JSX.Element { + return indentationDepth > 0 ? {"\u00A0".repeat(indentationDepth * 4)} : <>; +} + +export function validateStateIsClean(page: string) { + ok( + indentationDepth === 0, + `Rendering ${page}: Indentation depth increment/decrement not matched: ${indentationDepth}` ); } @@ -287,7 +293,7 @@ const typeRenderers: { const members: JSX.Element[] = []; const children: DeclarationReflection[] = type.declaration.children || []; - if (children.length) context.incrementCurrentDepth(); + indentationDepth++; for (const item of children) { if (item.getSignature && item.setSignature) { @@ -353,6 +359,8 @@ const typeRenderers: { } if (!members.length && type.declaration.signatures?.length === 1) { + indentationDepth--; + return ( <> ( @@ -371,27 +379,27 @@ const typeRenderers: { if (members.length) { const membersWithSeparators = members.flatMap((m) => [ - includeIndentation(context), + includeIndentation(), m, ; ,

, ]); membersWithSeparators.pop(); - context.decrementCurrentDepth(); - + indentationDepth--; return ( <> {"{"}

{membersWithSeparators}

- {includeIndentation(context)} + {includeIndentation()} {"}"} ); } + indentationDepth--; return {"{}"}; }, rest(context, type) {