Skip to content

Commit f7cab16

Browse files
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.
1 parent c69c47f commit f7cab16

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/converters/claude-to-opencode.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,11 @@ function renderHookStatements(
209209
): string[] {
210210
if (!matcher.hooks || matcher.hooks.length === 0) return []
211211
const tools = matcher.matcher
212-
.split("|")
213-
.map((tool) => tool.trim().toLowerCase())
214-
.filter(Boolean)
212+
? matcher.matcher
213+
.split("|")
214+
.map((tool) => tool.trim().toLowerCase())
215+
.filter(Boolean)
216+
: []
215217

216218
const useMatcher = useToolMatcher && tools.length > 0 && !tools.includes("*")
217219
const condition = useMatcher
@@ -232,10 +234,10 @@ function renderHookStatements(
232234
continue
233235
}
234236
if (hook.type === "prompt") {
235-
statements.push(`// Prompt hook for ${matcher.matcher}: ${hook.prompt.replace(/\n/g, " ")}`)
237+
statements.push(`// Prompt hook for ${matcher.matcher ?? "*"}: ${hook.prompt.replace(/\n/g, " ")}`)
236238
continue
237239
}
238-
statements.push(`// Agent hook for ${matcher.matcher}: ${hook.agent}`)
240+
statements.push(`// Agent hook for ${matcher.matcher ?? "*"}: ${hook.agent}`)
239241
}
240242

241243
return statements

src/types/claude.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type ClaudeHookAgent = {
7979
export type ClaudeHookEntry = ClaudeHookCommand | ClaudeHookPrompt | ClaudeHookAgent
8080

8181
export type ClaudeHookMatcher = {
82-
matcher: string
82+
matcher?: string
8383
hooks: ClaudeHookEntry[]
8484
}
8585

0 commit comments

Comments
 (0)