Skip to content

Commit 7d37fe5

Browse files
committed
feat: 🎸 rendering improvements
1 parent 3e2ceb1 commit 7d37fe5

32 files changed

+1200
-1655
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"clean": "npx rimraf dist",
1717
"test": "jest --watchAll --maxWorkers=3",
1818
"test:pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache --detectOpenHandles --forceExit",
19+
"test:update-snapshot": "jest --watchAll --maxWorkers=3 --updateSnapshot",
1920
"start": "tsc && (concurrently \"tsc -w\" \"tsc-alias -w\")",
2021
"build": "yarn clean && tsc -p tsconfig.build.json && tsc-alias",
2122
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",

packages/core/src/docs/docs.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { PluginOptions, PkgMeta } from "../types/package.types";
66
import { pluginOptionsPath, packageConfigPath } from "../constants/paths.constants";
77
import { cleanFileName } from "../parsing/generator/utils/file.utils";
88
import { getPackageDocsPath } from "../parsing/generator/utils/package.utils";
9-
import { ComponentsProps } from "importer/components/component.types";
10-
import { getComponent } from "importer/components/component-map.utils";
9+
import { DocsComponents, getComponent, GetComponentProps } from "importer/components/component-map.utils";
1110
import { getFile, getMatchingElement } from "importer/utils/docs.utils";
1211

1312
// TODO FIGURE OUT HOW TO MAKE THIS WORK
@@ -18,7 +17,9 @@ const file = {} as any;
1817
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1918
const options = {} as any;
2019

