Skip to content
Myr

Validate against `yield undefined` in transforms (#756)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 14fca86 · Browse files · 3 files+33 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+33 added−25 removed

  • tests:+20 added−19 removed
  • other:+13 added−6 removed

3 files+33 added−25 removed

modified:docs/transform.md+1 added-1 removed
modified:lib/stdio/generator.js+12 added-5 removed
84 unmodified lines
85 ? validateObjectTransformReturn.bind(undefined, optionName)86 : validateStringTransformReturn.bind(undefined, optionName);8788const validateObjectTransformReturn = function * (optionName, chunk) {89 if (chunk === null) {90 throw new TypeError(`The \`${optionName}\` option's function must not return null.`);91 }9289 validateEmptyReturn(optionName, chunk);90 yield chunk;91};9293const validateStringTransformReturn = function * (optionName, chunk) {94 validateEmptyReturn(optionName, chunk);9596 if (typeof chunk !== 'string' && !isBinary(chunk)) {98 throw new TypeError(`The \`${optionName}\` option's function must return a string or an Uint8Array, not ${typeof chunk}.`);97 throw new TypeError(`The \`${optionName}\` option's function must yield a string or an Uint8Array, not ${typeof chunk}.`);98 }99100 yield chunk;101};102103const validateEmptyReturn = (optionName, chunk) => {104 if (chunk === null || chunk === undefined) {105 throw new TypeError(`The \`${optionName}\` option's function must not call \`yield ${chunk}\`.106Instead, \`yield\` should either be called with a value, or not be called at all. For example:107 if (condition) { yield value; }`);108 }109};110111// `childProcess.stdin|stdout|stderr|stdio` is directly mutated.112export const pipeGenerator = (spawned, {value, direction, index}) => {113 if (direction === 'output') {114 pipeStreams(spawned.stdio[index], value);
13 unmodified lines
modified:test/stdio/generator.js+20 added-19 removed
84 unmodified lines
8586// eslint-disable-next-line max-params87const testGeneratorReturn = async (t, index, generators, fixtureName, isNull) => {88 const childProcess = execa(fixtureName, [`${index}`], getStdio(index, generators));89 const message = isNull ? /not return null/ : /a string or an Uint8Array/;89 const message = isNull ? /not be called at all/ : /a string or an Uint8Array/;90 await t.throwsAsync(childProcess, {message});91};9293const inputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, false), serializeGenerator];94const lastInputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, true)];95const invalidOutputObjectGenerator = getOutputGenerator(foobarObject, false);96const inputNullGenerator = objectMode => [foobarUint8Array, getOutputGenerator(null, objectMode), serializeGenerator];97const outputNullGenerator = objectMode => getOutputGenerator(null, objectMode);93const lastInputGenerator = (input, objectMode) => [foobarUint8Array, getOutputGenerator(input, objectMode)];94const inputGenerator = (input, objectMode) => [...lastInputGenerator(input, objectMode), serializeGenerator];9599test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputObjectGenerators, 'stdin-fd.js', false);100test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputObjectGenerators, 'stdin-fd.js', false);101test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputObjectGenerators, 'stdin-fd.js', false);102test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputObjectGenerators, 'stdin-fd.js', false);103test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, invalidOutputObjectGenerator, 'noop-fd.js', false);104test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, invalidOutputObjectGenerator, 'noop-fd.js', false);105test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, invalidOutputObjectGenerator, 'noop-fd.js', false);106test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputNullGenerator(false), 'stdin-fd.js', false);107test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputNullGenerator(true), 'stdin-fd.js', true);108test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, outputNullGenerator(false), 'noop-fd.js', false);109test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, outputNullGenerator(true), 'noop-fd.js', true);96test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputGenerator(foobarObject, false), 'stdin-fd.js', false);97test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputGenerator(foobarObject, false), 'stdin-fd.js', false);98test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);99test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputGenerator(foobarObject, true), 'stdin-fd.js', false);100test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);101test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);102test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, getOutputGenerator(foobarObject, false), 'noop-fd.js', false);103test('Generators with result.stdin cannot return null if not in objectMode', testGeneratorReturn, 0, inputGenerator(null, false), 'stdin-fd.js', true);104test('Generators with result.stdin cannot return null if in objectMode', testGeneratorReturn, 0, inputGenerator(null, true), 'stdin-fd.js', true);105test('Generators with result.stdout cannot return null if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, false), 'noop-fd.js', true);106test('Generators with result.stdout cannot return null if in objectMode', testGeneratorReturn, 1, getOutputGenerator(null, true), 'noop-fd.js', true);107test('Generators with result.stdin cannot return undefined if not in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, false), 'stdin-fd.js', true);108test('Generators with result.stdin cannot return undefined if in objectMode', testGeneratorReturn, 0, inputGenerator(undefined, true), 'stdin-fd.js', true);109test('Generators with result.stdout cannot return undefined if not in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, false), 'noop-fd.js', true);110test('Generators with result.stdout cannot return undefined if in objectMode', testGeneratorReturn, 1, getOutputGenerator(undefined, true), 'noop-fd.js', true);111112const testGeneratorFinal = async (t, fixtureName) => {113 const {stdout} = await execa(fixtureName, {stdout: convertTransformToFinal(getOutputGenerator(foobarString), true)});114 t.is(stdout, foobarString);
2 unmodified lines
117test('Generators "final" can be used', testGeneratorFinal, 'noop.js');118test('Generators "final" is used even on empty streams', testGeneratorFinal, 'empty.js');119120test('Generators "final" return value is validated', async t => {120 const childProcess = execa('noop.js', {stdout: convertTransformToFinal(outputNullGenerator(true), true)});121 await t.throwsAsync(childProcess, {message: /not return null/});121 const childProcess = execa('noop.js', {stdout: convertTransformToFinal(getOutputGenerator(null, true), true)});122 await t.throwsAsync(childProcess, {message: /not be called at all/});123});124125// eslint-disable-next-line max-params126const testGeneratorOutput = async (t, index, reject, useShortcutProperty, objectMode) => {
578 unmodified lines