From f9f840d2893c461e1b150dfb8b279e7ddf97f28b Mon Sep 17 00:00:00 2001 From: Brian Chen Date: Fri, 15 Sep 2023 07:51:44 +1000 Subject: [PATCH] [yargs] Fix example and middleware types (#4511) * improve example * fix middleware return type --- .../flow_v0.201.x-/yargs_v17.x.x.js | 11 +++++- definitions/npm/yargs_v17.x.x/test_yargs.js | 39 ++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/definitions/npm/yargs_v17.x.x/flow_v0.201.x-/yargs_v17.x.x.js b/definitions/npm/yargs_v17.x.x/flow_v0.201.x-/yargs_v17.x.x.js index dc4963a1c9..9150174666 100644 --- a/definitions/npm/yargs_v17.x.x/flow_v0.201.x-/yargs_v17.x.x.js +++ b/definitions/npm/yargs_v17.x.x/flow_v0.201.x-/yargs_v17.x.x.js @@ -64,8 +64,12 @@ declare module "yargs" { | ModuleObjectDescribe | ModuleObjectDescription; - declare type MiddleWareCallback = - | (argv: Argv, yargsInstance?: Yargs) => void + declare type MiddleWareCallback = (argv: Argv, yargsInstance?: Yargs) => ( + void + | Promise + | { ... } + | Promise<{ ... }> + ) | (argv: Argv, yargsInstance?: Yargs) => Promise; declare type Middleware = MiddleWareCallback | Array; @@ -186,6 +190,7 @@ declare module "yargs" { epilogue(text: string): this; example(cmd: string, desc?: string): this; + example(examples: Array<[string, string | void]>): this; exitProcess(enable: boolean): this; @@ -334,6 +339,8 @@ declare module "yargs" { wrap(columns: number | null): this; } + declare type YargsType = Yargs; + declare module.exports: Yargs; } diff --git a/definitions/npm/yargs_v17.x.x/test_yargs.js b/definitions/npm/yargs_v17.x.x/test_yargs.js index b4e82b3283..9f8772ab1a 100644 --- a/definitions/npm/yargs_v17.x.x/test_yargs.js +++ b/definitions/npm/yargs_v17.x.x/test_yargs.js @@ -1,7 +1,7 @@ // @flow import { describe, it, test } from 'flow-typed-test'; -import yargs from 'yargs'; +import yargs, { type YargsType, type Argv } from 'yargs'; const { hideBin } = require('yargs/helpers'); describe('command()', () => { @@ -37,6 +37,43 @@ describe('command()', () => { it('example', () => { yargs.example('fetch', 'fetch [...files]'); + yargs.example('fetch'); + + yargs.example([ + ['fetch', 'fetch [...files]'], + ['fetch', 'fetch [...files]'], + ['fetch', undefined], + ]); + + // $FlowExpectedError[incompatible-call] + yargs.example(); + // $FlowExpectedError[incompatible-call] + yargs.example(1); + // $FlowExpectedError[incompatible-call] + yargs.example('fetch', 1); + // $FlowExpectedError[incompatible-call] + yargs.example([ + [], + ]); + // $FlowExpectedError[incompatible-call] + yargs.example([1]); + }); + + it('middleware', () => { + yargs.middleware(() => {}); + yargs.middleware(async () => {}); + yargs.middleware(() => { return { runAll: true } }); + yargs.middleware(async () => { return { runAll: true }}); + + yargs.middleware((args, innerYargs) => { + (args: Argv); + (innerYargs: YargsType | void); + }); + + // $FlowExpectedError[incompatible-call] + yargs.middleware(); + // $FlowExpectedError[incompatible-call] + yargs.middleware(() => 1); }); });