Skip to content

Commit

Permalink
Remove null from booleanDefault type (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Feb 29, 2024
1 parent 7b9e739 commit a85c6e4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would

##### booleanDefault

Type: `boolean | null | undefined`\
Type: `boolean | undefined`\
Default: `false`

Value of `boolean` flags not defined in `argv`.
Expand Down
3 changes: 1 addition & 2 deletions source/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ export type Options<Flags extends AnyFlags> = {
//}
```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
readonly booleanDefault?: boolean | null | undefined;
readonly booleanDefault?: boolean | undefined;

// TODO: Remove this in meow 14.
/**
Expand Down
6 changes: 1 addition & 5 deletions source/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ const buildParserFlags = ({flags, booleanDefault}) => {
delete flag.shortFlag;
}

if (
booleanDefault !== undefined
&& flag.type === 'boolean'
&& !Object.hasOwn(flag, 'default')
) {
if (booleanDefault !== undefined && flag.type === 'boolean' && !Object.hasOwn(flag, 'default')) {
flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault;
}

Expand Down
1 change: 0 additions & 1 deletion test-d/build.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ expectType<Result<never>>(meow({importMeta, pkg: {foo: 'bar'}}));
expectType<Result<never>>(meow({importMeta, argv: ['foo', 'bar']}));
expectType<Result<never>>(meow({importMeta, inferType: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: null}));
expectType<Result<never>>(meow({importMeta, booleanDefault: undefined}));
expectType<Result<never>>(meow({importMeta}));

Expand Down
1 change: 0 additions & 1 deletion test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ expectType<Result<never>>(meow({importMeta, pkg: {foo: 'bar'}}));
expectType<Result<never>>(meow({importMeta, argv: ['foo', 'bar']}));
expectType<Result<never>>(meow({importMeta, inferType: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: true}));
expectType<Result<never>>(meow({importMeta, booleanDefault: null}));
expectType<Result<never>>(meow({importMeta, booleanDefault: undefined}));
expectType<Result<never>>(meow({importMeta, hardRejection: false}));

Expand Down
10 changes: 8 additions & 2 deletions test/flags/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import test from 'ava';
import meow from '../../source/index.js';

export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error}) => {
export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error, ...meowOptions}) => {
const assertions = await t.try(async tt => {
const arguments_ = args?.split(' ') ?? [];
const meowOptions = {importMeta, argv: arguments_, flags};

meowOptions = {
...meowOptions,
importMeta,
argv: arguments_,
flags,
};

tt.log('arguments:', arguments_);

Expand Down
85 changes: 44 additions & 41 deletions test/flags/boolean-default.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
import test from 'ava';
import meow from '../../source/index.js';
import {_verifyFlags} from './_utils.js';

const importMeta = import.meta;
const verifyFlags = _verifyFlags(import.meta);

test('undefined - filter out unset boolean args', t => {
const cli = meow({
importMeta,
argv: ['--foo'],
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean',
},
bar: {
type: 'boolean',
},
baz: {
type: 'boolean',
default: false,
},
test('undefined - filter out unset boolean args', verifyFlags, {
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean',
},
});

t.like(cli.flags, {
bar: {
type: 'boolean',
},
baz: {
type: 'boolean',
default: false,
},
},
args: '--foo',
expected: {
foo: true,
bar: undefined,
baz: false,
});
},
});

test('boolean args are false by default', t => {
const cli = meow({
importMeta,
argv: ['--foo'],
flags: {
foo: {
type: 'boolean',
},
bar: {
type: 'boolean',
default: true,
},
baz: {
type: 'boolean',
},
test('boolean args are false by default', verifyFlags, {
flags: {
foo: {
type: 'boolean',
},
});

t.like(cli.flags, {
bar: {
type: 'boolean',
default: true,
},
baz: {
type: 'boolean',
},
},
args: '--foo',
expected: {
foo: true,
bar: true,
baz: false,
});
},
});

test('throws if default is null', verifyFlags, {
booleanDefault: null,
flags: {
foo: {
type: 'boolean',
},
},
args: '--foo',
error: 'Expected "foo" default value to be of type "boolean", got "null"',
});

0 comments on commit a85c6e4

Please sign in to comment.