From f8460733aa8b42e7a63d5d332bb3cdfba0212f62 Mon Sep 17 00:00:00 2001 From: ikatyang Date: Tue, 27 Jun 2017 15:25:30 +0800 Subject: [PATCH] fix(curried-interfaces): support multi-version building --- gulpfile.ts | 18 ++- src/$curried-functions.d.ts | 119 +++++++++++++++++++ templates/$curried-functions.ts | 4 +- templates/README.md | 2 + templates/utils/create-curried-interfaces.ts | 11 +- 5 files changed, 145 insertions(+), 9 deletions(-) diff --git a/gulpfile.ts b/gulpfile.ts index 8c563ee..420b24e 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -104,7 +104,9 @@ function get_top_level_members(filename: string, selectable?: boolean, placehold const members: dts.ITopLevelMember[] = []; const basename = path.basename(filename); - if (basename.endsWith('.d.ts')) { + if (basename === '$curried-functions.ts') { + push_curried_functions_members(); + } else if (basename.endsWith('.d.ts')) { if (basename.startsWith('$')) { push_r_ts_members(); } else { @@ -154,15 +156,21 @@ function get_top_level_members(filename: string, selectable?: boolean, placehold } function push_ts_members() { - const declarations = get_ts_members(); + const declarations = get_ts_default(); members.push(...declarations); } function push_c_ts_members() { - const declarations = get_ts_members(); + const declarations = get_ts_default(); push_curry_members(declarations); } + function push_curried_functions_members() { + const curried_functions_generator = get_ts_default(true); + const declarations = curried_functions_generator(selectable, placeholder); + members.push(...declarations); + } + function push_curry_members(the_members: dts.ITopLevelMember[]) { const imports = the_members.filter( (member): member is dts.IImportNamed => (member.kind === dts.ElementKind.ImportNamed), @@ -212,13 +220,13 @@ function get_top_level_members(filename: string, selectable?: boolean, placehold ); } - function get_ts_members() { + function get_ts_default(special_case = false) { // tslint:disable-next-line:no-require-imports const required: any = require(filename); delete require.cache[require.resolve(filename)]; const declarations = required.default; - if (!is_valid_export_default(declarations)) { + if (!special_case && !is_valid_export_default(declarations)) { throw new Error(`Template.ts should default-export an array of declarations: ${filename}`); } diff --git a/src/$curried-functions.d.ts b/src/$curried-functions.d.ts index 2a841c9..4615940 100644 --- a/src/$curried-functions.d.ts +++ b/src/$curried-functions.d.ts @@ -6,6 +6,9 @@ export interface CurriedFunction1 { export interface CurriedFunction2 { (_1: PH, v2: T2): CurriedFunction1; (v1: T1, v2: T2): R; + <$SEL extends "01">(): (_1: PH, v2: T2) => CurriedFunction1; + <$SEL extends "11">(): (v1: T1, v2: T2) => R; + <$SEL extends "1">(): (v1: T1) => CurriedFunction1; (v1: T1): CurriedFunction1; } export interface CurriedFunction3 { @@ -15,6 +18,13 @@ export interface CurriedFunction3 { (v1: T1, v2: T2, v3: T3): R; (_1: PH, v2: T2): CurriedFunction2; (v1: T1, v2: T2): CurriedFunction1; + <$SEL extends "101">(): (v1: T1, _2: PH, v3: T3) => CurriedFunction1; + <$SEL extends "011">(): (_1: PH, v2: T2, v3: T3) => CurriedFunction1; + <$SEL extends "001">(): (_1: PH, _2: PH, v3: T3) => CurriedFunction2; + <$SEL extends "111">(): (v1: T1, v2: T2, v3: T3) => R; + <$SEL extends "01">(): (_1: PH, v2: T2) => CurriedFunction2; + <$SEL extends "11">(): (v1: T1, v2: T2) => CurriedFunction1; + <$SEL extends "1">(): (v1: T1) => CurriedFunction2; (v1: T1): CurriedFunction2; } export interface CurriedFunction4 { @@ -32,6 +42,21 @@ export interface CurriedFunction4 { (v1: T1, v2: T2, v3: T3): CurriedFunction1; (_1: PH, v2: T2): CurriedFunction3; (v1: T1, v2: T2): CurriedFunction2; + <$SEL extends "1101">(): (v1: T1, v2: T2, _3: PH, v4: T4) => CurriedFunction1; + <$SEL extends "1011">(): (v1: T1, _2: PH, v3: T3, v4: T4) => CurriedFunction1; + <$SEL extends "1001">(): (v1: T1, _2: PH, _3: PH, v4: T4) => CurriedFunction2; + <$SEL extends "0111">(): (_1: PH, v2: T2, v3: T3, v4: T4) => CurriedFunction1; + <$SEL extends "0101">(): (_1: PH, v2: T2, _3: PH, v4: T4) => CurriedFunction2; + <$SEL extends "0011">(): (_1: PH, _2: PH, v3: T3, v4: T4) => CurriedFunction2; + <$SEL extends "0001">(): (_1: PH, _2: PH, _3: PH, v4: T4) => CurriedFunction3; + <$SEL extends "1111">(): (v1: T1, v2: T2, v3: T3, v4: T4) => R; + <$SEL extends "101">(): (v1: T1, _2: PH, v3: T3) => CurriedFunction2; + <$SEL extends "011">(): (_1: PH, v2: T2, v3: T3) => CurriedFunction2; + <$SEL extends "001">(): (_1: PH, _2: PH, v3: T3) => CurriedFunction3; + <$SEL extends "111">(): (v1: T1, v2: T2, v3: T3) => CurriedFunction1; + <$SEL extends "01">(): (_1: PH, v2: T2) => CurriedFunction3; + <$SEL extends "11">(): (v1: T1, v2: T2) => CurriedFunction2; + <$SEL extends "1">(): (v1: T1) => CurriedFunction3; (v1: T1): CurriedFunction3; } export interface CurriedFunction5 { @@ -65,6 +90,37 @@ export interface CurriedFunction5 { (v1: T1, v2: T2, v3: T3): CurriedFunction2; (_1: PH, v2: T2): CurriedFunction4; (v1: T1, v2: T2): CurriedFunction3; + <$SEL extends "11101">(): (v1: T1, v2: T2, v3: T3, _4: PH, v5: T5) => CurriedFunction1; + <$SEL extends "11011">(): (v1: T1, v2: T2, _3: PH, v4: T4, v5: T5) => CurriedFunction1; + <$SEL extends "11001">(): (v1: T1, v2: T2, _3: PH, _4: PH, v5: T5) => CurriedFunction2; + <$SEL extends "10111">(): (v1: T1, _2: PH, v3: T3, v4: T4, v5: T5) => CurriedFunction1; + <$SEL extends "10101">(): (v1: T1, _2: PH, v3: T3, _4: PH, v5: T5) => CurriedFunction2; + <$SEL extends "10011">(): (v1: T1, _2: PH, _3: PH, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "10001">(): (v1: T1, _2: PH, _3: PH, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "01111">(): (_1: PH, v2: T2, v3: T3, v4: T4, v5: T5) => CurriedFunction1; + <$SEL extends "01101">(): (_1: PH, v2: T2, v3: T3, _4: PH, v5: T5) => CurriedFunction2; + <$SEL extends "01011">(): (_1: PH, v2: T2, _3: PH, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "01001">(): (_1: PH, v2: T2, _3: PH, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "00111">(): (_1: PH, _2: PH, v3: T3, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "00101">(): (_1: PH, _2: PH, v3: T3, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "00011">(): (_1: PH, _2: PH, _3: PH, v4: T4, v5: T5) => CurriedFunction3; + <$SEL extends "00001">(): (_1: PH, _2: PH, _3: PH, _4: PH, v5: T5) => CurriedFunction4; + <$SEL extends "11111">(): (v1: T1, v2: T2, v3: T3, v4: T4, v5: T5) => R; + <$SEL extends "1101">(): (v1: T1, v2: T2, _3: PH, v4: T4) => CurriedFunction2; + <$SEL extends "1011">(): (v1: T1, _2: PH, v3: T3, v4: T4) => CurriedFunction2; + <$SEL extends "1001">(): (v1: T1, _2: PH, _3: PH, v4: T4) => CurriedFunction3; + <$SEL extends "0111">(): (_1: PH, v2: T2, v3: T3, v4: T4) => CurriedFunction2; + <$SEL extends "0101">(): (_1: PH, v2: T2, _3: PH, v4: T4) => CurriedFunction3; + <$SEL extends "0011">(): (_1: PH, _2: PH, v3: T3, v4: T4) => CurriedFunction3; + <$SEL extends "0001">(): (_1: PH, _2: PH, _3: PH, v4: T4) => CurriedFunction4; + <$SEL extends "1111">(): (v1: T1, v2: T2, v3: T3, v4: T4) => CurriedFunction1; + <$SEL extends "101">(): (v1: T1, _2: PH, v3: T3) => CurriedFunction3; + <$SEL extends "011">(): (_1: PH, v2: T2, v3: T3) => CurriedFunction3; + <$SEL extends "001">(): (_1: PH, _2: PH, v3: T3) => CurriedFunction4; + <$SEL extends "111">(): (v1: T1, v2: T2, v3: T3) => CurriedFunction2; + <$SEL extends "01">(): (_1: PH, v2: T2) => CurriedFunction4; + <$SEL extends "11">(): (v1: T1, v2: T2) => CurriedFunction3; + <$SEL extends "1">(): (v1: T1) => CurriedFunction4; (v1: T1): CurriedFunction4; } export interface CurriedFunction6 { @@ -130,5 +186,68 @@ export interface CurriedFunction6 { (v1: T1, v2: T2, v3: T3): CurriedFunction3; (_1: PH, v2: T2): CurriedFunction5; (v1: T1, v2: T2): CurriedFunction4; + <$SEL extends "111101">(): (v1: T1, v2: T2, v3: T3, v4: T4, _5: PH, v6: T6) => CurriedFunction1; + <$SEL extends "111011">(): (v1: T1, v2: T2, v3: T3, _4: PH, v5: T5, v6: T6) => CurriedFunction1; + <$SEL extends "111001">(): (v1: T1, v2: T2, v3: T3, _4: PH, _5: PH, v6: T6) => CurriedFunction2; + <$SEL extends "110111">(): (v1: T1, v2: T2, _3: PH, v4: T4, v5: T5, v6: T6) => CurriedFunction1; + <$SEL extends "110101">(): (v1: T1, v2: T2, _3: PH, v4: T4, _5: PH, v6: T6) => CurriedFunction2; + <$SEL extends "110011">(): (v1: T1, v2: T2, _3: PH, _4: PH, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "110001">(): (v1: T1, v2: T2, _3: PH, _4: PH, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "101111">(): (v1: T1, _2: PH, v3: T3, v4: T4, v5: T5, v6: T6) => CurriedFunction1; + <$SEL extends "101101">(): (v1: T1, _2: PH, v3: T3, v4: T4, _5: PH, v6: T6) => CurriedFunction2; + <$SEL extends "101011">(): (v1: T1, _2: PH, v3: T3, _4: PH, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "101001">(): (v1: T1, _2: PH, v3: T3, _4: PH, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "100111">(): (v1: T1, _2: PH, _3: PH, v4: T4, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "100101">(): (v1: T1, _2: PH, _3: PH, v4: T4, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "100011">(): (v1: T1, _2: PH, _3: PH, _4: PH, v5: T5, v6: T6) => CurriedFunction3; + <$SEL extends "100001">(): (v1: T1, _2: PH, _3: PH, _4: PH, _5: PH, v6: T6) => CurriedFunction4; + <$SEL extends "011111">(): (_1: PH, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => CurriedFunction1; + <$SEL extends "011101">(): (_1: PH, v2: T2, v3: T3, v4: T4, _5: PH, v6: T6) => CurriedFunction2; + <$SEL extends "011011">(): (_1: PH, v2: T2, v3: T3, _4: PH, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "011001">(): (_1: PH, v2: T2, v3: T3, _4: PH, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "010111">(): (_1: PH, v2: T2, _3: PH, v4: T4, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "010101">(): (_1: PH, v2: T2, _3: PH, v4: T4, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "010011">(): (_1: PH, v2: T2, _3: PH, _4: PH, v5: T5, v6: T6) => CurriedFunction3; + <$SEL extends "010001">(): (_1: PH, v2: T2, _3: PH, _4: PH, _5: PH, v6: T6) => CurriedFunction4; + <$SEL extends "001111">(): (_1: PH, _2: PH, v3: T3, v4: T4, v5: T5, v6: T6) => CurriedFunction2; + <$SEL extends "001101">(): (_1: PH, _2: PH, v3: T3, v4: T4, _5: PH, v6: T6) => CurriedFunction3; + <$SEL extends "001011">(): (_1: PH, _2: PH, v3: T3, _4: PH, v5: T5, v6: T6) => CurriedFunction3; + <$SEL extends "001001">(): (_1: PH, _2: PH, v3: T3, _4: PH, _5: PH, v6: T6) => CurriedFunction4; + <$SEL extends "000111">(): (_1: PH, _2: PH, _3: PH, v4: T4, v5: T5, v6: T6) => CurriedFunction3; + <$SEL extends "000101">(): (_1: PH, _2: PH, _3: PH, v4: T4, _5: PH, v6: T6) => CurriedFunction4; + <$SEL extends "000011">(): (_1: PH, _2: PH, _3: PH, _4: PH, v5: T5, v6: T6) => CurriedFunction4; + <$SEL extends "000001">(): (_1: PH, _2: PH, _3: PH, _4: PH, _5: PH, v6: T6) => CurriedFunction5; + <$SEL extends "111111">(): (v1: T1, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R; + <$SEL extends "11101">(): (v1: T1, v2: T2, v3: T3, _4: PH, v5: T5) => CurriedFunction2; + <$SEL extends "11011">(): (v1: T1, v2: T2, _3: PH, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "11001">(): (v1: T1, v2: T2, _3: PH, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "10111">(): (v1: T1, _2: PH, v3: T3, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "10101">(): (v1: T1, _2: PH, v3: T3, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "10011">(): (v1: T1, _2: PH, _3: PH, v4: T4, v5: T5) => CurriedFunction3; + <$SEL extends "10001">(): (v1: T1, _2: PH, _3: PH, _4: PH, v5: T5) => CurriedFunction4; + <$SEL extends "01111">(): (_1: PH, v2: T2, v3: T3, v4: T4, v5: T5) => CurriedFunction2; + <$SEL extends "01101">(): (_1: PH, v2: T2, v3: T3, _4: PH, v5: T5) => CurriedFunction3; + <$SEL extends "01011">(): (_1: PH, v2: T2, _3: PH, v4: T4, v5: T5) => CurriedFunction3; + <$SEL extends "01001">(): (_1: PH, v2: T2, _3: PH, _4: PH, v5: T5) => CurriedFunction4; + <$SEL extends "00111">(): (_1: PH, _2: PH, v3: T3, v4: T4, v5: T5) => CurriedFunction3; + <$SEL extends "00101">(): (_1: PH, _2: PH, v3: T3, _4: PH, v5: T5) => CurriedFunction4; + <$SEL extends "00011">(): (_1: PH, _2: PH, _3: PH, v4: T4, v5: T5) => CurriedFunction4; + <$SEL extends "00001">(): (_1: PH, _2: PH, _3: PH, _4: PH, v5: T5) => CurriedFunction5; + <$SEL extends "11111">(): (v1: T1, v2: T2, v3: T3, v4: T4, v5: T5) => CurriedFunction1; + <$SEL extends "1101">(): (v1: T1, v2: T2, _3: PH, v4: T4) => CurriedFunction3; + <$SEL extends "1011">(): (v1: T1, _2: PH, v3: T3, v4: T4) => CurriedFunction3; + <$SEL extends "1001">(): (v1: T1, _2: PH, _3: PH, v4: T4) => CurriedFunction4; + <$SEL extends "0111">(): (_1: PH, v2: T2, v3: T3, v4: T4) => CurriedFunction3; + <$SEL extends "0101">(): (_1: PH, v2: T2, _3: PH, v4: T4) => CurriedFunction4; + <$SEL extends "0011">(): (_1: PH, _2: PH, v3: T3, v4: T4) => CurriedFunction4; + <$SEL extends "0001">(): (_1: PH, _2: PH, _3: PH, v4: T4) => CurriedFunction5; + <$SEL extends "1111">(): (v1: T1, v2: T2, v3: T3, v4: T4) => CurriedFunction2; + <$SEL extends "101">(): (v1: T1, _2: PH, v3: T3) => CurriedFunction4; + <$SEL extends "011">(): (_1: PH, v2: T2, v3: T3) => CurriedFunction4; + <$SEL extends "001">(): (_1: PH, _2: PH, v3: T3) => CurriedFunction5; + <$SEL extends "111">(): (v1: T1, v2: T2, v3: T3) => CurriedFunction3; + <$SEL extends "01">(): (_1: PH, v2: T2) => CurriedFunction5; + <$SEL extends "11">(): (v1: T1, v2: T2) => CurriedFunction4; + <$SEL extends "1">(): (v1: T1) => CurriedFunction5; (v1: T1): CurriedFunction5; } diff --git a/templates/$curried-functions.ts b/templates/$curried-functions.ts index a58ef65..21696f5 100644 --- a/templates/$curried-functions.ts +++ b/templates/$curried-functions.ts @@ -4,7 +4,7 @@ import {create_curried_interfaces} from './utils/create-curried-interfaces'; export const max_curry_level = 6; -export default [ +export default (selectable: boolean, placeholder: boolean) => [ dts.create_import_named({ members: [ dts.create_import_member({ @@ -15,7 +15,7 @@ export default [ from: './$placeholder', }), ...dts.parse(`export type CurriedFunction0 = () => R;`).members, - ...create_curried_interfaces(max_curry_level).map( + ...create_curried_interfaces(max_curry_level, selectable, placeholder).map( the_interface => ({ ...the_interface, export: true, diff --git a/templates/README.md b/templates/README.md index 6095d87..60e7508 100644 --- a/templates/README.md +++ b/templates/README.md @@ -1,3 +1,5 @@ +- `$curried-functions.ts`: special codegen types without jsdoc binding + - `*.r.ts`: manual constant with jsdoc binding - `*.d.ts`: manual funciton prototypes with jsdoc binding and auto-currying - `*.c.ts`: codegen funciton prototypes with jsdoc binding and auto-currying diff --git a/templates/utils/create-curried-interfaces.ts b/templates/utils/create-curried-interfaces.ts index 8d3e852..d50e47c 100644 --- a/templates/utils/create-curried-interfaces.ts +++ b/templates/utils/create-curried-interfaces.ts @@ -7,10 +7,11 @@ import { get_curried_interface_parameter_placeholder_name, placeholder_name_abbr, } from './constants'; +import {create_selectable_signatures} from './create-selectable-signatures'; import {push_signatures} from './push-signatures'; import {sort_signatures} from './sort-signatures'; -export const create_curried_interfaces = (max_curry_level: number) => { +export const create_curried_interfaces = (max_curry_level: number, selectable = true, placeholder = true) => { const generics = [...new Array(max_curry_level)].map((_, index) => dts.create_generic_declaration({ name: get_curried_interface_generic_name(index), @@ -47,7 +48,9 @@ export const create_curried_interfaces = (max_curry_level: number) => { interfaces.forEach((the_interface, index) => { push_signatures( parameters.slice(0, index + 1), - placeholders.slice(0, index + 1), + placeholder + ? placeholders.slice(0, index + 1) + : [], (used_parameters, return_parameters) => { the_interface.type!.members!.push(dts.create_object_member({ owned: dts.create_function_declaration({ @@ -70,6 +73,10 @@ export const create_curried_interfaces = (max_curry_level: number) => { ); const object_type = interfaces[index].type!; object_type.members = sort_signatures(object_type.members as dts.IObjectMember[]); + + if (selectable && object_type.members.length > 1) { + object_type.members.splice(-1, 0, ...create_selectable_signatures(object_type.members as dts.IObjectMember[])); + } }); return interfaces;