Fix types for `stdio: [..., undefined]` (#952)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent ed052d4 · Browse files · 4 files·+46 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+46 added−4 removed
- tests:+6 added−0 removed
- other:+40 added−4 removed
Provenance
- Commit
6233874- Tree
4197ac4- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ed052d4
4 files changed+46 added−4 removed
modified:lib/stdio/option.js+4 added
30 unmodified lines3132333435363738394041422 unmodified lines30 unmodified lines
const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);
const addDefaultValue = (stdioOption, fdNumber) => { if (Array.isArray(stdioOption)) { return stdioOption.map(item => addDefaultValue(item, fdNumber)); }
if (stdioOption === null || stdioOption === undefined) { return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe'; }
2 unmodified linesmodified:test/stdio/forward.js+6 added
33 unmodified lines343536373839404142434445464733 unmodified lines};
test('stdio[*] undefined means "ignore"', testFd3Undefined, undefined, {});test('stdio[*] null means "ignore"', testFd3Undefined, null, {});test('stdio[*] [undefined] means "ignore"', testFd3Undefined, [undefined], {});test('stdio[*] [null] means "ignore"', testFd3Undefined, [null], {});test('stdio[*] undefined means "ignore", "lines: true"', testFd3Undefined, undefined, {lines: true});test('stdio[*] null means "ignore", "lines: true"', testFd3Undefined, null, {lines: true});test('stdio[*] [undefined] means "ignore", "lines: true"', testFd3Undefined, [undefined], {lines: true});test('stdio[*] [null] means "ignore", "lines: true"', testFd3Undefined, [null], {lines: true});test('stdio[*] undefined means "ignore", "encoding: hex"', testFd3Undefined, undefined, {encoding: 'hex'});test('stdio[*] null means "ignore", "encoding: hex"', testFd3Undefined, null, {encoding: 'hex'});test('stdio[*] [undefined] means "ignore", "encoding: hex"', testFd3Undefined, [undefined], {encoding: 'hex'});test('stdio[*] [null] means "ignore", "encoding: hex"', testFd3Undefined, [null], {encoding: 'hex'});modified:index.d.ts+10 added-4 removed
7 unmodified lines8 type Unless<Condition extends boolean, ThenValue, ElseValue = never> = Condition extends true ? ElseValue : ThenValue;9 10 type AndUnless<Condition extends boolean, ThenValue, ElseValue = unknown> = Condition extends true ? ElseValue : ThenValue;11 12+type IsMainFd<FdNumber extends string> = FdNumber extends keyof StreamOptionsNames ? true : false;13+14 // When the `stdin`/`stdout`/`stderr`/`stdio` option is set to one of those values, no stream is created13-type NoStreamStdioOption =15+type NoStreamStdioOption<FdNumber extends string> =16 | 'ignore'17 | 'inherit'18 | 'ipc'19 | number20 | Readable21 | Writable20- | [NoStreamStdioOption];22+ | Unless<IsMainFd<FdNumber>, undefined>23+ | readonly [NoStreamStdioOption<FdNumber>];24 25 type BaseStdioOption<26 IsSync extends boolean = boolean,27 IsArray extends boolean = boolean,172 unmodified lines200 // Whether `result.stdout|stderr|all` is `undefined`, excluding the `buffer` option201 type IgnoresStreamResult<202 FdNumber extends string,203 OptionsType extends CommonOptions = CommonOptions,201-> = IgnoresStdioResult<StreamOption<FdNumber, OptionsType>>;204+> = IgnoresStdioResult<FdNumber, StreamOption<FdNumber, OptionsType>>;205 206 // Whether `result.stdio[*]` is `undefined`204-type IgnoresStdioResult<StdioOptionType extends StdioOptionCommon> = StdioOptionType extends NoStreamStdioOption ? true : false;207+type IgnoresStdioResult<208+ FdNumber extends string,209+ StdioOptionType extends StdioOptionCommon,210+> = StdioOptionType extends NoStreamStdioOption<FdNumber> ? true : false;211 212 // Whether `result.stdout|stderr|all` is `undefined`213 type IgnoresStreamOutput<214 FdNumber extends string,1444 unmodified linesmodified:index.test-d.ts+26 added
711 unmodified lines712 expectType<string>(streamArrayStderrResult.stdout);713 expectType<undefined>(streamArrayStderrResult.stderr);714 expectType<string>(streamArrayStderrResult.all);715 716+ const undefinedStdoutResult = await execa('unicorns', {stdout: undefined, all: true});717+ expectType<string>(undefinedStdoutResult.stdout);718+ expectType<string>(undefinedStdoutResult.stderr);719+ expectType<string>(undefinedStdoutResult.all);720+721+ const undefinedArrayStdoutResult = await execa('unicorns', {stdout: [undefined] as const, all: true});722+ expectType<string>(undefinedArrayStdoutResult.stdout);723+ expectType<string>(undefinedArrayStdoutResult.stderr);724+ expectType<string>(undefinedArrayStdoutResult.all);725+726+ const undefinedStderrResult = await execa('unicorns', {stderr: undefined, all: true});727+ expectType<string>(undefinedStderrResult.stdout);728+ expectType<string>(undefinedStderrResult.stderr);729+ expectType<string>(undefinedStderrResult.all);730+731+ const undefinedArrayStderrResult = await execa('unicorns', {stderr: [undefined] as const, all: true});732+ expectType<string>(undefinedArrayStderrResult.stdout);733+ expectType<string>(undefinedArrayStderrResult.stderr);734+ expectType<string>(undefinedArrayStderrResult.all);735+736 const fd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'pipe']});737 expectType<string>(fd3Result.stdio[3]);738 739 const inputFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', ['pipe', new Readable()]]});16 unmodified lines756 757 const ignoreFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', 'ignore']});758 expectType<undefined>(ignoreFd3Result.stdio[3]);759 760+ const undefinedFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', undefined]});761+ expectType<undefined>(undefinedFd3Result.stdio[3]);762+763+ const undefinedArrayFd3Result = await execa('unicorns', {stdio: ['pipe', 'pipe', 'pipe', [undefined] as const]});764+ expectType<undefined>(undefinedArrayFd3Result.stdio[3]);765+766 const objectTransformLinesStdoutResult = await execa('unicorns', {lines: true, stdout: {transform: objectGenerator, final: objectFinal, objectMode: true}});767 expectType<unknown[]>(objectTransformLinesStdoutResult.stdout);768 expectType<[undefined, unknown[], string[]]>(objectTransformLinesStdoutResult.stdio);769 2928 unmodified lines