Skip to content

Commit 6e9b8ce

Browse files
committed
fix: unexpected unknown type
1 parent e61657e commit 6e9b8ce

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

server/utils/docs/format.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function getNodeSignature(node: DenoDocNode): string | null {
2525
return `${asyncStr}function ${name}${typeParamsStr}(${params}): ${ret}`
2626
}
2727
case 'class': {
28-
const ext = node.classDef?.extends ? ` extends ${formatType(node.classDef.extends)}` : ''
28+
const ext = node.classDef?.extends ? ` extends ${node.classDef.extends}` : ''
2929
const impl = node.classDef?.implements?.map(t => formatType(t)).join(', ')
3030
const implStr = impl ? ` implements ${impl}` : ''
3131
const abstractStr = node.classDef?.isAbstract ? 'abstract ' : ''
@@ -72,9 +72,6 @@ export function formatParam(param: FunctionParam): string {
7272
export function formatType(type?: TsType): string {
7373
if (!type) return ''
7474

75-
// Strip ANSI codes from repr (deno doc may include terminal colors since it's built for that)
76-
if (type.repr) return stripAnsi(type.repr)
77-
7875
if (type.kind === 'keyword' && type.keyword) {
7976
return type.keyword
8077
}
@@ -92,5 +89,41 @@ export function formatType(type?: TsType): string {
9289
return type.union.map(t => formatType(t)).join(' | ')
9390
}
9491

92+
if (type.kind === 'this') {
93+
return 'this'
94+
}
95+
96+
if (type.kind === 'indexedAccess' && type.indexedAccess) {
97+
return `${formatType(type.indexedAccess.objType)}[${formatType(type.indexedAccess.indexType)}]`
98+
}
99+
100+
if (type.kind === 'typeOperator' && type.typeOperator) {
101+
return `${type.typeOperator.operator} ${formatType(type.typeOperator.tsType)}`
102+
}
103+
104+
if (type.kind === 'fnOrConstructor' && type.fnOrConstructor) {
105+
const { fnOrConstructor: fn } = type
106+
const typeParams = fn.typeParams?.map(t => t.name).join(', ')
107+
const typeParamsStr = typeParams ? `<${typeParams}>` : ''
108+
const params = fn.params.map(p => formatParam(p)).join(', ')
109+
const ret = formatType(fn.tsType) || 'void'
110+
return `${typeParamsStr}(${params}) => ${ret}`
111+
}
112+
113+
if (type.kind === 'typeLiteral' && type.typeLiteral) {
114+
const parts: string[] = []
115+
for (const prop of type.typeLiteral.properties) {
116+
const opt = prop.optional ? '?' : ''
117+
const ro = prop.readonly ? 'readonly ' : ''
118+
parts.push(`${ro}${prop.name}${opt}: ${formatType(prop.tsType) || 'unknown'}`)
119+
}
120+
for (const method of type.typeLiteral.methods) {
121+
const params = method.params?.map(p => formatParam(p)).join(', ') || ''
122+
const ret = formatType(method.returnType) || 'void'
123+
parts.push(`${method.name}(${params}): ${ret}`)
124+
}
125+
return `{ ${parts.join('; ')} }`
126+
}
127+
95128
return type.repr ? stripAnsi(type.repr) : 'unknown'
96129
}

shared/types/deno-doc.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ export interface TsType {
3535
number?: number
3636
boolean?: boolean
3737
}
38+
fnOrConstructor?: {
39+
constructor: boolean
40+
tsType: TsType
41+
params: FunctionParam[]
42+
typeParams?: Array<{ name: string; constraint?: TsType }>
43+
}
44+
indexedAccess?: {
45+
objType: TsType
46+
indexType: TsType
47+
}
48+
typeOperator?: {
49+
operator: string
50+
tsType: TsType
51+
}
52+
this?: boolean
53+
typeLiteral?: {
54+
properties: Array<{ name: string; tsType?: TsType; readonly?: boolean; optional?: boolean }>
55+
methods: Array<{ name: string; params?: FunctionParam[]; returnType?: TsType }>
56+
callSignatures: Array<{ params?: FunctionParam[]; tsType?: TsType }>
57+
indexSignatures: Array<{ params: FunctionParam[]; tsType?: TsType }>
58+
}
3859
}
3960

4061
/** Function parameter from deno doc */
@@ -89,7 +110,7 @@ export interface DenoDocNode {
89110
constructors?: Array<{
90111
params?: FunctionParam[]
91112
}>
92-
extends?: TsType
113+
extends?: string
93114
implements?: TsType[]
94115
}
95116
interfaceDef?: {

0 commit comments

Comments
 (0)