Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/core/completions/generators/powershell-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { POWERSHELL_DYNAMIC_HELPERS } from '../templates/powershell-templates.js
export class PowerShellGenerator implements CompletionGenerator {
readonly shell = 'powershell' as const;

private stripTrailingCommaFromLastLine(lines: string[]): void {
if (lines.length === 0) return;
lines[lines.length - 1] = lines[lines.length - 1].replace(/,\s*$/, '');
}

/**
* Generate a PowerShell completion script
*
Expand All @@ -20,6 +25,7 @@ export class PowerShellGenerator implements CompletionGenerator {
for (const cmd of commands) {
commandLines.push(` @{Name="${cmd.name}"; Description="${this.escapeDescription(cmd.description)}"},`);
}
this.stripTrailingCommaFromLastLine(commandLines);
const topLevelCommands = commandLines.join('\n');

// Build command cases using push() for loop clarity
Expand Down Expand Up @@ -88,6 +94,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
lines.push(`${indent} @{Name="${longFlag}"; Description="${this.escapeDescription(flag.description)}"},`);
}
}
this.stripTrailingCommaFromLastLine(lines);
lines.push(`${indent} )`);
lines.push(`${indent} $flags | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterName", $_.Description)`);
Expand All @@ -103,6 +110,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
for (const subcmd of cmd.subcommands) {
lines.push(`${indent} @{Name="${subcmd.name}"; Description="${this.escapeDescription(subcmd.description)}"},`);
}
this.stripTrailingCommaFromLastLine(lines);
lines.push(`${indent} )`);
lines.push(`${indent} $subcommands | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterValue", $_.Description)`);
Expand Down Expand Up @@ -148,6 +156,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
lines.push(`${indent} @{Name="${longFlag}"; Description="${this.escapeDescription(flag.description)}"},`);
}
}
this.stripTrailingCommaFromLastLine(lines);
lines.push(`${indent} )`);
lines.push(`${indent} $flags | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterName", $_.Description)`);
Expand Down
34 changes: 31 additions & 3 deletions test/core/completions/generators/powershell-generator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {describe, it, expect, beforeEach} from 'vitest';
import {PowerShellGenerator} from '../../../../src/core/completions/generators/powershell-generator.js';
import {CommandDefinition} from '../../../../src/core/completions/types.js';
import { describe, it, expect, beforeEach } from 'vitest';
import { PowerShellGenerator } from '../../../../src/core/completions/generators/powershell-generator.js';
import { CommandDefinition } from '../../../../src/core/completions/types.js';

describe('PowerShellGenerator', () => {
let generator: PowerShellGenerator;
Expand Down Expand Up @@ -444,6 +444,34 @@ describe('PowerShellGenerator', () => {
expect(script).toContain('Get-OpenSpecSpecs');
});

it('should not emit trailing commas in @() arrays', () => {
const commands: CommandDefinition[] = [
{
name: 'config',
description: 'Manage configuration',
flags: [
{
name: 'scope',
short: 's',
description: 'Configuration scope',
},
],
subcommands: [
{
name: 'get',
description: 'Get a config value',
flags: [],
},
],
},
];

const script = generator.generate(commands);

// PowerShell array literals (@(...)) can't have a trailing comma on the last element.
expect(script).not.toMatch(/\},\s*\r?\n\s*\)/);
});

it('should handle empty command list', () => {
const commands: CommandDefinition[] = [];

Expand Down
Loading