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
Provenance
- Commit
e960c90- Tree
9ea80b8- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
ebc8b41
2 files changed+98 added−83 removed
modified:typings/commander-tests.ts+55 added-40 removed
1 import * as commander from './index';2-import { Script } from 'vm';2 3 // Test Commander usage with TypeScript.4 // This is NOT a usable program, just used to test for compile errors!5 6+// 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 */8+9+// This conflicts with esline rule saying no space!10+/* eslint-disable @typescript-eslint/space-before-function-paren */11+12 // 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-44122469015 type 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 lines32 const errorInstance = new commander.CommanderError(1, 'code', 'message');33 34 // Command properties35 console.log(programWithOptions.someOption);36+// eslint-disable-next-line dot-notation37 console.log(programWithOptions['someOption']);38 const theArgs = program.args;39 const theCommands: commander.Command[] = program.commands;40 11 unmodified lines52 // addCommand53 const addCommandThis: commander.Command = program.addCommand(new commander.Command('abc'));54 55 // arguments50-const argumentsThis: commander.Command = program.arguments('<cmd> [env]')56+const argumentsThis: commander.Command = program.arguments('<cmd> [env]');57 58 // exitOverride59 const exitThis1: commander.Command = program.exitOverride();54-const exitThis2: commander.Command = program.exitOverride((err):never => {60+const exitThis2: commander.Command = program.exitOverride((err): never => {61 return process.exit(err.exitCode);62 });57-const exitThis3: commander.Command = program.exitOverride((err):void => {63+const 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 });70 71 // action66-const actionThis1: commander.Command = program.action(() => { });67-const actionThis2: commander.Command = program.action(async () => { });72+const actionThis1: commander.Command = program.action(() => {73+ // do nothing.74+});75+const actionThis2: commander.Command = program.action(async() => {76+ // do nothing.77+});78 79 // option80 const optionThis1: commander.Command = program.option('-a,--alpha');71-const optionThis2: commander.Command = program.option('-p, --peppers', 'Add peppers')72-const optionThis3: commander.Command = program.option('-s, --string [value]', 'default string', 'value')81+const optionThis2: commander.Command = program.option('-p, --peppers', 'Add peppers');82+const optionThis3: commander.Command = program.option('-s, --string [value]', 'default string', 'value');83 const optionThis4: commander.Command = program.option('-b, --boolean', 'default boolean', false);84 85 // example coercion functions from README86 77-function range(val: string) {87+function range(val: string): Number[] {88 return val.split('..').map(Number);89 }90 81-function myParseInt(value: string, dummyPrevious: number) {91+function myParseInt(value: string, dummyPrevious: number): number {92 return parseInt(value);93 }94 85-function increaseVerbosity(dummyValue: string, previous: number) {95+function increaseVerbosity(dummyValue: string, previous: number): number {96 return previous + 1;97 }98 89-function collect(value: string, previous: string[]) {99+function collect(value: string, previous: string[]): string[] {100 return previous.concat([value]);101 }102 93-function commaSeparatedList(value: string, dummyPrevious: string[]) {103+function commaSeparatedList(value: string, dummyPrevious: string[]): string[] {104 return value.split(',');105 }106 97-const optionThis5: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat)98-const optionThis6: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat, 3.2)99-const optionThis7: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt)100-const optionThis8: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt, 5)101-const optionThis9: commander.Command = program.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)102-const optionThis10: commander.Command = program.option('-c, --collect <value>', 'repeatable value', collect, [])107+const optionThis5: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat);108+const optionThis6: commander.Command = program.option('-f, --float <number>', 'float argument', parseFloat, 3.2);109+const optionThis7: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt);110+const optionThis8: commander.Command = program.option('-i, --integer <number>', 'integer argument', myParseInt, 5);111+const optionThis9: commander.Command = program.option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);112+const optionThis10: commander.Command = program.option('-c, --collect <value>', 'repeatable value', collect, []);113 const optionThis11: commander.Command = program.option('-l, --list <items>', 'comma separated list', commaSeparatedList);114 115 // requiredOption, same tests as option116 const requiredOptionThis1: commander.Command = program.requiredOption('-a,--alpha');107-const requiredOptionThis2: commander.Command = program.requiredOption('-p, --peppers', 'Add peppers')108-const requiredOptionThis3: commander.Command = program.requiredOption('-s, --string [value]', 'default string', 'value')117+const requiredOptionThis2: commander.Command = program.requiredOption('-p, --peppers', 'Add peppers');118+const requiredOptionThis3: commander.Command = program.requiredOption('-s, --string [value]', 'default string', 'value');119 const requiredOptionThis4: commander.Command = program.requiredOption('-b, --boolean', 'default boolean', false);120 111-const requiredOptionThis5: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat)112-const requiredOptionThis6: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat, 3.2)113-const requiredOptionThis7: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt)114-const requiredOptionThis8: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt, 5)115-const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)116-const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, [])121+const requiredOptionThis5: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat);122+const requiredOptionThis6: commander.Command = program.requiredOption('-f, --float <number>', 'float argument', parseFloat, 3.2);123+const requiredOptionThis7: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt);124+const requiredOptionThis8: commander.Command = program.requiredOption('-i, --integer <number>', 'integer argument', myParseInt, 5);125+const requiredOptionThis9: commander.Command = program.requiredOption('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0);126+const requiredOptionThis10: commander.Command = program.requiredOption('-c, --collect <value>', 'repeatable value', collect, []);127 const requiredOptionThis11: commander.Command = program.requiredOption('-l, --list <items>', 'comma separated list', commaSeparatedList);128 129 // storeOptionsAsProperties130 const storeOptionsAsPropertiesThis1: commander.Command = program.storeOptionsAsProperties();11 unmodified lines142 const parseThis1: commander.Command = program.parse();143 const parseThis2: commander.Command = program.parse(process.argv);144 const parseThis3: commander.Command = program.parse(['node', 'script.js'], { from: 'node' });145 const parseThis4: commander.Command = program.parse(['node', 'script.js'], { from: 'electron' });136-const parseThis5: commander.Command = program.parse(['--option'], { from: "user" });146+const parseThis5: commander.Command = program.parse(['--option'], { from: 'user' });147 148 // parseAsync, same tests as parse149 const parseAsyncThis1: Promise<commander.Command> = program.parseAsync();150 const parseAsyncThis2: Promise<commander.Command> = program.parseAsync(process.argv);151 const parseAsyncThis3: Promise<commander.Command> = program.parseAsync(['node', 'script.js'], { from: 'node' });152 const parseAsyncThis4: Promise<commander.Command> = program.parseAsync(['node', 'script.js'], { from: 'electron' });143-const parseAsyncThis5: Promise<commander.Command> = program.parseAsync(['--option'], { from: "user" });153+const parseAsyncThis5: Promise<commander.Command> = program.parseAsync(['--option'], { from: 'user' });154 155 // parseOptions (and ParseOptionsResult)156 const { operands, unknown } = program.parseOptions(['node', 'script.js', 'hello']);157 158 // opts159 const opts = program.opts();160 const optsVal1 = opts.foo;161+// eslint-disable-next-line dot-notation162 const opstVale2 = opts['bar'];163 164 // description154-const descriptionThis: commander.Command = program.description("my description");165+const descriptionThis: commander.Command = program.description('my description');166 const descriptionValue: string = program.description();167 168 // alias158-const aliasThis: commander.Command = program.alias("my alias");169+const aliasThis: commander.Command = program.alias('my alias');170 const aliasValue: string = program.alias();171 172 // usage162-const usageThis: commander.Command = program.usage("my usage");173+const usageThis: commander.Command = program.usage('my usage');174 const usageValue: string = program.usage();175 176 // name166-const nameThis: commander.Command = program.name("my-name");177+const nameThis: commander.Command = program.name('my-name');178 const nameValue: string = program.name();179 180 // outputHelp181 program.outputHelp();171-program.outputHelp((str: string) => { return str });182+program.outputHelp((str: string) => { return str; });183 184 // help185 program.help();175-program.help((str: string) => { return str });186+program.help((str: string) => { return str; });187 177-188 // helpInformation189 const helpInformnationValue: string = program.helpInformation();190 191 // helpOption1 unmodified line193 const helpOptionThis2: commander.Command = program.helpOption('-h,--help', 'custom description');194 const helpOptionThis3: commander.Command = program.helpOption(undefined, 'custom description');195 196 // on187-const onThis: commander.Command = program.on('--help', () => { })197+const onThis: commander.Command = program.on('--help', () => {198+ // do nothing.199+});200 201 // createCommand202 203 const createInstance1: commander.Command = program.createCommand();204 const createInstance2: commander.Command = program.createCommand('name');205 206 class MyCommand extends commander.Command {195- createCommand(name?: string) {207+ createCommand(name?: string): MyCommand {208 return new MyCommand(name);209 }210+198- myFunction() {}211+ myFunction(): void {212+ // do nothing.213+ }214 }215 const myProgram = new MyCommand();216 myProgram.myFunction();217 const mySub = myProgram.command('sub');1 unmodified linemodified:typings/index.d.ts+43 added-43 removed
7 unmodified lines8 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;13 14 interface Option {15 flags: string;16 required: boolean; // A value must be supplied when the option is specified.3 unmodified lines20 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;25 26 interface ParseOptions {27 from: 'node' | 'electron' | 'user';28 }5 unmodified lines34 35 commands: Command[];36 37 /**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;46 47 /**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 line58 * .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 definitions6 unmodified lines91 * 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- 95+96 /**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 lines184 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;187 188-188 /**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 lines195 196 /**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 lines210 allowUnknownOption(arg?: boolean): this;211 212 /**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;227 228 /**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 lines262 opts(): { [key: string]: any };263 264 /**265 * Set the description.267- * 266+ *267 * @returns Command for chaining268 */269 description(str: string, argsDescription?: {[argName: string]: string}): this;270 /**2 unmodified lines273 description(): string;274 275 /**276 * Set an alias for the command.278- * 277+ *278 * @returns Command for chaining279 */280 alias(alias: string): this;281 /**2 unmodified lines284 alias(): string;285 286 /**287 * Set the command usage.289- * 288+ *289 * @returns Command for chaining290 */291 usage(str: string): this;292 /**2 unmodified lines295 usage(): string;296 297 /**298 * Set the name of the command.300- * 299+ *300 * @returns Command for chaining301 */302 name(str: string): this;303 /**12 unmodified lines316 /**317 * Return command help documentation.318 */319 helpInformation(): string;321- 320+321 /**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;326 328- /** 327+ /**328 * Output help information and exit.329 */330 help(cb?: (str: string) => string): never;331 332 /**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;344 345+ interface CommandOptions {346+ noHelp?: boolean;347+ isDefault?: boolean;348+ executableFile?: string;349+ }350 347- interface CommandOptions {348- noHelp?: boolean;349- isDefault?: boolean;350- executableFile?: string;351- }351+ interface ParseOptionsResult {352+ operands: string[];353+ unknown: string[];354+ }355 353- 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+ }362 358- interface CommanderStatic extends Command {359- program: Command;360- Command: CommandConstructor;361- Option: OptionConstructor;362- CommanderError:CommanderErrorConstructor;363- }364-363 }364 365+// Declaring namespace AND global366+// eslint-disable-next-line no-redeclare367 declare const commander: commander.CommanderStatic;368 export = commander;