Skip to content

Commit 3ac0f0f

Browse files
authored
ts info extract support render callSignatures and constructSignatures… (#143)
1 parent f239001 commit 3ac0f0f

File tree

10 files changed

+679
-1731
lines changed

10 files changed

+679
-1731
lines changed

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
shamefully-hoist=true
22
strict-peer-dependencies=false
3+
auto-install-peers=false

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

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ export interface ButtonProps<TestGenerics extends string> extends Base {
4141
myExportedInterface: MyExportedInterface
4242
myInterface: MyInterface
4343
myImportedTypeAlias: MyImportedTypeAlias
44+
/** test call signatures */
45+
(options?: { ignorePending?: true }): Array<string | Promise<string>>
46+
(options: { ignorePending: false }): string[]
47+
/** test construct signatures */
48+
new (options: string): MyInterface
49+
new (): MyInterface
4450
}
4551

4652
interface Base {
@@ -79,6 +85,12 @@ export type SomeObjectLiteralType<TestGenerics> = {
7985
myExportedInterface: MyExportedInterface
8086
myInterface: MyInterface
8187
myImportedTypeAlias: MyImportedTypeAlias
88+
/** test call signatures */
89+
(options?: { ignorePending?: true }): Array<string | Promise<string>>
90+
(options: { ignorePending: false }): string[]
91+
/** test construct signatures */
92+
new (options: string): MyInterface
93+
new (): MyInterface
8294
}
8395

8496
/**

Diff for: packages/react-pages/clientTypes.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ export type TsInfo =
1818
name: string
1919
description: string
2020
properties: TsPropertyOrMethodInfo[]
21+
callSignatures: CallSignatureInfo[]
22+
constructSignatures: CallSignatureInfo[]
2123
}
2224
| {
2325
// example: interface MyInterface { k: v }
2426
type: 'interface'
2527
name: string
2628
description: string
2729
properties: TsPropertyOrMethodInfo[]
30+
callSignatures: CallSignatureInfo[]
31+
constructSignatures: CallSignatureInfo[]
2832
}
2933
| {
3034
// complex type literal
@@ -41,6 +45,10 @@ export interface TsPropertyOrMethodInfo {
4145
defaultValue: string | undefined
4246
optional: boolean
4347
}
48+
export interface CallSignatureInfo {
49+
type: string
50+
description: string
51+
}
4452

4553
export type UseAllPagesOutlines = (timeout: number) => any
4654

Diff for: packages/react-pages/src/node/virtual-module-plugins/ts-info-module/extract.ts

+54-40
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,11 @@ import {
66
ts,
77
} from 'ts-morph'
88

9-
export type TsInfo =
10-
| {
11-
// example: type A = { k: v }
12-
type: 'object-literal'
13-
name: string
14-
description: string
15-
properties: TsPropertyOrMethodInfo[]
16-
}
17-
| {
18-
// example: interface MyInterface { k: v }
19-
type: 'interface'
20-
name: string
21-
description: string
22-
properties: TsPropertyOrMethodInfo[]
23-
}
24-
| {
25-
// complex type literal
26-
// example: type A = 'asd' | 123
27-
type: 'other'
28-
name: string
29-
description: string
30-
text: string
31-
}
32-
33-
export interface TsPropertyOrMethodInfo {
34-
name: string
35-
type: string
36-
description: string
37-
defaultValue: string | undefined
38-
optional: boolean
39-
}
9+
import type {
10+
TsInfo,
11+
TsPropertyOrMethodInfo,
12+
CallSignatureInfo,
13+
} from '../../../../clientTypes'
4014

4115
const defaultTsConfig: ts.CompilerOptions = {
4216
target: ts.ScriptTarget.ESNext,
@@ -92,12 +66,15 @@ export function collectInterfaceInfo(
9266

9367
if (Node.isTypeLiteral(typeNode)) {
9468
// example: type A = { k: v }
95-
const members = handleTypeElementMembered(typeNode, typeChecker)
69+
const { members, callSignatures, constructSignatures } =
70+
handleTypeElementMembered(typeNode, typeChecker)
9671
return {
9772
type: 'object-literal',
9873
name,
9974
description,
10075
properties: members,
76+
callSignatures,
77+
constructSignatures,
10178
}
10279
} else {
10380
// example: type A = 'asd' | 123
@@ -122,8 +99,16 @@ export function collectInterfaceInfo(
12299
return jsDoc.getDescription().trim()
123100
})
124101
.join('\n\n')
125-
const members = handleTypeElementMembered(node, typeChecker)
126-
return { type: 'interface', name, description, properties: members }
102+
const { members, callSignatures, constructSignatures } =
103+
handleTypeElementMembered(node, typeChecker)
104+
return {
105+
type: 'interface',
106+
name,
107+
description,
108+
properties: members,
109+
callSignatures,
110+
constructSignatures,
111+
}
127112
}
128113

129114
throw new Error('unexpected node type: ' + node.getKindName())
@@ -136,12 +121,16 @@ export function collectInterfaceInfo(
136121
function handleTypeElementMembered(
137122
node: TypeElementMemberedNode & Node,
138123
typeChecker: TypeChecker
139-
): TsPropertyOrMethodInfo[] {
140-
const result: TsPropertyOrMethodInfo[] = []
124+
): {
125+
members: TsPropertyOrMethodInfo[]
126+
callSignatures: CallSignatureInfo[]
127+
constructSignatures: CallSignatureInfo[]
128+
} {
129+
const members: TsPropertyOrMethodInfo[] = []
141130
// or use node.getSymbol()?.getMembers() ?
142131
const nodeType = node.getType()
143-
const properties = nodeType.getProperties()
144-
for (const prop of properties) {
132+
// https://stackoverflow.com/a/68623960
133+
for (const prop of nodeType.getProperties()) {
145134
const name = prop.getName()
146135
const description = ts.displayPartsToString(
147136
prop.compilerSymbol.getDocumentationComment(typeChecker.compilerObject)
@@ -165,15 +154,40 @@ function handleTypeElementMembered(
165154
return res
166155
})()
167156
const optional = prop.isOptional()
168-
result.push({
157+
members.push({
169158
name,
170159
description,
171160
type,
172161
defaultValue,
173162
optional,
174163
})
175164
}
176-
return result
165+
166+
const callSignatures: CallSignatureInfo[] = []
167+
for (const sig of nodeType.getCallSignatures()) {
168+
const description = ts.displayPartsToString(
169+
sig.compilerSignature.getDocumentationComment(typeChecker.compilerObject)
170+
)
171+
const type = sig.getDeclaration().getText()
172+
callSignatures.push({
173+
description,
174+
type,
175+
})
176+
}
177+
178+
const constructSignatures: CallSignatureInfo[] = []
179+
for (const sig of nodeType.getConstructSignatures()) {
180+
const description = ts.displayPartsToString(
181+
sig.compilerSignature.getDocumentationComment(typeChecker.compilerObject)
182+
)
183+
const type = sig.getDeclaration().getText()
184+
constructSignatures.push({
185+
description,
186+
type,
187+
})
188+
}
189+
190+
return { members, callSignatures, constructSignatures }
177191
}
178192

179193
// an alternative way to implement handleTypeElementMembered

Diff for: packages/theme-doc/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@rollup/plugin-node-resolve": "^15.0.2",
5050
"@rollup/plugin-typescript": "^11.1.0",
5151
"@types/mdx": "^2.0.4",
52-
"antd": "^5.4.0",
52+
"antd": "^5.6.1",
5353
"chokidar": "^3.5.1",
5454
"concurrently": "^7.6.0",
5555
"copy-to-clipboard": "^3.3.3",

Diff for: packages/theme-doc/src/Layout/MDX/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const components: MDXComponents = {
4040
}),
4141
CodeBlock,
4242
Demo,
43-
TsInfo: withMdClassName(TsInfo),
43+
TsInfo,
4444
FileText: withMdClassName(FileText),
4545
a: (props: React.HTMLProps<HTMLAnchorElement>) => {
4646
const { href, ...rest } = props
@@ -123,7 +123,7 @@ export default MDX
123123
* Ref: "FlowContent" are the top-level block elements in mdast
124124
* https://github.com/syntax-tree/mdast#flowcontent
125125
*/
126-
function withMdClassName(Component: React.FC<any> | string) {
126+
export function withMdClassName(Component: React.FC<any> | string) {
127127
return function (props: any) {
128128
const { className } = props
129129
const newClassName = className ? `${className} markdown-el` : 'markdown-el'

Diff for: packages/theme-doc/src/Layout/TsInfo/index.module.css

-8
This file was deleted.
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.ctn {
2+
margin-bottom: 16px;
3+
}

0 commit comments

Comments
 (0)