Skip to content

Commit e3d2460

Browse files
fix(activate): resolve manifest rows before reading them
The commandManifest spec read `row.when` right after a `toBeDefined` assertion. `Array.prototype.find` answers `T | undefined` and no vitest matcher narrows it back, so the reads at src/activate/__tests__/commandManifest.spec.ts:36,42,48,56 and 80 failed the hosted `pnpm run check-types` (tsc --noEmit in src) with five TS18048 errors and turned the compile context red. `resolved()` returns the row or throws with that row's own message, so the lookups stay non-optional without a cast and a dropped row still fails with the message it carried before. Gates (pinned node v22.23.4, worktree at this commit): tsc --noEmit in src exit 0 (was exit 2 with the five errors); eslint --max-warnings=0 and prettier --check on the file exit 0; vitest misc lane on the file 21 passed before and after; mutating the editor/title row -> 4 failed/17 passed, flipping its when clause -> 1 failed/20 passed, renaming the declaration -> 2 failed/19 passed.
1 parent 050b5f0 commit e3d2460

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

‎src/activate/__tests__/commandManifest.spec.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ const manifest = JSON.parse(fs.readFileSync(fileURLToPath(new URL("../../package
1111
const commands: { command: string; title: string }[] = manifest.contributes.commands
1212
const menus = manifest.contributes.menus as Record<string, { command: string; when?: string }[]>
1313

14+
// `find` answers `T | undefined`, and TS does not narrow a row back through a `toBeDefined` assertion,
15+
// so the `row.when` read that follows such an assertion stays a type error. Resolving every lookup
16+
// through this helper keeps each row's own failure message and makes the dropped row the thing that
17+
// fails, rather than the property read after it.
18+
function resolved<T>(rows: T[], matches: (row: T) => boolean, message: string): T {
19+
const row = rows.find(matches)
20+
if (!row) {
21+
throw new Error(message)
22+
}
23+
return row
24+
}
25+
1426
const TAB_PANEL_ID = "zoo-code.TabPanelProvider"
1527
const SIDEBAR_VIEW_CLAUSE = `view == zoo-code.SidebarProvider`
1628
const TAB_ACTIVE_CLAUSE = `activeWebviewPanelId == ${TAB_PANEL_ID}`
@@ -31,28 +43,24 @@ describe("contributes manifest for the title-bar commands", () => {
3143
})
3244

3345
it.each(tabTitleCommands)("%s has an editor/title row gated on the tab panel", (id) => {
34-
const row = menus["editor/title"].find((r) => r.command === id)
35-
expect(row, `${id} must appear in editor/title`).toBeDefined()
46+
const row = resolved(menus["editor/title"], (r) => r.command === id, `${id} must appear in editor/title`)
3647
expect(row.when, `${id} must be gated on the active tab panel`).toBe(TAB_ACTIVE_CLAUSE)
3748
})
3849

3950
it.each(tabTitleCommands)("%s has a commandPalette row gated on the tab panel", (id) => {
40-
const row = menus.commandPalette.find((r) => r.command === id)
41-
expect(row, `${id} must appear in commandPalette`).toBeDefined()
51+
const row = resolved(menus.commandPalette, (r) => r.command === id, `${id} must appear in commandPalette`)
4252
expect(row.when, `${id} must be gated on the active tab panel`).toBe(TAB_ACTIVE_CLAUSE)
4353
})
4454

4555
it.each(sidebarTitleCommands)("%s has a view/title row gated on the sidebar view", (id) => {
46-
const row = menus["view/title"].find((r) => r.command === id)
47-
expect(row, `${id} must appear in view/title`).toBeDefined()
56+
const row = resolved(menus["view/title"], (r) => r.command === id, `${id} must appear in view/title`)
4857
expect(row.when, `${id} must be gated on the sidebar view`).toBe(SIDEBAR_VIEW_CLAUSE)
4958
})
5059

5160
// popoutButtonClicked is a view/title-only row: it has no commandPalette entry, and the four
5261
// sidebar task commands each have one hidden while the tab panel is the active webview.
5362
it.each(sidebarTaskCommands)("%s has a commandPalette row hidden while the tab panel is active", (id) => {
54-
const row = menus.commandPalette.find((r) => r.command === id)
55-
expect(row, `${id} must appear in commandPalette`).toBeDefined()
63+
const row = resolved(menus.commandPalette, (r) => r.command === id, `${id} must appear in commandPalette`)
5664
expect(row.when, `${id} must be hidden while the tab panel is the active webview`).toBe(TAB_INACTIVE_CLAUSE)
5765
})
5866
it("keeps popoutButtonClicked out of the command palette", () => {
@@ -75,8 +83,7 @@ describe("contributes manifest for the title-bar commands", () => {
7583
})
7684
it("names the title-bar commands with i18n placeholders, not literals", () => {
7785
for (const id of titleBarCommands) {
78-
const entry = commands.find((c) => c.command === id)
79-
expect(entry, `${id} must be declared`).toBeDefined()
86+
const entry = resolved(commands, (c) => c.command === id, `${id} must be declared`)
8087
expect(entry.title, `${id} must carry a %placeholder title`).toMatch(/^%/)
8188
}
8289
})

0 commit comments

Comments
 (0)