Skip to content

Commit 53d7cb9

Browse files
committed
refactor: add type safety improvements to TOC command
Replace string-based position validation with proper type guards and type-safe validation. This ensures compile-time type safety when converting CLI options to internal operation options.
1 parent c72b987 commit 53d7cb9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/commands/toc.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,29 @@ export async function tocCommand(
413413
cliOptions: TocCliOptions
414414
): Promise<void> {
415415
// Validate position option
416-
const validPositions = ['top', 'after-title', 'before-content', 'replace'];
417-
if (cliOptions.position && !validPositions.includes(cliOptions.position)) {
418-
throw new Error(`Invalid position: ${cliOptions.position}. Must be one of: ${validPositions.join(', ')}`);
416+
const validPositions: readonly TocOperationOptions['position'][] = [
417+
'top',
418+
'after-title',
419+
'before-content',
420+
'replace',
421+
];
422+
const isValidPosition = (pos: string): pos is TocOperationOptions['position'] => {
423+
return pos === 'top' || pos === 'after-title' || pos === 'before-content' || pos === 'replace';
424+
};
425+
426+
if (cliOptions.position && !isValidPosition(cliOptions.position)) {
427+
throw new Error(
428+
`Invalid position: ${cliOptions.position}. Must be one of: ${validPositions.join(', ')}`
429+
);
419430
}
420431

421432
// Convert CLI options to internal options
422433
const options: TocOperationOptions = {
423434
...cliOptions,
424-
position: (cliOptions.position as TocOperationOptions['position']) ?? 'after-title',
435+
position:
436+
cliOptions.position && isValidPosition(cliOptions.position)
437+
? cliOptions.position
438+
: 'after-title',
425439
};
426440

427441
try {

0 commit comments

Comments
 (0)