Fix crash when hook entries have no matcher (#160)

Claude Code allows hook entries without a `matcher` field (e.g.,
SessionStart and SubagentStop hooks don't need one). The OpenCode
converter assumed `matcher.matcher` was always present, causing
"undefined is not an object (evaluating 'matcher.matcher.split')"
when converting plugins with matcher-less hooks.

Make `matcher` optional in the type and guard all accesses.
This commit is contained in:
Roberto Mello
2026-02-08 15:59:57 -07:00
committed by GitHub
parent c69c47fe9b
commit f7cab16b06
2 changed files with 8 additions and 6 deletions

View File

@@ -209,9 +209,11 @@ function renderHookStatements(
): string[] {
if (!matcher.hooks || matcher.hooks.length === 0) return []
const tools = matcher.matcher
.split("|")
.map((tool) => tool.trim().toLowerCase())
.filter(Boolean)
? matcher.matcher
.split("|")
.map((tool) => tool.trim().toLowerCase())
.filter(Boolean)
: []
const useMatcher = useToolMatcher && tools.length > 0 && !tools.includes("*")
const condition = useMatcher
@@ -232,10 +234,10 @@ function renderHookStatements(
continue
}
if (hook.type === "prompt") {
statements.push(`// Prompt hook for ${matcher.matcher}: ${hook.prompt.replace(/\n/g, " ")}`)
statements.push(`// Prompt hook for ${matcher.matcher ?? "*"}: ${hook.prompt.replace(/\n/g, " ")}`)
continue
}
statements.push(`// Agent hook for ${matcher.matcher}: ${hook.agent}`)
statements.push(`// Agent hook for ${matcher.matcher ?? "*"}: ${hook.agent}`)
}
return statements

View File

@@ -79,7 +79,7 @@ export type ClaudeHookAgent = {
export type ClaudeHookEntry = ClaudeHookCommand | ClaudeHookPrompt | ClaudeHookAgent
export type ClaudeHookMatcher = {
matcher: string
matcher?: string
hooks: ClaudeHookEntry[]
}