Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reject-malformed-vcs-watch-hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hunkdiff": patch
---

Extension VCS operation registration now requires `watchSignature` and `watchPlan` to be absent or callable. A non-callable hook is dropped along with its operation at registration instead of throwing a `TypeError` the first time watch planning invokes it.
30 changes: 30 additions & 0 deletions packages/hunk/src/extensions/publicApiRobustness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,36 @@ describe("registerVcsAdapter with junk", () => {
expect(registry.vcsAdapters[0]?.adapter.operations).toEqual({});
});

test("an operation with a non-callable watch hook is dropped, not wrapped", () => {
const { registry, issues } = loadFactory(
(hunk: { registerVcsAdapter: (adapter: unknown) => void }) => {
hunk.registerVcsAdapter({
id: "hg",
name: "Mercurial",
detect: (cwd: string) => ({ id: "hg", repoRoot: cwd }),
operations: {
"working-tree-diff": {
load: async () => ({ files: [] }),
watchSignature: "not-a-function",
},
"revision-show": {
load: async () => ({ files: [] }),
watchPlan: 42,
},
"stash-show": { load: async () => ({ files: [] }) },
},
});
},
);

expect(issues).toEqual([]);
// Truthy-but-uncallable hooks would otherwise pass the `watchSignature &&`
// guard in toInternalVcsOperation and fail with a TypeError only once
// watch planning invokes them, well after registration.
const operations = registry.vcsAdapters[0]?.adapter.operations ?? {};
expect(Object.keys(operations)).toEqual(["stash-show"]);
});

test("built-in ids stay reserved however an extension asks for them", () => {
const { registry } = loadFactory((hunk: { registerVcsAdapter: (a: unknown) => void }) => {
for (const id of ["git", "jj", "sl"]) {
Expand Down
7 changes: 6 additions & 1 deletion packages/hunk/src/extensions/runExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,12 @@ export function toInternalVcsAdapter(

const internalOperations: Record<string, VcsOperation<VcsReviewInput>> = {};
for (const [kind, operation] of Object.entries(operations ?? {})) {
if (isPlainObject(operation) && typeof operation.load === "function") {
if (
isPlainObject(operation) &&
typeof operation.load === "function" &&
(operation.watchSignature === undefined || typeof operation.watchSignature === "function") &&
(operation.watchPlan === undefined || typeof operation.watchPlan === "function")
) {
internalOperations[kind] = toInternalVcsOperation(
operation as unknown as ExtensionVcsOperation<VcsReviewInput>,
);
Expand Down