Skip to content

refactor: extract getRootOpts() helper to replace fragile parent chain navigation #61

@iamfj

Description

@iamfj

Problem

Every command action navigates the Commander parent chain manually:

const ctx = createContext(command.parent!.parent!.opts());

This pattern appears ~25 times across all command files. It is:

  • Fragile — breaks silently if command nesting changes (wrong opts object, missing API token)
  • Non-obvious — a new contributor has to understand the Commander tree to know why it's .parent!.parent!
  • Non-null assertions! operators mask potential runtime errors

Proposed fix

Extract a helper in src/common/context.ts:

import type { Command } from "commander";

export function getRootOpts(command: Command): CommandOptions {
  let current: Command | null = command;
  while (current?.parent) {
    current = current.parent;
  }
  return current.opts() as CommandOptions;
}

Then replace all call sites:

// Before
const ctx = createContext(command.parent!.parent!.opts());

// After
const ctx = createContext(getRootOpts(command));

Affected files

All files in src/commands/:
auth.ts, comments.ts, cycles.ts, documents.ts, files.ts, issues.ts, labels.ts, milestones.ts, projects.ts, teams.ts, users.ts

Acceptance criteria

  • getRootOpts() helper exists and walks to the root command
  • All command.parent!.parent!.opts() replaced
  • No parent! assertions remain in command files
  • All existing tests pass

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions