Skip to content

feat: consistent return types #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 16 additions & 7 deletions src/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as bash from './bash';
import * as fish from './fish';
import * as powershell from './powershell';
import type { CAC } from 'cac';
import { Completion } from './';
import { Completion } from './index';
import { noopHandler, TabFunction } from './shared';

const execPath = process.execPath;
const processArgs = process.argv.slice(1);
Expand All @@ -17,7 +18,7 @@ function quoteIfNeeded(path: string): string {
return path.includes(' ') ? `'${path}'` : path;
}

export default function tab(instance: CAC): Completion {
const tab: TabFunction<CAC> = async (instance, completionConfig) => {
const completion = new Completion();

// Add all commands and their options
Expand All @@ -29,24 +30,30 @@ export default function tab(instance: CAC): Completion {
arg.startsWith('[')
); // true if optional (wrapped in [])

const isRootCommand = cmd.name === '@@global@@';
const commandCompletionConfig = isRootCommand
? completionConfig
: completionConfig?.subCommands?.[cmd.name];

// Add command to completion
const commandName = completion.addCommand(
cmd.name === '@@global@@' ? '' : cmd.name,
isRootCommand ? '' : cmd.name,
cmd.description || '',
args,
async () => []
commandCompletionConfig?.handler ?? noopHandler
);

// Add command options
for (const option of [...instance.globalCommand.options, ...cmd.options]) {
// Extract short flag from the name if it exists (e.g., "-c, --config" -> "c")
const shortFlag = option.name.match(/^-([a-zA-Z]), --/)?.[1];
const argName = option.name.replace(/^-[a-zA-Z], --/, '');

completion.addOption(
commandName,
`--${option.name.replace(/^-[a-zA-Z], --/, '')}`, // Remove the short flag part if it exists
`--${argName}`, // Remove the short flag part if it exists
option.description || '',
async () => [],
commandCompletionConfig?.options?.[argName]?.handler ?? noopHandler,
shortFlag
);
}
Expand Down Expand Up @@ -93,4 +100,6 @@ export default function tab(instance: CAC): Completion {
});

return completion;
}
};

export default tab;
32 changes: 9 additions & 23 deletions src/citty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import * as zsh from './zsh';
import * as bash from './bash';
import * as fish from './fish';
import * as powershell from './powershell';
import { Completion, type Handler } from '.';
import { Completion } from './index';
import type {
ArgsDef,
CommandDef,
PositionalArgDef,
SubCommandsDef,
} from 'citty';
import { generateFigSpec } from './fig';
import { CompletionConfig, noopHandler, TabFunction } from './shared';

function quoteIfNeeded(path: string) {
return path.includes(' ') ? `'${path}'` : path;
Expand All @@ -30,23 +31,6 @@ function isConfigPositional<T extends ArgsDef>(config: CommandDef<T>) {
);
}

// TODO (43081j): use type inference some day, so we can type-check
// that the sub commands exist, the options exist, etc.
interface CompletionConfig {
handler?: Handler;
subCommands?: Record<string, CompletionConfig>;
options?: Record<
string,
{
handler: Handler;
}
>;
}

const noopHandler: Handler = () => {
return [];
};

async function handleSubCommands(
completion: Completion,
subCommands: SubCommandsDef,
Expand Down Expand Up @@ -108,10 +92,10 @@ async function handleSubCommands(
}
}

export default async function tab<TArgs extends ArgsDef>(
instance: CommandDef<TArgs>,
completionConfig?: CompletionConfig
) {
const tab: TabFunction<CommandDef<ArgsDef>> = async (
instance,
completionConfig
) => {
const completion = new Completion();

const meta = await resolve(instance.meta);
Expand Down Expand Up @@ -220,7 +204,9 @@ export default async function tab<TArgs extends ArgsDef>(
subCommands.complete = completeCommand;

return completion;
}
};

export default tab;

type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);

Expand Down
23 changes: 23 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Handler, Completion } from './index';

export const noopHandler: Handler = () => {
return [];
};

// TODO (43081j): use type inference some day, so we can type-check
// that the sub commands exist, the options exist, etc.
export interface CompletionConfig {
handler?: Handler;
subCommands?: Record<string, CompletionConfig>;
options?: Record<
string,
{
handler: Handler;
}
>;
}

export type TabFunction<T> = (
instance: T,
completionConfig?: CompletionConfig
) => Promise<Completion>;
Loading