Skip to content

Commit

Permalink
removes null from booleanDefault type
Browse files Browse the repository at this point in the history
- this was introduced in sindresorhus#77, likely due to compatibility with minimist
- `booleanDefault: null` causes minimist-options to error
- even in 2019, this was a confusing type:
  sindresorhus#116 (comment)
  • Loading branch information
tommy-mitchell committed Jan 26, 2024
1 parent 96d6f6b commit 21b5c91
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion readme.md
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
2 changes: 1 addition & 1 deletion source/parser.ts
Expand Up @@ -29,7 +29,7 @@ const buildParserFlags = ({flags, booleanDefault}: ParsedOptions): ParserFlags =
? (flag.type ? `${flag.type}-array` as const : 'array' as const)
: flag.type,
default: shouldUseBooleanDefault
? (flag.isMultiple ? [booleanDefault] : booleanDefault) // TODO: can this be null?
? (flag.isMultiple ? [booleanDefault] : booleanDefault)
: (flag.default ?? (flag.isMultiple ? [] : undefined)),
alias: aliases,
};
Expand Down
3 changes: 1 addition & 2 deletions source/types.ts
Expand Up @@ -296,8 +296,7 @@ export type Options<Flags extends AnyFlags = AnyFlags> = {
//}
```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
readonly booleanDefault?: boolean | null | undefined;
readonly booleanDefault?: boolean;

// TODO: Remove this in meow 14.
/**
Expand Down
44 changes: 21 additions & 23 deletions test/test.ts
@@ -1,13 +1,12 @@
import path from 'node:path';
import process from 'node:process';
import test from 'ava';
import indentString from 'indent-string';
import {execa} from 'execa';
import {readPackage} from 'read-pkg';
import meow from '../source/index.js';
import {spawnFixture, __dirname} from './_utils.js';
import {spawnFixture, __dirname, _meowThrows} from './_utils.js';

const importMeta = import.meta;
const meowThrows = _meowThrows(importMeta);

const NODE_MAJOR_VERSION = process.versions.node.split('.').at(0); // eslint-disable-line @typescript-eslint/naming-convention

test('return object', t => {
Expand Down Expand Up @@ -40,27 +39,11 @@ test('return object', t => {
});
});

test('spawn cli and show version', async t => {
const pkg = await readPackage();
const {stdout} = await spawnFixture(['--version']);
t.is(stdout, pkg.version);
});

test('spawn cli and disabled autoVersion and autoHelp', async t => {
const {stdout} = await spawnFixture(['--version', '--help']);
t.is(stdout, 'version\nhelp\nmeow\ncamelCaseOption');
});

test('spawn cli and disabled autoVersion', async t => {
const {stdout} = await spawnFixture(['--version', '--no-auto-version']);
t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption');
});

test('spawn cli and not show version', async t => {
const {stdout} = await spawnFixture(['--version=beta']);
t.is(stdout, 'version\nmeow\ncamelCaseOption');
});

test('spawn cli and test input', async t => {
const {stdout} = await spawnFixture(['-u', 'cat']);
t.is(stdout, 'unicorn\nmeow\ncamelCaseOption');
Expand Down Expand Up @@ -88,9 +71,7 @@ test('setting pkg.bin should work', t => {
t.like(cli, {
pkg: {
name: 'browser-sync',
bin: {
'browser-sync': './bin/browser-sync.js',
},
bin: './bin/browser-sync.js',
},
version: undefined,
});
Expand Down Expand Up @@ -162,6 +143,23 @@ test('booleanDefault: undefined, filter out unset boolean args', t => {
});
});

test('booleanDefault: null throws', meowThrows, {
argv: ['--foo'],
booleanDefault: null,
flags: {
foo: {
type: 'boolean',
},
bar: {
type: 'boolean',
},
baz: {
type: 'boolean',
default: false,
},
},
}, {message: 'Expected "foo" default value to be of type "boolean", got "null"'});

test('boolean args are false by default', t => {
const cli = meow({
importMeta,
Expand Down

0 comments on commit 21b5c91

Please sign in to comment.