Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 56 additions & 15 deletions server/utils/docs/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getNodeSignature(node: DenoDocNode): string | null {
return `${asyncStr}function ${name}${typeParamsStr}(${params}): ${ret}`
}
case 'class': {
const ext = node.classDef?.extends ? ` extends ${formatType(node.classDef.extends)}` : ''
const ext = node.classDef?.extends ? ` extends ${node.classDef.extends}` : ''
const impl = node.classDef?.implements?.map(t => formatType(t)).join(', ')
const implStr = impl ? ` implements ${impl}` : ''
const abstractStr = node.classDef?.isAbstract ? 'abstract ' : ''
Expand Down Expand Up @@ -72,25 +72,66 @@ export function formatParam(param: FunctionParam): string {
export function formatType(type?: TsType): string {
if (!type) return ''

// Strip ANSI codes from repr (deno doc may include terminal colors since it's built for that)
if (type.repr) return stripAnsi(type.repr)
const formatter = TYPE_FORMATTERS[type.kind]
const formatted = formatter?.(type)

if (type.kind === 'keyword' && type.keyword) {
return type.keyword
}
if (formatted) return formatted
return type.repr ? stripAnsi(type.repr) : 'unknown'
}

if (type.kind === 'typeRef' && type.typeRef) {
const TYPE_FORMATTERS: Partial<Record<TsType['kind'], (type: TsType) => string>> = {
keyword: type => type.keyword || '',
literal: type => {
if (!type.literal) return ''
if (type.literal.kind === 'string') return `"${type.literal.string}"`
if (type.literal.kind === 'number') return String(type.literal.number)
if (type.literal.kind === 'boolean') return String(type.literal.boolean)
return ''
},
typeRef: type => {
if (!type.typeRef) return ''
const params = type.typeRef.typeParams?.map(t => formatType(t)).join(', ')
return params ? `${type.typeRef.typeName}<${params}>` : type.typeRef.typeName
}
},
array: type => {
if (!type.array) return ''
const element = formatType(type.array)
return type.array.kind === 'union' ? `(${element})[]` : `${element}[]`
},
union: type => (type.union ? type.union.map(t => formatType(t)).join(' | ') : ''),
this: () => 'this',
indexedAccess: type => {
if (!type.indexedAccess) return ''
return `${formatType(type.indexedAccess.objType)}[${formatType(type.indexedAccess.indexType)}]`
},
typeOperator: type => {
if (!type.typeOperator) return ''
return `${type.typeOperator.operator} ${formatType(type.typeOperator.tsType)}`
},
fnOrConstructor: type =>
type.fnOrConstructor ? formatFnOrConstructorType(type.fnOrConstructor) : '',
typeLiteral: type => (type.typeLiteral ? formatTypeLiteralType(type.typeLiteral) : ''),
}

if (type.kind === 'array' && type.array) {
return `${formatType(type.array)}[]`
}
function formatFnOrConstructorType(fn: NonNullable<TsType['fnOrConstructor']>): string {
const typeParams = fn.typeParams?.map(t => t.name).join(', ')
const typeParamsStr = typeParams ? `<${typeParams}>` : ''
const params = fn.params.map(p => formatParam(p)).join(', ')
const ret = formatType(fn.tsType) || 'void'
return `${typeParamsStr}(${params}) => ${ret}`
}
Comment on lines +116 to +122
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n server/utils/docs/format.ts | head -150 | tail -50

Repository: npmx-dev/npmx.dev

Length of output: 2493


🏁 Script executed:

rg -n "fnOrConstructor|TsType" server/utils/docs/format.ts | head -20

Repository: npmx-dev/npmx.dev

Length of output: 581


🏁 Script executed:

rg -n "interface.*TsType|type.*TsType" server/utils/docs/ -A 30

Repository: npmx-dev/npmx.dev

Length of output: 5460


🏁 Script executed:

fd -t f "types" --type f -path "*shared/types*" | head -20

Repository: npmx-dev/npmx.dev

Length of output: 229


🏁 Script executed:

find . -type f -name "*deno-doc*" -o -name "*types*" | grep -i "shared\|types" | head -20

Repository: npmx-dev/npmx.dev

Length of output: 340


🏁 Script executed:

cat -n shared/types/deno-doc.ts | grep -A 100 "fnOrConstructor\|interface.*TsType"

Repository: npmx-dev/npmx.dev

Length of output: 3442


🏁 Script executed:

cat -n shared/types/deno-doc.ts | sed -n '38,43p'

Repository: npmx-dev/npmx.dev

Length of output: 262


🏁 Script executed:

cat -n shared/types/deno-doc.ts | sed -n '38,43p'

Repository: npmx-dev/npmx.dev

Length of output: 262


🏁 Script executed:

cat -n server/utils/docs/format.ts | sed -n '116,122p'

Repository: npmx-dev/npmx.dev

Length of output: 482


fnOrConstructor formatting drops generic constraints and constructor semantics.

Line 117 ignores the constraint field of type parameters (e.g. K extends keyof ...), and line 121 does not check the constructor boolean flag, so constructor signatures are rendered as plain function arrows instead of constructor syntax.

Proposed fix
 function formatFnOrConstructorType(fn: NonNullable<TsType['fnOrConstructor']>): string {
-  const typeParams = fn.typeParams?.map(t => t.name).join(', ')
+  const typeParams = fn.typeParams
+    ?.map(t => (t.constraint ? `${t.name} extends ${formatType(t.constraint)}` : t.name))
+    .join(', ')
   const typeParamsStr = typeParams ? `<${typeParams}>` : ''
   const params = fn.params.map(p => formatParam(p)).join(', ')
   const ret = formatType(fn.tsType) || 'void'
-  return `${typeParamsStr}(${params}) => ${ret}`
+  const ctorPrefix = fn.constructor ? 'new ' : ''
+  return `${ctorPrefix}${typeParamsStr}(${params}) => ${ret}`
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function formatFnOrConstructorType(fn: NonNullable<TsType['fnOrConstructor']>): string {
const typeParams = fn.typeParams?.map(t => t.name).join(', ')
const typeParamsStr = typeParams ? `<${typeParams}>` : ''
const params = fn.params.map(p => formatParam(p)).join(', ')
const ret = formatType(fn.tsType) || 'void'
return `${typeParamsStr}(${params}) => ${ret}`
}
function formatFnOrConstructorType(fn: NonNullable<TsType['fnOrConstructor']>): string {
const typeParams = fn.typeParams
?.map(t => (t.constraint ? `${t.name} extends ${formatType(t.constraint)}` : t.name))
.join(', ')
const typeParamsStr = typeParams ? `<${typeParams}>` : ''
const params = fn.params.map(p => formatParam(p)).join(', ')
const ret = formatType(fn.tsType) || 'void'
const ctorPrefix = fn.constructor ? 'new ' : ''
return `${ctorPrefix}${typeParamsStr}(${params}) => ${ret}`
}


if (type.kind === 'union' && type.union) {
return type.union.map(t => formatType(t)).join(' | ')
function formatTypeLiteralType(lit: NonNullable<TsType['typeLiteral']>): string {
const parts: string[] = []
for (const prop of lit.properties) {
const opt = prop.optional ? '?' : ''
const ro = prop.readonly ? 'readonly ' : ''
parts.push(`${ro}${prop.name}${opt}: ${formatType(prop.tsType) || 'unknown'}`)
}

return type.repr ? stripAnsi(type.repr) : 'unknown'
for (const method of lit.methods) {
const params = method.params?.map(p => formatParam(p)).join(', ') || ''
const ret = formatType(method.returnType) || 'void'
parts.push(`${method.name}(${params}): ${ret}`)
}
return `{ ${parts.join('; ')} }`
}
23 changes: 22 additions & 1 deletion shared/types/deno-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ export interface TsType {
number?: number
boolean?: boolean
}
fnOrConstructor?: {
constructor: boolean
tsType: TsType
params: FunctionParam[]
typeParams?: Array<{ name: string; constraint?: TsType }>
}
indexedAccess?: {
objType: TsType
indexType: TsType
}
typeOperator?: {
operator: string
tsType: TsType
}
this?: boolean
typeLiteral?: {
properties: Array<{ name: string; tsType?: TsType; readonly?: boolean; optional?: boolean }>
methods: Array<{ name: string; params?: FunctionParam[]; returnType?: TsType }>
callSignatures: Array<{ params?: FunctionParam[]; tsType?: TsType }>
indexSignatures: Array<{ params: FunctionParam[]; tsType?: TsType }>
}
}

/** Function parameter from deno doc */
Expand Down Expand Up @@ -89,7 +110,7 @@ export interface DenoDocNode {
constructors?: Array<{
params?: FunctionParam[]
}>
extends?: TsType
extends?: string
implements?: TsType[]
}
interfaceDef?: {
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/esm-sh/doc-nodes/linkdave@0.0.2-client-message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "ClientMessage",
"kind": "typeAlias",
"typeAliasDef": {
"tsType": {
"repr": "",
"kind": "union",
"union": [
{
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"properties": [
{
"name": "op",
"optional": false,
"tsType": {
"repr": "voiceUpdate",
"kind": "literal",
"literal": { "kind": "string", "string": "voiceUpdate" }
}
},
{
"name": "guildId",
"optional": false,
"tsType": { "repr": "string", "kind": "keyword", "keyword": "string" }
}
],
"methods": [],
"callSignatures": [],
"indexSignatures": []
}
},
{
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"properties": [
{
"name": "op",
"optional": false,
"tsType": {
"repr": "play",
"kind": "literal",
"literal": { "kind": "string", "string": "play" }
}
},
{
"name": "guildId",
"optional": false,
"tsType": { "repr": "string", "kind": "keyword", "keyword": "string" }
}
],
"methods": [],
"callSignatures": [],
"indexSignatures": []
}
}
]
},
"typeParams": []
}
}
158 changes: 158 additions & 0 deletions test/fixtures/esm-sh/doc-nodes/linkdave@0.0.2-client.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"name": "LinkDaveClient",
"kind": "interface",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [
{
"name": "on",
"computed": false,
"optional": false,
"tsType": {
"repr": "",
"kind": "fnOrConstructor",
"fnOrConstructor": {
"constructor": false,
"tsType": { "repr": "this", "kind": "this", "this": true },
"params": [
{
"kind": "identifier",
"name": "event",
"optional": false,
"tsType": {
"repr": "K",
"kind": "typeRef",
"typeRef": { "typeName": "K" }
}
},
{
"kind": "identifier",
"name": "listener",
"optional": false,
"tsType": {
"repr": "",
"kind": "fnOrConstructor",
"fnOrConstructor": {
"constructor": false,
"tsType": { "repr": "void", "kind": "keyword", "keyword": "void" },
"params": [
{
"kind": "identifier",
"name": "data",
"optional": false,
"tsType": {
"repr": "",
"kind": "indexedAccess",
"indexedAccess": {
"readonly": false,
"objType": {
"repr": "ManagerEvents",
"kind": "typeRef",
"typeRef": { "typeName": "ManagerEvents" }
},
"indexType": {
"repr": "K",
"kind": "typeRef",
"typeRef": { "typeName": "K" }
}
}
}
}
],
"typeParams": []
}
}
}
],
"typeParams": [
{
"name": "K",
"constraint": {
"repr": "",
"kind": "typeOperator",
"typeOperator": {
"operator": "keyof",
"tsType": {
"repr": "ManagerEvents",
"kind": "typeRef",
"typeRef": { "typeName": "ManagerEvents" }
}
}
}
}
]
}
}
},
{
"name": "emit",
"computed": false,
"optional": false,
"tsType": {
"repr": "",
"kind": "fnOrConstructor",
"fnOrConstructor": {
"constructor": false,
"tsType": { "repr": "boolean", "kind": "keyword", "keyword": "boolean" },
"params": [
{
"kind": "identifier",
"name": "event",
"optional": false,
"tsType": {
"repr": "K",
"kind": "typeRef",
"typeRef": { "typeName": "K" }
}
},
{
"kind": "identifier",
"name": "data",
"optional": false,
"tsType": {
"repr": "",
"kind": "indexedAccess",
"indexedAccess": {
"readonly": false,
"objType": {
"repr": "ManagerEvents",
"kind": "typeRef",
"typeRef": { "typeName": "ManagerEvents" }
},
"indexType": {
"repr": "K",
"kind": "typeRef",
"typeRef": { "typeName": "K" }
}
}
}
}
],
"typeParams": [
{
"name": "K",
"constraint": {
"repr": "",
"kind": "typeOperator",
"typeOperator": {
"operator": "keyof",
"tsType": {
"repr": "ManagerEvents",
"kind": "typeRef",
"typeRef": { "typeName": "ManagerEvents" }
}
}
}
}
]
}
}
}
],
"callSignatures": [],
"indexSignatures": [],
"typeParams": []
}
}
Loading
Loading