Skip to content
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

fix: do not crash completion when having negable options #2322

Merged
merged 1 commit into from
Apr 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 25 additions & 19 deletions lib/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ export class Completion implements CompletionInstance {
!options.hiddenOptions.includes(key) &&
!this.argsContainKey(args, key, negable)
) {
this.completeOptionKey(key, completions, current);
if (negable && !!options.default[key])
this.completeOptionKey(`no-${key}`, completions, current);
this.completeOptionKey(
key,
completions,
current,
negable && !!options.default[key]
);
}
});
}
Expand Down Expand Up @@ -248,28 +251,31 @@ export class Completion implements CompletionInstance {
private completeOptionKey(
key: string,
completions: string[],
current: string
current: string,
negable: boolean
) {
const descs = this.usage.getDescriptions();
const startsByTwoDashes = (s: string) => /^--/.test(s);
const isShortOption = (s: string) => /^[^0-9]$/.test(s);
const dashes =
!startsByTwoDashes(current) && isShortOption(key) ? '-' : '--';
if (!this.zshShell) {
completions.push(dashes + key);
} else {
const aliasKey = this?.aliases?.[key].find(alias => {
let keyWithDesc = key;
if (this.zshShell) {
const descs = this.usage.getDescriptions();
const aliasKey = this?.aliases?.[key]?.find(alias => {
const desc = descs[alias];
return typeof desc === 'string' && desc.length > 0;
});
const descFromAlias = aliasKey ? descs[aliasKey] : undefined;
const desc = descs[key] ?? descFromAlias ?? '';
completions.push(
dashes +
`${key.replace(/:/g, '\\:')}:${desc
.replace('__yargsString__:', '')
.replace(/(\r\n|\n|\r)/gm, ' ')}`
);
keyWithDesc = `${key.replace(/:/g, '\\:')}:${desc
.replace('__yargsString__:', '')
.replace(/(\r\n|\n|\r)/gm, ' ')}`;
}

const startsByTwoDashes = (s: string) => /^--/.test(s);
const isShortOption = (s: string) => /^[^0-9]$/.test(s);
const dashes =
!startsByTwoDashes(current) && isShortOption(key) ? '-' : '--';

completions.push(dashes + keyWithDesc);
if (negable) {
completions.push(dashes + 'no-' + keyWithDesc);
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/completion.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,28 @@ describe('Completion', () => {
r.logs.should.include('--foo:bar');
});

it('completes with no- prefix flags defaulting to true when boolean-negation is set', () => {
process.env.SHELL = '/bin/zsh';

const r = checkUsage(
() =>
yargs(['./completion', '--get-yargs-completions', '--'])
.options({
foo: {describe: 'foo flag', type: 'boolean', default: true},
bar: {describe: 'bar flag', type: 'boolean'},
})
.parserConfiguration({'boolean-negation': true}).argv
);

r.logs.should.eql([
'--help:Show help',
'--version:Show version number',
'--foo:foo flag',
'--no-foo:foo flag',
'--bar:bar flag',
]);
});

it('bails out early when full command matches', () => {
process.env.SHELL = '/bin/zsh';
const r = checkUsage(() => {
Expand Down