From 910dd40a5ed5f79d8f79c884c7e392eca9b0038f Mon Sep 17 00:00:00 2001 From: ikatyang Date: Mon, 17 Jul 2017 18:00:54 +0800 Subject: [PATCH] feat(pipeP-like): allow non-PromiseLike as fn return type --- snapshots/ramda-tests.ts | 6 ++++++ templates/composeP.ts | 1 + templates/pipeP.ts | 1 + .../utils/create-composition-declarations.ts | 16 +++++++++------- tests/__snapshots__/ramda-tests.ts.snap | 6 ++++++ tests/ramda-tests.ts | 6 ++++++ 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/snapshots/ramda-tests.ts b/snapshots/ramda-tests.ts index 616d0d5..4add709 100644 --- a/snapshots/ramda-tests.ts +++ b/snapshots/ramda-tests.ts @@ -2194,6 +2194,12 @@ import * as R from '../ramda/dist/index'; (m: number) => Promise.resolve(R.multiply(2, m)), (m: number) => Promise.resolve(m / 2), )(10); + // @dts-jest:pass -> PromiseLike + R.pipeP( + (m: number) => Promise.resolve(R.multiply(2, m)), + (m: number) => Promise.resolve(m / 2), + R.multiply(5), + )(10); })(); // @dts-jest:group pluck diff --git a/templates/composeP.ts b/templates/composeP.ts index 66d8e9d..8c50113 100644 --- a/templates/composeP.ts +++ b/templates/composeP.ts @@ -8,5 +8,6 @@ export default create_composition_declarations( max_curry_level, 6, x => x, + x => `PromiseLike<${x}> | ${x}`, x => `PromiseLike<${x}>`, ); diff --git a/templates/pipeP.ts b/templates/pipeP.ts index 98f5c37..c9a5bad 100644 --- a/templates/pipeP.ts +++ b/templates/pipeP.ts @@ -8,5 +8,6 @@ export default create_composition_declarations( max_curry_level, 6, x => x, + x => `PromiseLike<${x}> | ${x}`, x => `PromiseLike<${x}>`, ); diff --git a/templates/utils/create-composition-declarations.ts b/templates/utils/create-composition-declarations.ts index 467a0db..1f1107e 100644 --- a/templates/utils/create-composition-declarations.ts +++ b/templates/utils/create-composition-declarations.ts @@ -6,9 +6,11 @@ export const create_composition_declarations = ( max_function_count: number, max_parameter_count: number, // istanbul ignore next - generate_parameter_type: (generic: string) => string = x => x, + generate_function_parameter_type: (generic: string) => string = x => x, // istanbul ignore next - generate_return_type: (generic: string) => string = x => x, + generate_function_return_type: (generic: string) => string = x => x, + // istanbul ignore next + generate_composed_return_type: (generic: string) => string = generate_function_return_type, ) => { const function_names = [...new Array(max_function_count)].map((_, index) => `fn${index + 1}`); const return_generics = [...new Array(max_function_count)].map((_, index) => `R${index + 1}`); @@ -33,7 +35,7 @@ export const create_composition_declarations = ( const current_parameter_generics = parameter_generics.slice(0, parameter_count); const entry_parameters = current_parameter_names.map((parameter_name, param_index) => ` - ${parameter_name}: ${generate_parameter_type(current_parameter_generics[param_index])} + ${parameter_name}: ${generate_function_parameter_type(current_parameter_generics[param_index])} `).join(','); declarations.push(` @@ -42,12 +44,12 @@ export const create_composition_declarations = ( const function_name = current_sorted_function_names[index]; const return_generic = current_sorted_return_generics[index]; return (index === entry_index) - ? `${function_name}: (${entry_parameters}) => ${generate_return_type(return_generic)}` + ? `${function_name}: (${entry_parameters}) => ${generate_function_return_type(return_generic)}` : `${function_name}: (v: ${ - generate_parameter_type(current_sorted_return_generics[index + (kind === 'compose' ? 1 : -1)]) - }) => ${generate_return_type(return_generic)}`; + generate_function_parameter_type(current_sorted_return_generics[index + (kind === 'compose' ? 1 : -1)]) + }) => ${generate_function_return_type(return_generic)}`; }).join(',')} - ): (${entry_parameters}) => ${generate_return_type(current_return_generics[current_return_generics.length - 1])}; + ): (${entry_parameters}) => ${generate_composed_return_type(current_return_generics[current_return_generics.length - 1])}; `); } } diff --git a/tests/__snapshots__/ramda-tests.ts.snap b/tests/__snapshots__/ramda-tests.ts.snap index 8b84785..c470c68 100644 --- a/tests/__snapshots__/ramda-tests.ts.snap +++ b/tests/__snapshots__/ramda-tests.ts.snap @@ -1025,6 +1025,12 @@ exports[`pipe f(3, 4) 1`] = `"number"`; exports[`pipe squareThenDoubleThenTriple(5) 1`] = `"number"`; +exports[`pipeP R.pipeP( + (m: number) => Promise.resolve(R.multiply(2, m)), + (m: number) => Promise.resolve(m / 2), + R.multiply(5), + )(10) 1`] = `"PromiseLike"`; + exports[`pipeP R.pipeP( (m: number) => Promise.resolve(R.multiply(2, m)), (m: number) => Promise.resolve(m / 2), diff --git a/tests/ramda-tests.ts b/tests/ramda-tests.ts index 84f36ed..a07010c 100644 --- a/tests/ramda-tests.ts +++ b/tests/ramda-tests.ts @@ -2194,6 +2194,12 @@ import * as R from '../ramda/dist/index'; (m: number) => Promise.resolve(R.multiply(2, m)), (m: number) => Promise.resolve(m / 2), )(10); + // @dts-jest:pass + R.pipeP( + (m: number) => Promise.resolve(R.multiply(2, m)), + (m: number) => Promise.resolve(m / 2), + R.multiply(5), + )(10); })(); // @dts-jest:group pluck