Skip to content

Commit e40a64f

Browse files
authored
Add || [] fallback to all array-returning VscodeAdapter members (#317)
The `terminals` getter already defaulted to [] when the API returned undefined, but `visibleTextEditors`, `extensions`, and `getCommands()` did not. A misbehaving VSCode API could return undefined and crash callers that assume arrays. Benefits: - Consistent defensive pattern across all array-returning members - Callers can safely iterate without null checks - Each fallback has a matching "returns empty array when undefined" test
1 parent c49b507 commit e40a64f

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

packages/rangelink-vscode-extension/src/__tests__/ide/vscode/VscodeAdapter.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,36 @@ describe('VscodeAdapter', () => {
16851685
});
16861686
});
16871687

1688+
describe('getCommands', () => {
1689+
it('should return commands from VSCode API', async () => {
1690+
const commands = ['workbench.action.files.save', 'editor.action.formatDocument'];
1691+
mockVSCode.commands.getCommands.mockResolvedValue(commands);
1692+
1693+
const result = await adapter.getCommands();
1694+
1695+
expect(mockVSCode.commands.getCommands).toHaveBeenCalledWith(false);
1696+
expect(result).toStrictEqual(commands);
1697+
});
1698+
1699+
it('should pass filterInternal parameter', async () => {
1700+
const commands = ['workbench.action.files.save'];
1701+
mockVSCode.commands.getCommands.mockResolvedValue(commands);
1702+
1703+
const result = await adapter.getCommands(true);
1704+
1705+
expect(mockVSCode.commands.getCommands).toHaveBeenCalledWith(true);
1706+
expect(result).toStrictEqual(commands);
1707+
});
1708+
1709+
it('should return empty array when getCommands returns undefined', async () => {
1710+
mockVSCode.commands.getCommands.mockResolvedValue(undefined);
1711+
1712+
const result = await adapter.getCommands();
1713+
1714+
expect(result).toStrictEqual([]);
1715+
});
1716+
});
1717+
16881718
describe('parseUri', () => {
16891719
it('should parse file:// URI using VSCode API', () => {
16901720
const uriString = 'file:///workspace/file.ts';
@@ -2205,6 +2235,14 @@ describe('VscodeAdapter', () => {
22052235
expect(result).toStrictEqual(mockEditors);
22062236
expect(result).toHaveLength(3);
22072237
});
2238+
2239+
it('should return empty array when visibleTextEditors is undefined', () => {
2240+
mockVSCode.window.visibleTextEditors = undefined;
2241+
2242+
const result = adapter.visibleTextEditors;
2243+
2244+
expect(result).toStrictEqual([]);
2245+
});
22082246
});
22092247
});
22102248

@@ -2468,6 +2506,14 @@ describe('VscodeAdapter', () => {
24682506
expect(result).toStrictEqual([]);
24692507
expect(result).toHaveLength(0);
24702508
});
2509+
2510+
it('should return empty array when extensions.all is undefined', () => {
2511+
mockVSCode.extensions.all = undefined;
2512+
2513+
const result = adapter.extensions;
2514+
2515+
expect(result).toStrictEqual([]);
2516+
});
24712517
});
24722518
});
24732519

packages/rangelink-vscode-extension/src/ide/vscode/VscodeAdapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class VscodeAdapter implements ConfigurationProvider, ErrorFeedbackProvid
372372
* @returns Array of all extensions
373373
*/
374374
get extensions(): readonly vscode.Extension<unknown>[] {
375-
return this.ideInstance.extensions.all;
375+
return this.ideInstance.extensions.all || [];
376376
}
377377

378378
/**
@@ -414,7 +414,7 @@ export class VscodeAdapter implements ConfigurationProvider, ErrorFeedbackProvid
414414
* @returns Promise resolving to array of command identifiers
415415
*/
416416
async getCommands(filterInternal = false): Promise<string[]> {
417-
return this.ideInstance.commands.getCommands(filterInternal);
417+
return (await this.ideInstance.commands.getCommands(filterInternal)) || [];
418418
}
419419

420420
// ============================================================================
@@ -682,7 +682,7 @@ export class VscodeAdapter implements ConfigurationProvider, ErrorFeedbackProvid
682682
* @returns Array of visible text editors
683683
*/
684684
get visibleTextEditors(): readonly vscode.TextEditor[] {
685-
return this.ideInstance.window.visibleTextEditors;
685+
return this.ideInstance.window.visibleTextEditors || [];
686686
}
687687

688688
/**

0 commit comments

Comments
 (0)