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
18 changes: 13 additions & 5 deletions server/utils/docs/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getNodeSignature(node: DenoDocNode): string | null {
case 'function': {
const typeParams = node.functionDef?.typeParams?.map(t => t.name).join(', ')
const typeParamsStr = typeParams ? `<${typeParams}>` : ''
const params = node.functionDef?.params?.map(p => formatParam(p)).join(', ') || ''
const params = formatParams(node.functionDef?.params)
const ret = formatType(node.functionDef?.returnType) || 'void'
const asyncStr = node.functionDef?.isAsync ? 'async ' : ''
return `${asyncStr}function ${name}${typeParamsStr}(${params}): ${ret}`
Expand Down Expand Up @@ -60,10 +60,18 @@ export function getNodeSignature(node: DenoDocNode): string | null {
/**
* Format a function parameter.
*/
export function formatParam(param: FunctionParam): string {
export function formatParam(param: FunctionParam, index = 0): string {
const name = param.name || `arg_${index}`
const optional = param.optional ? '?' : ''
const type = formatType(param.tsType)
return type ? `${param.name}${optional}: ${type}` : `${param.name}${optional}`
return type ? `${name}${optional}: ${type}` : `${name}${optional}`
}

/**
* Format a function parameter list.
*/
export function formatParams(params?: FunctionParam[]): string {
return params?.map((param, index) => formatParam(param, index)).join(', ') || ''
}

/**
Expand Down Expand Up @@ -116,7 +124,7 @@ const TYPE_FORMATTERS: Partial<Record<TsType['kind'], (type: TsType) => string>>
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 params = formatParams(fn.params)
const ret = formatType(fn.tsType) || 'void'
return `${typeParamsStr}(${params}) => ${ret}`
}
Expand All @@ -129,7 +137,7 @@ function formatTypeLiteralType(lit: NonNullable<TsType['typeLiteral']>): string
parts.push(`${ro}${prop.name}${opt}: ${formatType(prop.tsType) || 'unknown'}`)
}
for (const method of lit.methods) {
const params = method.params?.map(p => formatParam(p)).join(', ') || ''
const params = formatParams(method.params)
const ret = formatType(method.returnType) || 'void'
parts.push(`${method.name}(${params}): ${ret}`)
}
Expand Down
8 changes: 4 additions & 4 deletions server/utils/docs/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { DenoDocNode, JsDocTag } from '#shared/types/deno-doc'
import { highlightCodeBlock } from '../shiki'
import { formatParam, formatType, getNodeSignature } from './format'
import { formatParams, formatType, getNodeSignature } from './format'
import { groupMergedByKind } from './processing'
import { escapeHtml, createSymbolId, parseJsDocLinks, renderMarkdown } from './text'
import type { MergedSymbol, SymbolLookup } from './types'
Expand Down Expand Up @@ -306,7 +306,7 @@ function renderClassMembers(def: NonNullable<DenoDocNode['classDef']>): string {
lines.push(`<div class="docs-members">`)
lines.push(`<h4>Constructor</h4>`)
for (const ctor of constructors) {
const params = ctor.params?.map(p => formatParam(p)).join(', ') || ''
const params = formatParams(ctor.params)
lines.push(`<pre><code>constructor(${escapeHtml(params)})</code></pre>`)
}
lines.push(`</div>`)
Expand Down Expand Up @@ -350,7 +350,7 @@ function renderClassMembers(def: NonNullable<DenoDocNode['classDef']>): string {

if (regularMethods.length > 0) {
const methodItems: DefinitionListItem[] = regularMethods.map(method => {
const params = method.functionDef?.params?.map(p => formatParam(p)).join(', ') || ''
const params = formatParams(method.functionDef?.params)
const ret = formatType(method.functionDef?.returnType) || 'void'
const staticStr = method.isStatic ? 'static ' : ''

Expand Down Expand Up @@ -397,7 +397,7 @@ function renderInterfaceMembers(def: NonNullable<DenoDocNode['interfaceDef']>):
lines.push(`<h4>Methods</h4>`)
lines.push(`<dl>`)
for (const method of methods) {
const params = method.params?.map(p => formatParam(p)).join(', ') || ''
const params = formatParams(method.params)
const ret = formatType(method.returnType) || 'void'
lines.push(
`<dt><code>${escapeHtml(method.name)}(${escapeHtml(params)}): ${escapeHtml(ret)}</code></dt>`,
Expand Down
2 changes: 1 addition & 1 deletion shared/types/deno-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface TsType {
/** Function parameter from deno doc */
export interface FunctionParam {
kind: string
name: string
name?: string
optional?: boolean
tsType?: TsType
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "createHighlighter",
"isDefault": false,
"kind": "function",
"functionDef": {
"params": [
{
"kind": "object",
"props": [
{
"kind": "assign",
"key": "fallbackLanguage"
},
{
"kind": "assign",
"key": "languages"
}
],
"optional": false,
"tsType": {
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"properties": [
{
"name": "fallbackLanguage",
"optional": true,
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string"
}
},
{
"name": "languages",
"optional": false,
"tsType": {
"repr": "ReadonlyArray",
"kind": "typeRef",
"typeRef": {
"typeParams": [
{
"repr": "LanguageDefinition",
"kind": "typeRef",
"typeRef": {
"typeName": "LanguageDefinition"
}
}
],
"typeName": "ReadonlyArray"
}
}
}
],
"methods": [],
"callSignatures": [],
"indexSignatures": []
}
}
}
],
"returnType": {
"repr": "Highlighter",
"kind": "typeRef",
"typeRef": {
"typeName": "Highlighter"
}
},
"isAsync": false,
"isGenerator": false,
"typeParams": []
}
}
28 changes: 27 additions & 1 deletion test/unit/server/utils/docs/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import { formatType, getNodeSignature } from '#server/utils/docs/format'
import { formatParams, formatType, getNodeSignature } from '#server/utils/docs/format'
import type { DenoDocNode } from '#shared/types/deno-doc'

function loadFixture(name: string): DenoDocNode {
Expand Down Expand Up @@ -84,3 +84,29 @@ describe('issue #1411 - linkdave@0.0.2 unknown types', () => {
expect(pickSig).not.toContain('= unknown')
})
})

describe('anonymous function parameters', () => {
it('uses a positional fallback for a destructured parameter', () => {
const node = loadFixture('tanstack-highlight@0.0.9-create-highlighter.json')

expect(getNodeSignature(node)).toBe(
'function createHighlighter(arg_0: { fallbackLanguage?: string; languages: ReadonlyArray<LanguageDefinition> }): Highlighter',
)
})

it('uses the zero-based parameter index in fallback names', () => {
expect(
formatParams([
{
kind: 'identifier',
name: 'value',
tsType: { repr: 'string', kind: 'keyword', keyword: 'string' },
},
{
kind: 'object',
tsType: { repr: 'object', kind: 'keyword', keyword: 'object' },
},
]),
).toBe('value: string, arg_1: object')
})
})
Loading