Skip to content

Commit

Permalink
fix: do not crash completion when having negated options (#2322)
Browse files Browse the repository at this point in the history
Current completion crashes when using ZSH and having negated options.
  • Loading branch information
alan-agius4 committed Apr 27, 2023
1 parent 2b6ba31 commit 7f42848
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/completion.ts
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
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

0 comments on commit 7f42848

Please sign in to comment.