Skip to content

Commit 9bacf8d

Browse files
committed
Add type assertions
1 parent 2103bc9 commit 9bacf8d

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

lib/argument.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class Argument {
9999
choices(values) {
100100
this.argChoices = values.slice();
101101
this.parseArg = (arg, previous) => {
102-
if (!this.argChoices.includes(arg)) {
103-
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
102+
const argChoices = /** @type {string[]} */ (this.argChoices);
103+
if (!argChoices.includes(arg)) {
104+
throw new InvalidArgumentError(`Allowed choices are ${argChoices.join(', ')}.`);
104105
}
105106
if (this.variadic) {
106107
return this._concatValue(arg, previous);

lib/command.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ class Command extends EventEmitter {
142142
desc = null;
143143
}
144144
opts = opts || {};
145-
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
145+
const [, name, args] = /** @type {[*, string, string | undefined]} */ (
146+
nameAndArgs.match(/([^ ]+) *(.*)/)
147+
);
146148

147149
const cmd = this.createCommand(name);
148150
if (desc) {
@@ -1297,7 +1299,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
12971299

12981300
let actionResult;
12991301
actionResult = this._chainOrCallHooks(actionResult, 'preAction');
1300-
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
1302+
actionResult = this._chainOrCall(actionResult, () => (
1303+
/** @type {Function} */ (this._actionHandler)(this.processedArgs)
1304+
));
13011305
if (this.parent) {
13021306
actionResult = this._chainOrCall(actionResult, () => {
13031307
this.parent.emit(commandEvent, operands, unknown); // legacy
@@ -1446,7 +1450,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
14461450
// parse options
14471451
let activeVariadicOption = null;
14481452
while (args.length) {
1449-
const arg = args.shift();
1453+
const arg = /** @type {string} */ (args.shift());
14501454

14511455
// literal
14521456
if (arg === '--') {

lib/error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CommanderError extends Error {
99
* Constructs the CommanderError class
1010
* @param {number} exitCode suggested exit code which could be used with process.exit
1111
* @param {string} code an id string representing the error
12-
* @param {string} message human-readable description of the error
12+
* @param {string} [message] human-readable description of the error
1313
* @constructor
1414
*/
1515
constructor(exitCode, code, message) {

lib/help.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ class Help {
3030
const visibleCommands = cmd.commands.filter(cmd => !cmd._hidden);
3131
if (cmd._hasImplicitHelpCommand()) {
3232
// Create a command matching the implicit help command.
33-
const [, helpName, helpArgs] = cmd._helpCommandnameAndArgs.match(/([^ ]+) *(.*)/);
33+
const [, helpName, helpArgs] = /** @type {[*, string, string | undefined]} */ (
34+
cmd._helpCommandnameAndArgs.match(/([^ ]+) *(.*)/)
35+
);
3436
const helpCommand = cmd.createCommand(helpName)
3537
.helpOption(false);
3638
helpCommand.description(cmd._helpCommandDescription);

lib/option.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ class Option {
181181
choices(values) {
182182
this.argChoices = values.slice();
183183
this.parseArg = (arg, previous) => {
184-
if (!this.argChoices.includes(arg)) {
185-
throw new InvalidArgumentError(`Allowed choices are ${this.argChoices.join(', ')}.`);
184+
const argChoices = /** @type {string[]} */ (this.argChoices);
185+
if (!argChoices.includes(arg)) {
186+
throw new InvalidArgumentError(`Allowed choices are ${argChoices.join(', ')}.`);
186187
}
187188
if (this.variadic) {
188189
return this._concatValue(arg, previous);

0 commit comments

Comments
 (0)