docs-util: handle workflows with intersection hook types (#12099)

This commit is contained in:
Shahed Nasser
2025-04-07 14:00:08 +03:00
committed by GitHub
parent 5b5fea29c7
commit fde85888bd
3 changed files with 87 additions and 24 deletions
@@ -1,7 +1,7 @@
import { MarkdownTheme } from "../../theme.js" import { MarkdownTheme } from "../../theme.js"
import Handlebars from "handlebars" import Handlebars from "handlebars"
import { DeclarationReflection, SignatureReflection } from "typedoc" import { DeclarationReflection, SignatureReflection } from "typedoc"
import { cleanUpHookInput, getProjectChild } from "utils" import { cleanUpHookInput, getHookChildren, getProjectChild } from "utils"
import beautifyCode from "../../utils/beautify-code.js" import beautifyCode from "../../utils/beautify-code.js"
export default function (theme: MarkdownTheme) { export default function (theme: MarkdownTheme) {
@@ -25,28 +25,39 @@ export default function (theme: MarkdownTheme) {
Handlebars.helpers.incrementCurrentTitleLevel() Handlebars.helpers.incrementCurrentTitleLevel()
const hooksTitleLevel = Handlebars.helpers.titleLevel() const hooksTitleLevel = Handlebars.helpers.titleLevel()
const hookChildrenProperty = this.parent?.getChildByName("hooks") const hookChildren = getHookChildren(this.parent)
const hookChildren =
hookChildrenProperty instanceof DeclarationReflection &&
hookChildrenProperty.type?.type === "reflection"
? hookChildrenProperty.type.declaration.children || []
: []
hooks.forEach((hook) => { hooks.forEach((hook) => {
const hookReflection = const hookReflection =
hookChildren.find( hookChildren.find((child) => {
(child) => child.name === hook.name && child.signatures?.length if (child.name !== hook.name) {
) || return false
}
if (child.signatures?.length) {
return true
}
return (
child.type?.type === "reflection" &&
child.type.declaration.signatures?.length
)
}) ||
((this.parent.getChildByName(hook.name) || ((this.parent.getChildByName(hook.name) ||
getProjectChild( getProjectChild(theme.project!, hook.name)) as
theme.project!, | DeclarationReflection
hook.name | undefined)
)) as DeclarationReflection)
const signatures =
hookReflection?.signatures ||
(hookReflection?.type?.type === "reflection"
? hookReflection.type.declaration.signatures
: [])
if ( if (
!hookReflection || !hookReflection ||
!hookReflection.signatures?.length || !signatures?.length ||
!hookReflection.signatures[0].parameters?.length !signatures[0].parameters?.length
) { ) {
return return
} }
@@ -75,7 +86,7 @@ export default function (theme: MarkdownTheme) {
str += `Handlers consuming this hook accept the following input.\n\n` str += `Handlers consuming this hook accept the following input.\n\n`
str += Handlebars.helpers.parameterComponent.call( str += Handlebars.helpers.parameterComponent.call(
cleanUpHookInput(hookReflection.signatures[0].parameters), cleanUpHookInput(signatures[0].parameters),
{ {
hash: { hash: {
sectionTitle: hook.name, sectionTitle: hook.name,
@@ -493,12 +493,32 @@ class WorkflowsPlugin {
workflowComments?: CommentTag[] workflowComments?: CommentTag[]
workflowReflection: DeclarationReflection workflowReflection: DeclarationReflection
}): DeclarationReflection { }): DeclarationReflection {
const declarationReflection = context.createDeclarationReflection( const hooksProperty = workflowReflection.getChildByName("hooks")
ReflectionKind.Function, let declarationReflection: DeclarationReflection | undefined
undefined, if (
undefined, hooksProperty?.isDeclaration() &&
stepId hooksProperty.type?.type === "intersection"
) ) {
hooksProperty.type.types.some((hookType) => {
if (hookType.type !== "reflection") {
return
}
declarationReflection = hookType.declaration.children?.find((child) => {
return child.name === stepId
})
return declarationReflection !== undefined
})
}
if (!declarationReflection) {
declarationReflection = context.createDeclarationReflection(
ReflectionKind.Function,
undefined,
undefined,
stepId
)
}
declarationReflection.comment = new Comment() declarationReflection.comment = new Comment()
@@ -579,7 +599,6 @@ class WorkflowsPlugin {
]) ])
) )
const hooksProperty = workflowReflection.getChildByName("hooks")
if ( if (
hooksProperty?.isDeclaration() && hooksProperty?.isDeclaration() &&
hooksProperty.type?.type === "reflection" hooksProperty.type?.type === "reflection"
@@ -5,6 +5,39 @@ import {
Reflection, Reflection,
} from "typedoc" } from "typedoc"
export function getHookChildren(
reflection?: DeclarationReflection
): DeclarationReflection[] {
const hookChildren: DeclarationReflection[] = []
if (!reflection) {
return hookChildren
}
const hookChildrenProperty = reflection.getChildByName("hooks")
if (!(hookChildrenProperty instanceof DeclarationReflection)) {
return hookChildren
}
switch (hookChildrenProperty.type?.type) {
case "reflection":
hookChildren.push(
...(hookChildrenProperty.type.declaration.children || [])
)
break
case "intersection":
hookChildrenProperty.type.types.forEach((type) => {
if (type.type !== "reflection") {
return
}
hookChildren.push(...(type.declaration.children || []))
})
break
}
return hookChildren
}
export function cleanUpHookInput( export function cleanUpHookInput(
parameters: ParameterReflection[] parameters: ParameterReflection[]
): ParameterReflection[] { ): ParameterReflection[] {