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 all type errors #1967

Closed
wants to merge 13 commits into from
Closed
5 changes: 3 additions & 2 deletions lib/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ class Argument {
choices(values) {
this.argChoices = values.slice();
this.parseArg = (arg, previous) => {
if (!this.argChoices.includes(arg)) {
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
const argChoices = /** @type {string[]} */ (this.argChoices);
if (!argChoices.includes(arg)) {
throw new InvalidArgumentError(`Allowed choices are ${argChoices.join(', ')}.`);
}
if (this.variadic) {
return this._concatValue(arg, previous);
Expand Down
64 changes: 40 additions & 24 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ class Command extends EventEmitter {
desc = null;
}
opts = opts || {};
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
const [, name, args] = /** @type {[*, string, string | undefined]} */ (
nameAndArgs.match(/([^ ]+) *(.*)/)
);

const cmd = this.createCommand(name);
if (desc) {
Expand Down Expand Up @@ -385,7 +387,9 @@ class Command extends EventEmitter {

_hasImplicitHelpCommand() {
if (this._addImplicitHelpCommand === undefined) {
return this.commands.length && !this._actionHandler && !this._findCommand('help');
return Boolean(
this.commands.length && !this._actionHandler && !this._findCommand('help')
);
}
return this._addImplicitHelpCommand;
}
Expand Down Expand Up @@ -538,7 +542,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
try {
val = option.parseArg(val, oldValue);
} catch (err) {
if (err.code === 'commander.invalidArgument') {
if (err instanceof CommanderError && err.code === 'commander.invalidArgument') {
const message = `${invalidValueMessage} ${err.message}`;
this.error(message, { exitCode: err.exitCode, code: err.code });
}
Expand Down Expand Up @@ -780,15 +784,15 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/

setOptionValue(key, value) {
return this.setOptionValueWithSource(key, value, undefined);
return this.setOptionValueWithSource(key, value);
}

/**
* Store option value and where the value came from.
*
* @param {string} key
* @param {Object} value
* @param {string} source - expected values are default/config/env/cli/implied
* @param {string} [source] - expected values are default/config/env/cli/implied
* @return {Command} `this` command for chaining
*/

Expand All @@ -807,7 +811,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
* Expected values are default | config | env | cli | implied
*
* @param {string} key
* @return {string}
* @return {string | undefined}
*/

getOptionValueSource(key) {
Expand All @@ -819,7 +823,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
* Expected values are default | config | env | cli | implied
*
* @param {string} key
* @return {string}
* @return {string | undefined}
*/

getOptionValueSourceWithGlobals(key) {
Expand Down Expand Up @@ -1070,7 +1074,7 @@ Expecting one of '${allowedValues.join("', '")}'`);

_dispatchSubcommand(commandName, operands, unknown) {
const subCommand = this._findCommand(commandName);
if (!subCommand) this.help({ error: true });
if (!subCommand) return this.help({ error: true });

let hookResult;
hookResult = this._chainOrCallSubCommandHook(hookResult, subCommand, 'preSubcommand');
Expand All @@ -1093,11 +1097,11 @@ Expecting one of '${allowedValues.join("', '")}'`);

_dispatchHelpCommand(subcommandName) {
if (!subcommandName) {
this.help();
return this.help();
}
const subCommand = this._findCommand(subcommandName);
if (subCommand && !subCommand._executableHandler) {
subCommand.help();
return subCommand.help();
}

// Fallback to parsing the help flag to invoke the help.
Expand Down Expand Up @@ -1140,7 +1144,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
try {
parsedValue = argument.parseArg(value, previous);
} catch (err) {
if (err.code === 'commander.invalidArgument') {
if (err instanceof CommanderError && err.code === 'commander.invalidArgument') {
const message = `error: command-argument value '${value}' is invalid for argument '${argument.name()}'. ${err.message}`;
this.error(message, { exitCode: err.exitCode, code: err.code });
}
Expand Down Expand Up @@ -1297,7 +1301,9 @@ Expecting one of '${allowedValues.join("', '")}'`);

let actionResult;
actionResult = this._chainOrCallHooks(actionResult, 'preAction');
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
actionResult = this._chainOrCall(actionResult, () => (
/** @type {Function} */ (this._actionHandler)(this.processedArgs)
));
if (this.parent) {
actionResult = this._chainOrCall(actionResult, () => {
this.parent.emit(commandEvent, operands, unknown); // legacy
Expand Down Expand Up @@ -1348,7 +1354,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
* Return an option matching `arg` if any.
*
* @param {string} arg
* @return {Option}
* @return {Option | undefined}
* @api private
*/

Expand Down Expand Up @@ -1446,7 +1452,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
// parse options
let activeVariadicOption = null;
while (args.length) {
const arg = args.shift();
const arg = /** @type {string} */ (args.shift());

// literal
if (arg === '--') {
Expand Down Expand Up @@ -1619,7 +1625,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (option.envVar && option.envVar in process.env) {
const optionKey = option.attributeName();
// Priority check. Do not overwrite cli or options from unknown source (client-code).
if (this.getOptionValue(optionKey) === undefined || ['default', 'config', 'env'].includes(this.getOptionValueSource(optionKey))) {
if (this.getOptionValue(optionKey) === undefined || (
/** @type {Array} */ (['default', 'config', 'env']).includes(
this.getOptionValueSource(optionKey)
)
)) {
if (option.required || option.optional) { // option can take a value
// keep very simple, optional always takes value
this.emit(`optionEnv:${option.name()}`, process.env[option.envVar]);
Expand All @@ -1640,7 +1650,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
_parseOptionsImplied() {
const dualHelper = new DualOptions(this.options);
const hasCustomOptionValue = (optionKey) => {
return this.getOptionValue(optionKey) !== undefined && !['default', 'implied'].includes(this.getOptionValueSource(optionKey));
return this.getOptionValue(optionKey) !== undefined && !(
/** @type {Array} */ (['default', 'implied']).includes(
this.getOptionValueSource(optionKey)
)
);
};
this.options
.filter(option => (option.implied !== undefined) &&
Expand Down Expand Up @@ -1810,7 +1824,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
* @param {string} str
* @param {string} [flags]
* @param {string} [description]
* @return {this | string} `this` command for chaining, or version string if no arguments
* @return {this | string | undefined} `this` command for chaining, or version string if no arguments
*/

version(str, flags, description) {
Expand All @@ -1837,7 +1851,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
description(str, argsDescription) {
if (str === undefined && argsDescription === undefined) return this._description;
this._description = str;
if (str) {
this._description = str;
}
if (argsDescription) {
this._argsDescription = argsDescription;
}
Expand Down Expand Up @@ -1912,11 +1928,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
const args = this._args.map((arg) => {
return humanReadableArgName(arg);
});
return [].concat(
(this.options.length || this._hasHelpOption ? '[options]' : []),
(this.commands.length ? '[command]' : []),
(this._args.length ? args : [])
).join(' ');
return [
...(this.options.length || this._hasHelpOption ? ['[options]'] : []),
...(this.commands.length ? ['[command]'] : []),
...(this._args.length ? args : [])
].join(' ');
}

this._usage = str;
Expand Down Expand Up @@ -1964,7 +1980,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
* program.executableDir('subcommands');
*
* @param {string} [path]
* @return {string|Command}
* @return {string|null|Command}
*/

executableDir(path) {
Expand Down
2 changes: 1 addition & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CommanderError extends Error {
* Constructs the CommanderError class
* @param {number} exitCode suggested exit code which could be used with process.exit
* @param {string} code an id string representing the error
* @param {string} message human-readable description of the error
* @param {string} [message] human-readable description of the error
* @constructor
*/
constructor(exitCode, code, message) {
Expand Down
4 changes: 3 additions & 1 deletion lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class Help {
const visibleCommands = cmd.commands.filter(cmd => !cmd._hidden);
if (cmd._hasImplicitHelpCommand()) {
// Create a command matching the implicit help command.
const [, helpName, helpArgs] = cmd._helpCommandnameAndArgs.match(/([^ ]+) *(.*)/);
const [, helpName, helpArgs] = /** @type {[*, string, string | undefined]} */ (
cmd._helpCommandnameAndArgs.match(/([^ ]+) *(.*)/)
);
const helpCommand = cmd.createCommand(helpName)
.helpOption(false);
helpCommand.description(cmd._helpCommandDescription);
Expand Down
5 changes: 3 additions & 2 deletions lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ class Option {
choices(values) {
this.argChoices = values.slice();
this.parseArg = (arg, previous) => {
if (!this.argChoices.includes(arg)) {
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
const argChoices = /** @type {string[]} */ (this.argChoices);
if (!argChoices.includes(arg)) {
throw new InvalidArgumentError(`Allowed choices are ${argChoices.join(', ')}.`);
}
if (this.variadic) {
return this._concatValue(arg, previous);
Expand Down
8 changes: 4 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,17 +595,17 @@ export class Command {
/**
* Store option value and where the value came from.
*/
setOptionValueWithSource(key: string, value: unknown, source: OptionValueSource): this;
setOptionValueWithSource(key: string, value: unknown, source?: OptionValueSource | string | undefined): this;

/**
* Get source of option value.
*/
getOptionValueSource(key: string): OptionValueSource | undefined;
getOptionValueSource(key: string): OptionValueSource | string | undefined;

/**
* Get source of option value. See also .optsWithGlobals().
*/
getOptionValueSourceWithGlobals(key: string): OptionValueSource | undefined;
getOptionValueSourceWithGlobals(key: string): OptionValueSource | string | undefined;

/**
* Alter parsing of short flags with optional values.
Expand Down Expand Up @@ -819,7 +819,7 @@ export class Command {
/**
* Get the executable search directory.
*/
executableDir(): string;
executableDir(): string | null;

/**
* Output help information for this command.
Expand Down
6 changes: 3 additions & 3 deletions typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ expectType<commander.Command>(program.setOptionValue('example', true));
expectType<commander.Command>(program.setOptionValueWithSource('example', [], 'cli'));

// getOptionValueSource
expectType<commander.OptionValueSource | undefined>(program.getOptionValueSource('example'));
expectType<string | undefined>(program.getOptionValueSource('example'));

// getOptionValueSourceWithGlobals
expectType<commander.OptionValueSource | undefined>(program.getOptionValueSourceWithGlobals('example'));
expectType<string | undefined>(program.getOptionValueSourceWithGlobals('example'));

// combineFlagAndOptionalValue
expectType<commander.Command>(program.combineFlagAndOptionalValue());
Expand Down Expand Up @@ -275,7 +275,7 @@ expectType<commander.Command>(program.nameFromFilename(__filename));

// executableDir
expectType<commander.Command>(program.executableDir(__dirname));
expectType<string>(program.executableDir());
expectType<string | null>(program.executableDir());

// outputHelp
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type, @typescript-eslint/no-confusing-void-expression
Expand Down