21-
export const Docs = (props: ComponentsProps & { packageName: string; componentName: string }) => {
20+
export const Docs = (
21+
props: GetComponentProps<keyof DocsComponents> & { packageName: string; componentName: string },
22+
) => {
2223
const { packageName, componentName } = props;
2324

2425
const currentVersionedDir = file.history[0]
Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import React from "react";
3-
4-
import { PagePropsType } from "../../types/page.types";
51
import { Definition } from "../../pages/components/definition";
62
import { Returns } from "../../pages/components/returns";
73
import { Generics } from "../../pages/components/generics";
@@ -18,76 +14,44 @@ import { Description } from "../../pages/components/description";
1814
import { Signature } from "../../pages/components/signature";
1915
import { Sources } from "../../pages/components/sources";
2016
import { Type } from "../../pages/components/type";
21-
import { ComponentsProps } from "./component.types";
2217
import { ReturnsPreview } from "../../pages/components/returns-preview";
2318
import { Npm } from "pages/components/npm";
2419
import { Details } from "pages/components/details";
2520

26-
export const getComponent = (options: ComponentsProps): React.FC<PagePropsType> => {
21+
const components = {
22+
definition: Definition,
23+
npm: Npm,
24+
import: Import,
25+
details: Details,
26+
description: Description,
27+
generics: Generics,
28+
method: Method,
29+
methods: Methods,
30+
name: Name,
31+
parameter: Parameter,
32+
parameters: Parameters,
33+
preview: Preview,
34+
property: Property,
35+
properties: Properties,
36+
returns: Returns,
37+
returnsPreview: ReturnsPreview,
38+
signature: Signature,
39+
sources: Sources,
40+
type: Type,
41+
};
42+
43+
export type DocsComponents = typeof components;
44+
45+
export type GetComponentProps<Name extends keyof DocsComponents> = {
46+
type: Name;
47+
} & React.ComponentPropsWithoutRef<(typeof components)[Name]>;
48+
49+
export const getComponent = <Name extends keyof DocsComponents>(options: GetComponentProps<Name>) => {
2750
const { type: componentType } = options;
28-
switch (componentType) {
29-
case "definition": {
30-
return Definition;
31-
}
32-
case "npm": {
33-
return Npm;
34-
}
35-
case "import": {
36-
return Import;
37-
}
38-
case "details": {
39-
return Details;
40-
}
41-
case "description": {
42-
return Description;
43-
}
44-
case "generics": {
45-
return Generics as any;
46-
}
47-
case "method": {
48-
return Method;
49-
}
50-
case "methods": {
51-
return Methods;
52-
}
53-
case "name": {
54-
return Name;
55-
}
56-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
57-
// @ts-ignore
58-
case "parameter": {
59-
return Parameter;
60-
}
61-
case "parameters": {
62-
return Parameters;
63-
}
64-
case "preview": {
65-
return Preview as any;
66-
}
67-
case "property": {
68-
return Property;
69-
}
70-
case "properties": {
71-
return Properties;
72-
}
73-
case "returns": {
74-
return Returns;
75-
}
76-
case "returnsPreview": {
77-
return ReturnsPreview;
78-
}
79-
case "signature": {
80-
return Signature as any;
81-
}
82-
case "sources": {
83-
return Sources;
84-
}
85-
case "type": {
86-
return Type as React.FC<PagePropsType>;
87-
}
88-
// Todo display page
89-
default: {
90-
throw new Error(`Component type not found - ${componentType}`);
91-
}
51+
52+
if (!components[componentType]) {
53+
throw new Error(`Component type not found - ${componentType}`);
9254
}
55+
56+
return components[componentType];
9357
};

packages/core/src/importer/components/component.types.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

packages/core/src/importer/importer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "path";
2+
import React from "react";
23
// TS fails to import it at build
34
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
45
// @ts-ignore
@@ -13,11 +14,10 @@ import { parse } from "@reins/query-params";
1314
import { PluginOptions, PkgMeta } from "../types/package.types";
1415
import { pluginOptionsPath, packageConfigPath } from "../constants/paths.constants";
1516
import { getFile, getMatchingElement } from "./utils/docs.utils";
16-
import { getComponent } from "./components/component-map.utils";
17+
import { DocsComponents, getComponent, GetComponentProps } from "./components/component-map.utils";
1718
import { cleanFileName } from "../parsing/generator/utils/file.utils";
1819
import { renderer } from "../parsing/renderer/renderer";
1920
import { getPackageDocsPath } from "../parsing/generator/utils/package.utils";
20-
import { ComponentsProps } from "./components/component.types";
2121

2222
function escapeRegExp(string: string) {
2323
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
@@ -135,9 +135,9 @@ export const importer = (options: { packageRoute: string; apiDir: string; versio
135135
if (optionsNormalization[optionsNormalization.length - 1] === "&") {
136136
optionsNormalization = optionsNormalization.slice(0, -1);
137137
}
138-
const parsedOptions: ComponentsProps = parse(optionsNormalization) as ComponentsProps;
138+
const parsedOptions = parse(optionsNormalization) as GetComponentProps<keyof DocsComponents>;
139139
const Component = getComponent(parsedOptions);
140-
const html = renderer(Component, {
140+
const html = renderer(Component as React.FC, {
141141
...parsedOptions,
142142
reflection,
143143
reflectionsTree: packagesReflections,

packages/core/src/pages/components/description.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const Description: React.FC<PagePropsType & DescriptionProps> = (props) =
1212
const { reflection, className = "" } = props;
1313

1414
const signature = getSignature(reflection);
15+
const children = getCommentNode(signature || reflection) || getCommentNode(reflection);
1516

16-
return <div className={`api-docs__description ${className}`}>{getCommentNode(signature || reflection)}</div>;
17+
return <div className={`api-docs__description ${className}`}>{children}</div>;
1718
};

packages/core/src/pages/components/examples.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22

3-
import { getTag } from "pages/handlers/tags";
3+
import { getTags } from "pages/handlers/tags";
44
import { getSignature } from "pages/utils/signature.utils";
55
import { PagePropsType } from "types/page.types";
66
import { HeadingType } from "types/components.types";
@@ -17,7 +17,7 @@ export const Examples: React.FC<PagePropsType & ExamplesProps> = (props) => {
1717

1818
const signature = getSignature(reflection);
1919
const comment = signature?.comment || reflection.comment;
20-
const examples = getTag(comment, "@example");
20+
const examples = getTags(comment, "@example");
2121

2222
if (!examples.length) {
2323
return null;

packages/core/src/pages/components/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export * from "./returns-preview";
1919
export * from "./returns";
2020
export * from "./section";
2121
export * from "./separator";
22-
export * from "./signature";
2322
export * from "./sources";
2423
export * from "./tag";
2524
export * from "./type";

packages/core/src/pages/components/method.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type MethodProps = {
2121
};
2222

2323
export const Method: React.FC<PagePropsType & MethodProps> = (props) => {
24-
const { reflection, className = "", display, withParams = false } = props;
24+
const { reflection, reflectionsTree, className = "", display, withParams = false } = props;
2525
const { name } = reflection;
2626
const methodSignature = getSignature(reflection);
2727

@@ -43,6 +43,7 @@ export const Method: React.FC<PagePropsType & MethodProps> = (props) => {
4343
pageProps={props}
4444
title="Parameters"
4545
reflections={methodSignature.parameters}
46+
reflectionsTree={reflectionsTree}
4647
/>
4748
);
4849
}
@@ -59,6 +60,7 @@ export const Method: React.FC<PagePropsType & MethodProps> = (props) => {
5960
pageProps={props}
6061
title="Parameters"
6162
reflections={methodSignature.parameters}
63+
reflectionsTree={reflectionsTree}
6264
/>
6365
);
6466
}
@@ -82,7 +84,7 @@ export const Method: React.FC<PagePropsType & MethodProps> = (props) => {
8284
<Section headingSize="h6" title="Parameters">
8385
<Parameters {...props} headingSize="h4" />
8486
</Section>
85-
<Section headingSize="h6" title="Return">
87+
<Section headingSize="h6" title="Returns">
8688
<Returns {...props} />
8789
</Section>
8890
<hr />

packages/core/src/pages/components/methods.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from "react";
33
import { PagePropsType } from "../../types/page.types";
44
import { getMethods } from "../utils/methods.utils";
55
import { Method } from "./method";
6-
import { getTypesArray } from "../utils/properties.utils";
6+
import { getTypes } from "../utils/properties.utils";
77
import { DisplayType } from "types/components.types";
88
import { Table } from "../modules/table/table";
99
import { Grid } from "../modules/grid/grid";
@@ -15,7 +15,7 @@ export type MethodsProps = {
1515

1616
export const Methods: React.FC<PagePropsType & MethodsProps> = (props) => {
1717
const { reflection, reflectionsTree, className = "", display } = props;
18-
const children = getTypesArray(reflectionsTree, reflection);
18+
const children = getTypes(reflectionsTree, reflection);
1919

2020
if (!children) return null;
2121

@@ -25,13 +25,25 @@ export const Methods: React.FC<PagePropsType & MethodsProps> = (props) => {
2525

2626
if (display === "grid") {
2727
return (
28-
<Grid className={`api-docs__methods ${className}`} pageProps={props} title="Methods" reflections={methods} />
28+
<Grid
29+
className={`api-docs__methods ${className}`}
30+
pageProps={props}
31+
title="Methods"
32+
reflections={methods}
33+
reflectionsTree={reflectionsTree}
34+
/>
2935
);
3036
}
3137

3238
if (display === "table") {
3339
return (
34-
<Table className={`api-docs__methods ${className}`} pageProps={props} title="Methods" reflections={methods} />
40+
<Table
41+
className={`api-docs__methods ${className}`}
42+
pageProps={props}
43+
title="Methods"
44+
reflections={methods}
45+
reflectionsTree={reflectionsTree}
46+
/>
3547
);
3648
}
3749

0 commit comments

Comments
 (0)