Skip to content
Myr

Fixing lint errors in TypeScript (#1208)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Fixing lint errors in TypeScript * Fix lint errors for constructor declarations * Disable lint error for global+namepsace * Fix new warning for space before arrow of arrow function

authored by John Gee · committed by GitHub · parent ebc8b41 · Browse files · 2 files+98 added−83 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+98 added−83 removed

  • other:+98 added−83 removed

2 files+98 added−83 removed

modified:typings/commander-tests.ts+55 added-40 removed
1import * as commander from './index';2import { Script } from 'vm';23// Test Commander usage with TypeScript.4// This is NOT a usable program, just used to test for compile errors!56// We declare lots of variables just to check types of right-side of expression, so disable this:7/* eslint-disable @typescript-eslint/no-unused-vars */89// This conflicts with esline rule saying no space!10/* eslint-disable @typescript-eslint/space-before-function-paren */1112// Defined stricter type, as the options as properties `[key: string]: any`8// makes the type checking very weak. 13// makes the type checking very weak.14// https://github.com/Microsoft/TypeScript/issues/25987#issuecomment-44122469015type KnownKeys<T> = {16 [K in keyof T]: string extends K ? never : number extends K ? never : K17} extends {[_ in keyof T]: infer U} ? ({} extends U ? never : U) : never;
14 unmodified lines
32const errorInstance = new commander.CommanderError(1, 'code', 'message');3334// Command properties35console.log(programWithOptions.someOption);36// eslint-disable-next-line dot-notation37console.log(programWithOptions['someOption']);38const theArgs = program.args;39const theCommands: commander.Command[] = program.commands;40
11 unmodified lines
52// addCommand53const addCommandThis: commander.Command = program.addCommand(new commander.Command('abc'));5455// arguments50const argumentsThis: commander.Command = program.arguments('<cmd> [env]')56const argumentsThis: commander.Command = program.arguments('<cmd> [env]');5758// exitOverride59const exitThis1: commander.Command = program.exitOverride();54const exitThis2: commander.Command = program.exitOverride((err):never => {60const exitThis2: commander.Command = program.exitOverride((err): never => {61 return process.exit(err.exitCode);62});57const exitThis3: commander.Command = program.exitOverride((err):void => {63const exitThis3: commander.Command = program.exitOverride((err): void => {64 if (err.code !== 'commander.executeSubCommandAsync') {65 throw err;66 } else {67 // Async callback from spawn events, not useful to throw.68 }69});7071// action66const actionThis1: commander.Command = program.action(() => { });67const actionThis2: commander.Command = program.action(async () => { });72const actionThis1: commander.Command = program.action(() => {73 // do nothing.74});75const actionThis2: commander.Command = program.action(async() => {76 // do nothing.77});7879// option80const optionThis1: commander.Command = program.option('-a,--alpha');71const optionThis2: commander.Command = program.option('-p, --peppers', 'Add peppers')72const optionThis3: commander.Command = program.option('-s, --string [value]', 'default string', 'value')81const optionThis2: commander.Command = program.option('-p, --peppers', 'Add peppers');82const optionThis3: commander.Command = program.option('-s, --string [value]', 'default string', 'value');83const optionThis4: commander.Command = program.option('-b, --boolean', 'default boolean', false);8485// example coercion functions from README8677function range(val: string) {87function range(val: string): Number[] {88 return val.split('..').map(Number);89}9081function myParseInt(value: string, dummyPrevious: number) {91function myParseInt(value: string, dummyPrevious: number): number {92 return parseInt(value);93}9485function increaseVerbosity(dummyValue: string, previous: number) {95function increaseVerbosity(dummyValue: string, previous: number): number {96 return previous + 1;97}9889function collect(value: string, previous: string[]) {99function collect(value: string, previous: string[]): string[] {100 return previous.concat([value]);101}10293function commaSeparatedList(value: string, dummyPrevious: string[]) {103function commaSeparatedList(value: string, dummyPrevious: string[]): string[] {104 return value.split(',');105}10697const optionThis5: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat)98const optionThis6: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat, 3.2)99const optionThis7: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt)100const optionThis8: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt, 5)101const optionThis9: commander.Command = program.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)102const optionThis10: commander.Command = program.option('-c, --collect <value>', 'repeatable value', collect, [])107const optionThis5: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat);108const optionThis6: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat, 3.2);109const optionThis7: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt);110const optionThis8: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt, 5);111const optionThis9: commander.Command = program.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);112const optionThis10: commander.Command = program.option('-c, --collect <value>', 'repeatable value', collect, []);113const optionThis11: commander.Command = program.option('-l, --list <items>', 'comma separated list', commaSeparatedList);114115// requiredOption, same tests as option116const requiredOptionThis1: commander.Command = program.requiredOption('-a,--alpha');107const requiredOptionThis2: commander.Command = program.requiredOption('-p, --peppers', 'Add peppers')108const requiredOptionThis3: commander.Command = program.requiredOption('-s, --string [value]', 'default string', 'value')117const requiredOptionThis2: commander.Command = program.requiredOption('-p, --peppers', 'Add peppers');118const requiredOptionThis3: commander.Command = program.requiredOption('-s, --string [value]', 'default string', 'value');119const requiredOptionThis4: commander.Command = program.requiredOption('-b, --boolean', 'default boolean', false);120111const requiredOptionThis5: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat)112const requiredOptionThis6: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat, 3.2)113const requiredOptionThis7: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt)114const requiredOptionThis8: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt, 5)115const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)116const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, [])121const requiredOptionThis5: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat);122const requiredOptionThis6: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat, 3.2);123const requiredOptionThis7: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt);124const requiredOptionThis8: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt, 5);125const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);126const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, []);127const requiredOptionThis11: commander.Command = program.requiredOption('-l, --list <items>', 'comma separated list', commaSeparatedList);128129// storeOptionsAsProperties130const storeOptionsAsPropertiesThis1: commander.Command = program.storeOptionsAsProperties();
11 unmodified lines
142const parseThis1: commander.Command = program.parse();143const parseThis2: commander.Command = program.parse(process.argv);144const parseThis3: commander.Command = program.parse(['node', 'script.js'], { from: 'node' });145const parseThis4: commander.Command = program.parse(['node', 'script.js'], { from: 'electron' });136const parseThis5: commander.Command = program.parse(['--option'], { from: "user" });146const parseThis5: commander.Command = program.parse(['--option'], { from: 'user' });147148// parseAsync, same tests as parse149const parseAsyncThis1: Promise<commander.Command> = program.parseAsync();150const parseAsyncThis2: Promise<commander.Command> = program.parseAsync(process.argv);151const parseAsyncThis3: Promise<commander.Command> = program.parseAsync(['node', 'script.js'], { from: 'node' });152const parseAsyncThis4: Promise<commander.Command> = program.parseAsync(['node', 'script.js'], { from: 'electron' });143const parseAsyncThis5: Promise<commander.Command> = program.parseAsync(['--option'], { from: "user" });153const parseAsyncThis5: Promise<commander.Command> = program.parseAsync(['--option'], { from: 'user' });154155// parseOptions (and ParseOptionsResult)156const { operands, unknown } = program.parseOptions(['node', 'script.js', 'hello']);157158// opts159const opts = program.opts();160const optsVal1 = opts.foo;161// eslint-disable-next-line dot-notation162const opstVale2 = opts['bar'];163164// description154const descriptionThis: commander.Command = program.description("my description");165const descriptionThis: commander.Command = program.description('my description');166const descriptionValue: string = program.description();167168// alias158const aliasThis: commander.Command = program.alias("my alias");169const aliasThis: commander.Command = program.alias('my alias');170const aliasValue: string = program.alias();171172// usage162const usageThis: commander.Command = program.usage("my usage");173const usageThis: commander.Command = program.usage('my usage');174const usageValue: string = program.usage();175176// name166const nameThis: commander.Command = program.name("my-name");177const nameThis: commander.Command = program.name('my-name');178const nameValue: string = program.name();179180// outputHelp181program.outputHelp();171program.outputHelp((str: string) => { return str });182program.outputHelp((str: string) => { return str; });183184// help185program.help();175program.help((str: string) => { return str });186program.help((str: string) => { return str; });187177188// helpInformation189const helpInformnationValue: string = program.helpInformation();190191// helpOption
1 unmodified line
193const helpOptionThis2: commander.Command = program.helpOption('-h,--help', 'custom description');194const helpOptionThis3: commander.Command = program.helpOption(undefined, 'custom description');195196// on187const onThis: commander.Command = program.on('--help', () => { })197const onThis: commander.Command = program.on('--help', () => {198 // do nothing.199});200201// createCommand202203const createInstance1: commander.Command = program.createCommand();204const createInstance2: commander.Command = program.createCommand('name');205206class MyCommand extends commander.Command {195 createCommand(name?: string) {207 createCommand(name?: string): MyCommand {208 return new MyCommand(name);209 }210198 myFunction() {}211 myFunction(): void {212 // do nothing.213 }214}215const myProgram = new MyCommand();216myProgram.myFunction();217const mySub = myProgram.command('sub');
1 unmodified line
modified:typings/index.d.ts+43 added-43 removed
7 unmodified lines
8 exitCode: number;9 message: string;10 nestedError?: string;11 }12 type CommanderErrorConstructor = { new (exitCode: number, code: string, message: string): CommanderError };12 type CommanderErrorConstructor = new (exitCode: number, code: string, message: string) => CommanderError;1314 interface Option {15 flags: string;16 required: boolean; // A value must be supplied when the option is specified.
3 unmodified lines
20 short?: string;21 long: string;22 description: string;23 }24 type OptionConstructor = { new (flags: string, description?: string): Option };24 type OptionConstructor = new (flags: string, description?: string) => Option;2526 interface ParseOptions {27 from: 'node' | 'electron' | 'user';28 }
5 unmodified lines
3435 commands: Command[];3637 /**38 * Set the program version to `str`. 38 * Set the program version to `str`.39 *40 * This method auto-registers the "-V, --version" flag41 * which will print the version number when passed.42 * 42 *43 * You can optionally supply the flags and description to override the defaults.44 */45 version(str: string, flags?: string, description?: string): this;4647 /**48 * Define a command, implemented using an action handler.49 * 49 *50 * @remarks51 * The command description is supplied using `.description`, not as a parameter to `.command`.52 * 52 *53 * @example54 * ```ts55 * program56 * .command('clone <source> [destination]')
1 unmodified line
58 * .action((source, destination) => {59 * console.log('clone command called');60 * });61 * ```62 * 62 *63 * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`64 * @param opts - configuration options65 * @returns new command66 */67 command(nameAndArgs: string, opts?: CommandOptions): ReturnType<this['createCommand']>;68 /**69 * Define a command, implemented in a separate executable file.70 * 70 *71 * @remarks72 * The command description is supplied as the second parameter to `.command`.73 * 73 *74 * @example75 * ```ts76 * program77 * .command('start <service>', 'start named service')78 * .command('stop [service]', 'stop named serice, or all if no name supplied');79 * ```80 * 80 *81 * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`82 * @param description - description of executable command83 * @param opts - configuration options84 * @returns top level command for chaining more command definitions
6 unmodified lines
91 * See .command() for creating an attached subcommand, which uses this routine to92 * create the command. You can override createCommand to customise subcommands.93 */94 createCommand(name?: string): Command;95 9596 /**97 * Add a prepared subcommand.98 * 98 *99 * See .command() for creating an attached subcommand which inherits settings from its parent.100 * 100 *101 * @returns parent command for chaining102 */103 addCommand(cmd: Command): this;104
79 unmodified lines
184 requiredOption(flags: string, description?: string, defaultValue?: string | boolean): this;185 requiredOption(flags: string, description: string, regexp: RegExp, defaultValue?: string | boolean): this;186 requiredOption<T>(flags: string, description: string, fn: (value: string, previous: T) => T, defaultValue?: T): this;187188188 /**189 * Whether to store option values as properties on command object,190 * or store separately (specify false). In both cases the option values can be accessed using .opts().191 *
3 unmodified lines
195196 /**197 * Whether to pass command to action handler,198 * or just the options (specify false).200 * 199 *200 * @return Command for chaining201 */202 passCommandToAction(value?: boolean): this;203
6 unmodified lines
210 allowUnknownOption(arg?: boolean): this;211212 /**213 * Parse `argv`, setting options and invoking commands when defined.215 * 214 *215 * The default expectation is that the arguments are from node and have the application as argv[0]216 * and the script being run in argv[1], with user parameters after that.217 *218 * Examples:219 *220 * program.parse(process.argv);221 * program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions222 * program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]224 * 223 *224 * @returns Command for chaining225 */226 parse(argv?: string[], options?: ParseOptions): this;227228 /**229 * Parse `argv`, setting options and invoking commands when defined.231 * 230 *231 * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.233 * 232 *233 * The default expectation is that the arguments are from node and have the application as argv[0]234 * and the script being run in argv[1], with user parameters after that.235 *236 * Examples:
25 unmodified lines
262 opts(): { [key: string]: any };263264 /**265 * Set the description.267 * 266 *267 * @returns Command for chaining268 */269 description(str: string, argsDescription?: {[argName: string]: string}): this;270 /**
2 unmodified lines
273 description(): string;274275 /**276 * Set an alias for the command.278 * 277 *278 * @returns Command for chaining279 */280 alias(alias: string): this;281 /**
2 unmodified lines
284 alias(): string;285286 /**287 * Set the command usage.289 * 288 *289 * @returns Command for chaining290 */291 usage(str: string): this;292 /**
2 unmodified lines
295 usage(): string;296297 /**298 * Set the name of the command.300 * 299 *300 * @returns Command for chaining301 */302 name(str: string): this;303 /**
12 unmodified lines
316 /**317 * Return command help documentation.318 */319 helpInformation(): string;321 320321 /**322 * You can pass in flags and a description to override the help323 * flags and help description for your command.324 */325 helpOption(flags?: string, description?: string): this;326328 /** 327 /**328 * Output help information and exit.329 */330 help(cb?: (str: string) => string): never;331332 /**333 * Add a listener (callback) for when events occur. (Implemented using EventEmitter.)335 * 334 *335 * @example336 * program337 * .on('--help', () -> {338 * console.log('See web site for more information.');339 * });340 */341 on(event: string | symbol, listener: (...args: any[]) => void): this;342 }344 type CommandConstructor = { new (name?: string): Command };343 type CommandConstructor = new (name?: string) => Command;344345 interface CommandOptions {346 noHelp?: boolean;347 isDefault?: boolean;348 executableFile?: string;349 }350347 interface CommandOptions {348 noHelp?: boolean;349 isDefault?: boolean;350 executableFile?: string;351 }351 interface ParseOptionsResult {352 operands: string[];353 unknown: string[];354 }355353 interface ParseOptionsResult {354 operands: string[];355 unknown: string[];356 }356 interface CommanderStatic extends Command {357 program: Command;358 Command: CommandConstructor;359 Option: OptionConstructor;360 CommanderError: CommanderErrorConstructor;361 }362358 interface CommanderStatic extends Command {359 program: Command;360 Command: CommandConstructor;361 Option: OptionConstructor;362 CommanderError:CommanderErrorConstructor;363 }364363}364365// Declaring namespace AND global366// eslint-disable-next-line no-redeclare367declare const commander: commander.CommanderStatic;368export = commander;