Skip to content

Commit

Permalink
support function type documentation (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious authored Jun 17, 2024
1 parent e18b86b commit d60c091
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-boxes-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsxmod/utils": minor
---

Adds support for function type documentation in `getTypeDocumentation`.
102 changes: 102 additions & 0 deletions packages/utils/src/types/getTypeDocumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,4 +1054,106 @@ describe('getTypeDocumentation', () => {
}
`)
})

test('function types', () => {
const sourceFile = project.createSourceFile(
'test.ts',
dedent`
import * as React from 'react';
export function getExportedTypes() {
return [
{
/** The name of the component. */
name: 'Button',
/** The description of the component. */
description: 'A button component'
}
]
}
type BaseExportedTypesProps = {
/** Controls how types are rendered. */
children?: (
exportedTypes: ReturnType<typeof getExportedTypes>
) => React.ReactNode
}
type ExportedTypesProps =
| ({ source: string } & BaseExportedTypesProps)
| ({ filename: string; value: string } & BaseExportedTypesProps)
function ExportedTypes({ children }: ExportedTypesProps) {}
`,
{ overwrite: true }
)
const types = getTypeDocumentation(
sourceFile.getFunctionOrThrow('ExportedTypes')
)

expect(types).toMatchInlineSnapshot(`
{
"name": "ExportedTypes",
"parameters": [
{
"defaultValue": undefined,
"description": undefined,
"name": undefined,
"properties": [
{
"defaultValue": undefined,
"description": "Controls how types are rendered.",
"name": "children",
"parameters": [
{
"defaultValue": undefined,
"description": undefined,
"name": "exportedTypes",
"required": true,
"type": "{ name: string; description: string; }[]",
},
],
"required": false,
"returnType": "React.ReactNode",
"tags": undefined,
"type": "(exportedTypes: ReturnType<typeof getExportedTypes>) => React.ReactNode",
},
],
"required": true,
"type": "ExportedTypesProps",
"unionProperties": [
[
{
"defaultValue": undefined,
"description": undefined,
"name": "source",
"required": true,
"type": "string",
},
],
[
{
"defaultValue": undefined,
"description": undefined,
"name": "filename",
"required": true,
"type": "string",
},
{
"defaultValue": undefined,
"description": undefined,
"name": "value",
"required": true,
"type": "string",
},
],
],
},
],
"returnType": "void",
"type": "({ children }: ExportedTypesProps) => void",
}
`)
})
})
Loading

0 comments on commit d60c091

Please sign in to comment.