Skip to content

Commit

Permalink
print local node module union types
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Jun 20, 2024
1 parent 8c749fe commit 72f1da8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-buckets-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsxmod/utils": patch
---

Prints types for locally defined union members that come from `node_modules` in `getTypeDocumentation`.
62 changes: 62 additions & 0 deletions packages/utils/src/types/getTypeDocumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1909,4 +1909,66 @@ describe('getTypeDocumentation', () => {
}
`)
})

test('printing imported node module union types', () => {
project.createSourceFile(
'node_modules/library/index.d.ts',
dedent`
export type InterfaceMetadata = {
kind: 'Interface'
name: string
}
`
)

const sourceFile = project.createSourceFile(
'test.ts',
dedent`
import type { InterfaceMetadata } from 'library'
export type TypeAliasMetadata = {
kind: 'TypeAlias'
name: string
}
type AllMetadata = InterfaceMetadata | TypeAliasMetadata
`,
{ overwrite: true }
)
const types = getTypeDocumentation(
sourceFile.getTypeAliasOrThrow('AllMetadata')
)

expect(types).toMatchInlineSnapshot(`
{
"kind": "TypeAlias",
"name": "AllMetadata",
"properties": [],
"type": "InterfaceMetadata | TypeAliasMetadata",
"unionProperties": [
[
{
"type": "InterfaceMetadata",
},
],
[
{
"defaultValue": undefined,
"description": undefined,
"name": "kind",
"required": true,
"type": "'TypeAlias'",
},
{
"defaultValue": undefined,
"description": undefined,
"name": "name",
"required": true,
"type": "string",
},
],
],
}
`)
})
})
35 changes: 27 additions & 8 deletions packages/utils/src/types/getTypeDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface ComponentMetadata extends SharedMetadata {

export interface SharedPropertyMetadata extends SharedMetadata {
defaultValue?: any
required: boolean
required?: boolean
}

export interface FunctionTypePropertyMetadata extends SharedPropertyMetadata {
Expand Down Expand Up @@ -256,7 +256,6 @@ function processTypeAlias(
const metadata: TypeAliasMetadata = {
name,
kind: 'TypeAlias',
name: typeAlias.getName(),
properties: undefined as any,
// Use the type node text if the name is the same as the type to provide a more accurate type
type: nameIsSameAsType
Expand Down Expand Up @@ -734,14 +733,18 @@ function processUnionType(
propertyFilter?: PropertyFilter,
defaultValues?: Record<string, any>
) {
const unionTypeInNodeModules = getSymbolDeclaration(unionType.getSymbol())
?.getSourceFile()
.isInNodeModules()
const allUnionTypes = unionType
.getUnionTypes()
.map((subType) =>
processTypeProperties(
subType,
enclosingNode,
propertyFilter,
defaultValues
defaultValues,
!unionTypeInNodeModules
)
)
const { duplicates, filtered } = parseDuplicateTypes(allUnionTypes)
Expand All @@ -757,7 +760,8 @@ function processTypeProperties(
type: Type,
enclosingNode?: Node,
propertyFilter?: PropertyFilter,
defaultValues: Record<string, any> = {}
defaultValues: Record<string, any> = {},
isUsedLocally = false
): PropertyMetadata[] {
// Handle intersection types by recursively processing each type in the intersection
if (type.isIntersection()) {
Expand All @@ -768,16 +772,31 @@ function processTypeProperties(
intersectType,
enclosingNode,
propertyFilter,
defaultValues
defaultValues,
isUsedLocally
)
)
}

const typeMetadata = getTypeMetadata(type, enclosingNode)

/** If no property filter is provided, short-circuit for types in node_modules. */
if (!propertyFilter && typeMetadata.isInNodeModules) {
return []
if (typeMetadata.isInNodeModules) {
/** If the node modules type is used locally, return the type text and the library that it is from. */
if (isUsedLocally) {
return [
{
type: type.getText(
enclosingNode,
TypeFormatFlags.UseAliasDefinedOutsideCurrentScope
),
},
]
}

/** If no property filter is provided, short-circuit for types in node_modules. */
if (!propertyFilter) {
return []
}
}

/**
Expand Down

0 comments on commit 72f1da8

Please sign in to comment.