Skip to content

Commit c907e39

Browse files
authored
refactor ts info extract (using ts-morph); support complex type. (#124)
1 parent 188a8a5 commit c907e39

File tree

15 files changed

+498
-210
lines changed

15 files changed

+498
-210
lines changed

packages/create-project/template-lib-monorepo/packages/button/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ You can import demos like this:
1717

1818
<Demo src="./demos/demo2.tsx" />
1919

20-
## Extract API info from Typescript code
20+
## Extract Type info from Typescript code
2121

22-
You can extract API from Typescript interface and render it into page.
22+
You can extract Typescript type info and render it into page. This is very useful for API documentation.
23+
24+
### Render Interface
2325

2426
The following markdown
2527

@@ -33,7 +35,19 @@ will result in:
3335

3436
<TsInfo src="./src/types.ts" name="ButtonProps" />
3537

36-
In jsx page, You can render TsInfo like this:
38+
### Render Type Alias
39+
40+
Besides interface, TsInfo API also support type alias.
41+
42+
SomeObjectLiteralType (Object Literal):
43+
<TsInfo src="./src/types.ts" name="SomeObjectLiteralType" />
44+
45+
SomeComplexType (Complex Type):
46+
<TsInfo src="./src/types.ts" name="SomeComplexType" />
47+
48+
### Using TsInfo API in JS files
49+
50+
In jsx page, You can import and render tsInfo like this:
3751

3852
```tsx
3953
import tsInfoData from './types.ts?tsInfo=ButtonProps'

packages/create-project/template-lib-monorepo/packages/button/src/types.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This is the description of the Button component's props
33
*/
4-
export interface ButtonProps {
4+
export interface ButtonProps<TestGenerics extends string> extends Base {
55
/**
66
* the type of button
77
* @defaultValue 'default'
@@ -11,7 +11,7 @@ export interface ButtonProps {
1111
* the size of button
1212
* @defaultValue 'middle'
1313
*/
14-
size?: 'large' | 'middle' | 'small'
14+
size?: 'large' | 'middle' | 'small' | TestGenerics
1515
/**
1616
* loading state of button
1717
* @defaultValue false
@@ -21,8 +21,46 @@ export interface ButtonProps {
2121
* click handler
2222
*/
2323
onClick?: (event: React.MouseEvent) => void
24+
/** test method declaration */
25+
testMethod(param: string): void
26+
/** test required property */
27+
testRequired: boolean
28+
}
29+
30+
interface Base {
2431
/**
25-
* button content
32+
* children content
2633
*/
2734
children: React.ReactNode
2835
}
36+
37+
export type SomeObjectLiteralType<TestGenerics> = {
38+
/**
39+
* the type of button
40+
* @defaultValue 'default'
41+
*/
42+
type?: 'primary' | 'default' | 'text'
43+
/**
44+
* the size of button
45+
* @defaultValue 'middle'
46+
*/
47+
size?: 'large' | 'middle' | 'small' | TestGenerics
48+
/**
49+
* loading state of button
50+
* @defaultValue false
51+
*/
52+
loading?: boolean
53+
/**
54+
* click handler
55+
*/
56+
onClick?: (event: React.MouseEvent) => void
57+
/** test method declaration */
58+
testMethod(param: string): void
59+
/** test required property */
60+
testRequired: boolean
61+
}
62+
63+
/**
64+
* Description of SomeComplexType ...
65+
*/
66+
export type SomeComplexType = 0 | 1 | 'a' | 'b' | { key: string }

packages/create-project/template-lib/src/Button/README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ You can import demos like this:
1717

1818
<Demo src="./demos/demo2.tsx" />
1919

20-
## Extract API info from Typescript code
20+
## Extract Type info from Typescript code
2121

22-
You can extract API from Typescript interface and render it into page.
22+
You can extract Typescript type info and render it into page. This is very useful for API documentation.
23+
24+
### Render Interface
2325

2426
The following markdown
2527

@@ -33,7 +35,19 @@ will result in:
3335

3436
<TsInfo src="./types.ts" name="ButtonProps" />
3537

36-
In jsx page, You can render TsInfo like this:
38+
### Render Type Alias
39+
40+
Besides interface, TsInfo API also support type alias.
41+
42+
SomeObjectLiteralType (Object Literal):
43+
<TsInfo src="./types.ts" name="SomeObjectLiteralType" />
44+
45+
SomeComplexType (Complex Type):
46+
<TsInfo src="./types.ts" name="SomeComplexType" />
47+
48+
### Using TsInfo API in JS files
49+
50+
In jsx page, You can import and render tsInfo like this:
3751

3852
```tsx
3953
import tsInfoData from './types.ts?tsInfo=ButtonProps'

packages/create-project/template-lib/src/Button/types.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This is the description of the Button component's props
33
*/
4-
export interface ButtonProps {
4+
export interface ButtonProps<TestGenerics extends string> extends Base {
55
/**
66
* the type of button
77
* @defaultValue 'default'
@@ -11,7 +11,7 @@ export interface ButtonProps {
1111
* the size of button
1212
* @defaultValue 'middle'
1313
*/
14-
size?: 'large' | 'middle' | 'small'
14+
size?: 'large' | 'middle' | 'small' | TestGenerics
1515
/**
1616
* loading state of button
1717
* @defaultValue false
@@ -21,8 +21,46 @@ export interface ButtonProps {
2121
* click handler
2222
*/
2323
onClick?: (event: React.MouseEvent) => void
24+
/** test method declaration */
25+
testMethod(param: string): void
26+
/** test required property */
27+
testRequired: boolean
28+
}
29+
30+
interface Base {
2431
/**
25-
* button content
32+
* children content
2633
*/
2734
children: React.ReactNode
2835
}
36+
37+
export type SomeObjectLiteralType<TestGenerics> = {
38+
/**
39+
* the type of button
40+
* @defaultValue 'default'
41+
*/
42+
type?: 'primary' | 'default' | 'text'
43+
/**
44+
* the size of button
45+
* @defaultValue 'middle'
46+
*/
47+
size?: 'large' | 'middle' | 'small' | TestGenerics
48+
/**
49+
* loading state of button
50+
* @defaultValue false
51+
*/
52+
loading?: boolean
53+
/**
54+
* click handler
55+
*/
56+
onClick?: (event: React.MouseEvent) => void
57+
/** test method declaration */
58+
testMethod(param: string): void
59+
/** test required property */
60+
testRequired: boolean
61+
}
62+
63+
/**
64+
* Description of SomeComplexType ...
65+
*/
66+
export type SomeComplexType = 0 | 1 | 'a' | 'b' | { key: string }

packages/playground/use-theme-doc/pages/components/button/index$.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ subGroup: general
88
demo1:
99
<Demo src="./demos/demo1.tsx" />
1010

11-
ButtonProps:
11+
ButtonProps (Interface):
1212
<TsInfo src="./types.ts" name="ButtonProps" />
1313

14-
ButtonProps by FileText:
15-
<FileText src="./types.ts" syntax="ts" />
14+
SomeObjectLiteralType (Object Literal):
15+
<TsInfo src="./types.ts" name="SomeObjectLiteralType" />
16+
17+
SomeComplexType (Complex Type):
18+
<TsInfo src="./types.ts" name="SomeComplexType" />
19+
20+
FileText:
21+
<FileText src="./types.ts" syntax="ts" />

packages/playground/use-theme-doc/pages/components/button/types.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This is the description of the Button component's props
33
*/
4-
export interface ButtonProps<TestGenerics extends string> {
4+
export interface ButtonProps<TestGenerics extends string> extends Base {
55
/**
66
* the type of button
77
* @defaultValue 'default'
@@ -21,8 +21,46 @@ export interface ButtonProps<TestGenerics extends string> {
2121
* click handler
2222
*/
2323
onClick?: (event: React.MouseEvent) => void
24+
/** test method declaration */
25+
testMethod(param: string): void
26+
/** test required property */
27+
testRequired: boolean
28+
}
29+
30+
interface Base {
2431
/**
25-
* button content
32+
* children content
2633
*/
2734
children: React.ReactNode
2835
}
36+
37+
export type SomeObjectLiteralType<TestGenerics> = {
38+
/**
39+
* the type of button
40+
* @defaultValue 'default'
41+
*/
42+
type?: 'primary' | 'default' | 'text'
43+
/**
44+
* the size of button
45+
* @defaultValue 'middle'
46+
*/
47+
size?: 'large' | 'middle' | 'small' | TestGenerics
48+
/**
49+
* loading state of button
50+
* @defaultValue false
51+
*/
52+
loading?: boolean
53+
/**
54+
* click handler
55+
*/
56+
onClick?: (event: React.MouseEvent) => void
57+
/** test method declaration */
58+
testMethod(param: string): void
59+
/** test required property */
60+
testRequired: boolean
61+
}
62+
63+
/**
64+
* Description of SomeComplexType ...
65+
*/
66+
export type SomeComplexType = 0 | 1 | 'a' | 'b' | { key: string }

packages/react-pages/clientTypes.d.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,34 @@ export interface PagesInternal {
1111
}
1212

1313
// the result of tsInfo extraction
14-
export interface TsInterfaceInfo {
14+
export type TsInfo =
15+
| {
16+
// example: type A = { k: v }
17+
type: 'object-literal'
18+
name: string
19+
description: string
20+
properties: TsPropertyOrMethodInfo[]
21+
}
22+
| {
23+
// example: interface MyInterface { k: v }
24+
type: 'interface'
25+
name: string
26+
description: string
27+
properties: TsPropertyOrMethodInfo[]
28+
}
29+
| {
30+
// complex type literal
31+
// example: type A = 'asd' | 123
32+
type: 'other'
33+
name: string
34+
description: string
35+
text: string
36+
}
37+
export interface TsPropertyOrMethodInfo {
1538
name: string
16-
// commentText: string
17-
description: string
18-
// fullText: string
19-
properties: TsInterfacePropertyInfo[]
20-
}
21-
22-
export interface TsInterfacePropertyInfo {
23-
name: string
24-
// commentText: string
2539
type: string
2640
description: string
2741
defaultValue: string | undefined
28-
// fullText: string
2942
optional: boolean
3043
}
3144

packages/react-pages/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"remark-mdx-images": "^2.0.0",
8989
"slash": "^3.0.0",
9090
"tiny-invariant": "^1.3.1",
91+
"ts-morph": "^17.0.1",
9192
"typescript": "^4.9.4",
9293
"unist-util-visit": "^4.1.1"
9394
}

packages/react-pages/src/node/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ export type {
234234
LoadState,
235235
PagesLoaded,
236236
PagesStaticData,
237-
TsInterfaceInfo,
238-
TsInterfacePropertyInfo,
237+
TsInfo,
238+
TsPropertyOrMethodInfo,
239239
} from '../../clientTypes'
240240

241241
export type { FileHandler } from './page-strategy/types.doc'

packages/react-pages/src/node/virtual-module-plugins/demo-modules/mdx-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function DemoMdxPlugin() {
2828
const nextIndex = addImports.length
2929
const varName = `_demo${nextIndex}`
3030
// add import:
31-
// import * as varName from "${srcValue}?tsInfo=${nameValue}"
31+
// import * as varName from "${srcValue}?demo"
3232
addImports.push(
3333
createNameSpaceImportNode({
3434
name: varName,

0 commit comments

Comments
 (0)