Skip to content

Commit 07dd634

Browse files
nobShinjoTabishB
andauthored
fix(powershell-generator): remove trailing comma from last entry (Fission-AI#485)
Co-authored-by: Tabish Bidiwale <30385142+TabishB@users.noreply.github.com>
1 parent 36078b1 commit 07dd634

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

src/core/completions/generators/powershell-generator.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { POWERSHELL_DYNAMIC_HELPERS } from '../templates/powershell-templates.js
88
export class PowerShellGenerator implements CompletionGenerator {
99
readonly shell = 'powershell' as const;
1010

11+
private stripTrailingCommaFromLastLine(lines: string[]): void {
12+
if (lines.length === 0) return;
13+
lines[lines.length - 1] = lines[lines.length - 1].replace(/,\s*$/, '');
14+
}
15+
1116
/**
1217
* Generate a PowerShell completion script
1318
*
@@ -20,6 +25,7 @@ export class PowerShellGenerator implements CompletionGenerator {
2025
for (const cmd of commands) {
2126
commandLines.push(` @{Name="${cmd.name}"; Description="${this.escapeDescription(cmd.description)}"},`);
2227
}
28+
this.stripTrailingCommaFromLastLine(commandLines);
2329
const topLevelCommands = commandLines.join('\n');
2430

2531
// Build command cases using push() for loop clarity
@@ -88,6 +94,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
8894
lines.push(`${indent} @{Name="${longFlag}"; Description="${this.escapeDescription(flag.description)}"},`);
8995
}
9096
}
97+
this.stripTrailingCommaFromLastLine(lines);
9198
lines.push(`${indent} )`);
9299
lines.push(`${indent} $flags | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
93100
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterName", $_.Description)`);
@@ -103,6 +110,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
103110
for (const subcmd of cmd.subcommands) {
104111
lines.push(`${indent} @{Name="${subcmd.name}"; Description="${this.escapeDescription(subcmd.description)}"},`);
105112
}
113+
this.stripTrailingCommaFromLastLine(lines);
106114
lines.push(`${indent} )`);
107115
lines.push(`${indent} $subcommands | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
108116
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterValue", $_.Description)`);
@@ -148,6 +156,7 @@ Register-ArgumentCompleter -CommandName openspec -ScriptBlock $openspecCompleter
148156
lines.push(`${indent} @{Name="${longFlag}"; Description="${this.escapeDescription(flag.description)}"},`);
149157
}
150158
}
159+
this.stripTrailingCommaFromLastLine(lines);
151160
lines.push(`${indent} )`);
152161
lines.push(`${indent} $flags | Where-Object { $_.Name -like "$wordToComplete*" } | ForEach-Object {`);
153162
lines.push(`${indent} [System.Management.Automation.CompletionResult]::new($_.Name, $_.Name, "ParameterName", $_.Description)`);

test/core/completions/generators/powershell-generator.test.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {describe, it, expect, beforeEach} from 'vitest';
2-
import {PowerShellGenerator} from '../../../../src/core/completions/generators/powershell-generator.js';
3-
import {CommandDefinition} from '../../../../src/core/completions/types.js';
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import { PowerShellGenerator } from '../../../../src/core/completions/generators/powershell-generator.js';
3+
import { CommandDefinition } from '../../../../src/core/completions/types.js';
44

55
describe('PowerShellGenerator', () => {
66
let generator: PowerShellGenerator;
@@ -444,6 +444,34 @@ describe('PowerShellGenerator', () => {
444444
expect(script).toContain('Get-OpenSpecSpecs');
445445
});
446446

447+
it('should not emit trailing commas in @() arrays', () => {
448+
const commands: CommandDefinition[] = [
449+
{
450+
name: 'config',
451+
description: 'Manage configuration',
452+
flags: [
453+
{
454+
name: 'scope',
455+
short: 's',
456+
description: 'Configuration scope',
457+
},
458+
],
459+
subcommands: [
460+
{
461+
name: 'get',
462+
description: 'Get a config value',
463+
flags: [],
464+
},
465+
],
466+
},
467+
];
468+
469+
const script = generator.generate(commands);
470+
471+
// PowerShell array literals (@(...)) can't have a trailing comma on the last element.
472+
expect(script).not.toMatch(/\},\s*\r?\n\s*\)/);
473+
});
474+
447475
it('should handle empty command list', () => {
448476
const commands: CommandDefinition[] = [];
449477

0 commit comments

Comments
 (0)