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

Refactor getCommandAndParents() into Command._getCommandAndAncestors() and use consistently #1939

Merged
merged 3 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 25 additions & 27 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ class Command extends EventEmitter {
return this;
}

/**
* @returns {Command[]}
* @api private
*/

_getCommandAndAncestors() {
const result = [];
for (let command = this; command; command = command.parent) {
result.push(command);
}
return result;
}

/**
* Define a command.
*
Expand Down Expand Up @@ -825,7 +838,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
getOptionValueSourceWithGlobals(key) {
// global overwrites local, like optsWithGlobals
let source;
getCommandAndParents(this).forEach((cmd) => {
this._getCommandAndAncestors().forEach((cmd) => {
if (cmd.getOptionValueSource(key) !== undefined) {
source = cmd.getOptionValueSource(key);
}
Expand Down Expand Up @@ -1208,7 +1221,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
_chainOrCallHooks(promise, event) {
let result = promise;
const hooks = [];
getCommandAndParents(this)
this._getCommandAndAncestors()
.reverse()
.filter(cmd => cmd._lifeCycleHooks[event] !== undefined)
.forEach(hookedCommand => {
Expand Down Expand Up @@ -1365,13 +1378,13 @@ Expecting one of '${allowedValues.join("', '")}'`);

_checkForMissingMandatoryOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd.options.forEach((anOption) => {
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
cmd.missingMandatoryOptionValue(anOption);
}
});
}
});
}

/**
Expand Down Expand Up @@ -1412,9 +1425,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
_checkForConflictingOptions() {
// Walk up hierarchy so can call in subcommand after checking for displaying help.
for (let cmd = this; cmd; cmd = cmd.parent) {
this._getCommandAndAncestors().forEach((cmd) => {
cmd._checkForConflictingLocalOptions();
}
});
}

/**
Expand Down Expand Up @@ -1577,7 +1590,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
optsWithGlobals() {
// globals overwrite locals
return getCommandAndParents(this).reduce(
return this._getCommandAndAncestors().reduce(
(combinedOptions, cmd) => Object.assign(combinedOptions, cmd.opts()),
{}
);
Expand Down Expand Up @@ -1743,14 +1756,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (flag.startsWith('--') && this._showSuggestionAfterError) {
// Looping to pick up the global options too
let candidateFlags = [];
let command = this;
do {
for (const command of this._getCommandAndAncestors()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a forEach? (I don't like the for ( of ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be if not for the early exit that is impossible with forEach. I guess this is one of the cases where iterating manually via .parent is more appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const moreFlags = command.createHelp().visibleOptions(command)
.filter(option => option.long)
.map(option => option.long);
candidateFlags = candidateFlags.concat(moreFlags);
command = command.parent;
} while (command && !command._enablePositionalOptions);
if (command._enablePositionalOptions) break;
}
suggestion = suggestSimilar(flag, candidateFlags);
}

Expand Down Expand Up @@ -2022,7 +2034,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
const context = this._getHelpContext(contextOptions);

getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));
this._getCommandAndAncestors().reverse().forEach(command => command.emit('beforeAllHelp', context));
this.emit('beforeHelp', context);

let helpInformation = this.helpInformation(context);
Expand All @@ -2036,7 +2048,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

this.emit(this._helpLongFlag); // deprecated
this.emit('afterHelp', context);
getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
this._getCommandAndAncestors().forEach(command => command.emit('afterAllHelp', context));
}

/**
Expand Down Expand Up @@ -2179,18 +2191,4 @@ function incrementNodeInspectorPort(args) {
});
}

/**
* @param {Command} startCommand
* @returns {Command[]}
* @api private
*/

function getCommandAndParents(startCommand) {
const result = [];
for (let command = startCommand; command; command = command.parent) {
result.push(command);
}
return result;
}

exports.Command = Command;
16 changes: 8 additions & 8 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class Help {
if (!this.showGlobalOptions) return [];

const globalOptions = [];
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Having to use the slice makes this too subtle for my liking.

Copy link
Collaborator

@shadowspawn shadowspawn Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Ok with the active variable changing to ancestorCmd though.)

const visibleOptions = ancestorCmd.options.filter((option) => !option.hidden);
globalOptions.push(...visibleOptions);
}
});
if (this.sortOptions) {
globalOptions.sort(this.compareOptions);
}
Expand Down Expand Up @@ -240,11 +240,11 @@ class Help {
if (cmd._aliases[0]) {
cmdName = cmdName + '|' + cmd._aliases[0];
}
let parentCmdNames = '';
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
}
return parentCmdNames + cmdName + ' ' + cmd.usage();
let ancestorCmdNames = '';
cmd._getCommandAndAncestors().slice(1).forEach((ancestorCmd) => {
Copy link
Collaborator

@shadowspawn shadowspawn Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Having to use the slice makes this too subtle for my liking.
Ok with the active variables changing to ancestorX though.

ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
});
return ancestorCmdNames + cmdName + ' ' + cmd.usage();
}

/**
Expand Down