Skip to content

Commit 5085ddc

Browse files
committed
Get rid of redundant currentParent
.currentParent is expected to never be different from .parent in existing code using the library as intended.
1 parent ea728ec commit 5085ddc

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

lib/command.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class Command extends EventEmitter {
3838
*
3939
* @type {Command | null}
4040
*/
41-
this.currentParent = null;
42-
/** @type {Command | null} */
43-
this.parent = null; // last added parent command (legacy)
41+
this.parent = null;
4442
this._allowUnknownOption = false;
4543
this._allowExcessArguments = true;
4644
/** @type {Argument[]} */
@@ -171,7 +169,7 @@ class Command extends EventEmitter {
171169
if (args) cmd.arguments(args);
172170
this.commands.push(cmd);
173171
cmd.parents.push(this);
174-
cmd.currentParent = cmd.parent = this;
172+
cmd.parent = this;
175173
cmd.copyInheritedSettings(this);
176174

177175
if (desc) return this;
@@ -289,7 +287,7 @@ class Command extends EventEmitter {
289287

290288
this.commands.push(cmd);
291289
cmd.parents.push(this);
292-
cmd.currentParent = cmd.parent = this;
290+
cmd.parent = this;
293291
cmd._checkForBrokenPassThrough();
294292

295293
return this;
@@ -1306,8 +1304,8 @@ Call on top-level command instead`);
13061304
* @api private
13071305
*/
13081306

1309-
_parseCommand(operands, unknown, currentParent) {
1310-
this.currentParent = currentParent ?? null;
1307+
_parseCommand(operands, unknown, parent) {
1308+
this.parent = parent ?? null;
13111309

13121310
const parsed = this.parseOptions(unknown);
13131311
this._parseOptionsEnv(); // after cli, so parseArg not called on both cli and env
@@ -1350,18 +1348,18 @@ Call on top-level command instead`);
13501348
let actionResult;
13511349
actionResult = this._chainOrCallHooks(actionResult, 'preAction');
13521350
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
1353-
if (currentParent) {
1351+
if (parent) {
13541352
actionResult = this._chainOrCall(actionResult, () => {
1355-
currentParent.emit(commandEvent, operands, unknown); // legacy
1353+
parent.emit(commandEvent, operands, unknown); // legacy
13561354
});
13571355
}
13581356
actionResult = this._chainOrCallHooks(actionResult, 'postAction');
13591357
return actionResult;
13601358
}
1361-
if (currentParent?.listenerCount(commandEvent)) {
1359+
if (parent?.listenerCount(commandEvent)) {
13621360
checkForUnknownOptions();
13631361
this._processArguments();
1364-
currentParent.emit(commandEvent, operands, unknown); // legacy
1362+
parent.emit(commandEvent, operands, unknown); // legacy
13651363
} else if (operands.length) {
13661364
if (this._findCommand('*')) { // legacy default command
13671365
return this._dispatchSubcommand('*', operands, unknown);
@@ -1417,7 +1415,7 @@ Call on top-level command instead`);
14171415

14181416
_checkForMissingMandatoryOptions() {
14191417
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1420-
for (let cmd = this; cmd; cmd = cmd.currentParent) {
1418+
for (let cmd = this; cmd; cmd = cmd.parent) {
14211419
cmd.options.forEach((anOption) => {
14221420
if (anOption.mandatory && (cmd.getOptionValue(anOption.attributeName()) === undefined)) {
14231421
cmd.missingMandatoryOptionValue(anOption);
@@ -1464,7 +1462,7 @@ Call on top-level command instead`);
14641462
*/
14651463
_checkForConflictingOptions() {
14661464
// Walk up hierarchy so can call in subcommand after checking for displaying help.
1467-
for (let cmd = this; cmd; cmd = cmd.currentParent) {
1465+
for (let cmd = this; cmd; cmd = cmd.parent) {
14681466
cmd._checkForConflictingLocalOptions();
14691467
}
14701468
}
@@ -1801,7 +1799,7 @@ Call on top-level command instead`);
18011799
.filter(option => option.long)
18021800
.map(option => option.long);
18031801
candidateFlags = candidateFlags.concat(moreFlags);
1804-
command = command.currentParent;
1802+
command = command.parent;
18051803
} while (command && !command._enablePositionalOptions);
18061804
suggestion = suggestSimilar(flag, candidateFlags);
18071805
}
@@ -1822,7 +1820,7 @@ Call on top-level command instead`);
18221820

18231821
const expected = this._args.length;
18241822
const s = (expected === 1) ? '' : 's';
1825-
const forSubcommand = this.currentParent ? ` for '${this.name()}'` : '';
1823+
const forSubcommand = this.parent ? ` for '${this.name()}'` : '';
18261824
const message = `error: too many arguments${forSubcommand}. Expected ${expected} argument${s} but got ${receivedArgs.length}.`;
18271825
this.error(message, { code: 'commander.excessArguments' });
18281826
}
@@ -2239,7 +2237,7 @@ function incrementNodeInspectorPort(args) {
22392237

22402238
function getCommandAndParents(startCommand) {
22412239
const result = [];
2242-
for (let command = startCommand; command; command = command.currentParent) {
2240+
for (let command = startCommand; command; command = command.parent) {
22432241
result.push(command);
22442242
}
22452243
return result;

lib/help.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Help {
101101
if (!this.showGlobalOptions) return [];
102102

103103
const globalOptions = [];
104-
for (let parentCmd = cmd.currentParent; parentCmd; parentCmd = parentCmd.currentParent) {
104+
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
105105
const visibleOptions = parentCmd.options.filter((option) => !option.hidden);
106106
globalOptions.push(...visibleOptions);
107107
}
@@ -241,7 +241,7 @@ class Help {
241241
cmdName = cmdName + '|' + cmd._aliases[0];
242242
}
243243
let parentCmdNames = '';
244-
for (let parentCmd = cmd.currentParent; parentCmd; parentCmd = parentCmd.currentParent) {
244+
for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {
245245
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
246246
}
247247
return parentCmdNames + cmdName + ' ' + cmd.usage();

0 commit comments

Comments
 (0)