Skip to content

Commit

Permalink
[yargs] Fix example and middleware types (flow-typed#4511)
Browse files Browse the repository at this point in the history
* improve example

* fix middleware return type
  • Loading branch information
Brianzchen committed Sep 14, 2023
1 parent 2769806 commit f9f840d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
11 changes: 9 additions & 2 deletions definitions/npm/yargs_v17.x.x/flow_v0.201.x-/yargs_v17.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
| { ... }
| Promise<{ ... }>
)
| (argv: Argv, yargsInstance?: Yargs) => Promise<void>;

declare type Middleware = MiddleWareCallback | Array<MiddleWareCallback>;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -334,6 +339,8 @@ declare module "yargs" {
wrap(columns: number | null): this;
}

declare type YargsType = Yargs;

declare module.exports: Yargs;
}

Expand Down
39 changes: 38 additions & 1 deletion definitions/npm/yargs_v17.x.x/test_yargs.js
Original file line number Diff line number Diff line change
@@ -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()', () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit f9f840d

Please sign in to comment.