Skip to content

Commit

Permalink
add component kind
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Jun 18, 2024
1 parent 3619c3d commit f5f6721
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-needles-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsxmod/utils": patch
---

Adds `Component` kind for `getTypeDocumentation`.
60 changes: 41 additions & 19 deletions packages/utils/src/types/getTypeDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ export interface FunctionMetadata {
tags?: { tagName: string; text?: string }[]
}

export interface ComponentMetadata {
kind: 'Component'
name?: string
properties: PropertyMetadata[]
type: string
returnType: string
description?: string
tags?: { tagName: string; text?: string }[]
}

interface BasePropertyMetadata {
name?: string
description?: string
Expand Down Expand Up @@ -142,6 +152,7 @@ type Metadata =
| TypeAliasMetadata
| ClassMetadata
| FunctionMetadata
| ComponentMetadata

export type DocumentationMetadata<Type> = Type extends InterfaceDeclaration
? InterfaceMetadata
Expand All @@ -150,9 +161,9 @@ export type DocumentationMetadata<Type> = Type extends InterfaceDeclaration
: Type extends ClassDeclaration
? ClassMetadata
: Type extends FunctionDeclaration
? FunctionMetadata
? FunctionMetadata | ComponentMetadata
: Type extends VariableDeclaration
? FunctionMetadata
? FunctionMetadata | ComponentMetadata
: never

export function getTypeDocumentation<Type extends Declaration>(
Expand Down Expand Up @@ -245,7 +256,7 @@ function processTypeAlias(
}
}

/** Processes a function declaration into a metadata object. */
/** Processes a function or expression into a metadata object. */
function processFunctionOrExpression(
functionDeclarationOrExpression:
| FunctionDeclaration
Expand All @@ -255,37 +266,24 @@ function processFunctionOrExpression(
| CallExpression,
propertyFilter?: PropertyFilter,
variableDeclaration?: VariableDeclaration
): FunctionMetadata {
): FunctionMetadata | ComponentMetadata {
const signatures = functionDeclarationOrExpression
.getType()
.getCallSignatures()

// TODO: add support for multiple signatures (overloads)
if (signatures.length === 0) {
throw new Error(
`No signatures found for function declaration or expression: ${functionDeclarationOrExpression.getText()}`
)
}

// TODO: add support for multiple signatures (overloads)
const signature = signatures.at(0)!
const parameters = signature.getParameters()
let parameterTypes: ReturnType<typeof processParameterType>[] = []

for (const parameter of parameters) {
const parameterType = processParameterType(
parameter.getValueDeclaration() as ParameterDeclaration,
functionDeclarationOrExpression,
propertyFilter
)
parameterTypes.push(parameterType)
}

return {
kind: 'Function',
const sharedMetadata = {
name: variableDeclaration
? variableDeclaration.getName()
: (functionDeclarationOrExpression as FunctionDeclaration).getName(),
parameters: parameterTypes,
type: functionDeclarationOrExpression
.getType()
.getText(
Expand All @@ -299,6 +297,30 @@ function processFunctionOrExpression(
TypeFormatFlags.UseAliasDefinedOutsideCurrentScope
),
...getJsDocMetadata(variableDeclaration || functionDeclarationOrExpression),
} as const
const parameterTypes = parameters.map((parameter) =>
processParameterType(
parameter.getValueDeclaration() as ParameterDeclaration,
functionDeclarationOrExpression,
propertyFilter
)
)
const isComponent = sharedMetadata.name
? /[A-Z]/.test(sharedMetadata.name.charAt(0))
: false

if (isComponent) {
return {
kind: 'Component',
properties: parameterTypes.at(0)!.properties!,
...sharedMetadata,
}
}

return {
kind: 'Function',
parameters: parameterTypes,
...sharedMetadata,
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
type ClassAccessorMetadata,
type ClassMethodMetadata,
type FunctionMetadata,
type ComponentMetadata,
type PropertyMetadata,
type ParameterMetadata,
type PropertyFilter,
Expand Down

0 comments on commit f5f6721

Please sign in to comment.