Skip to content

Commit

Permalink
fix(codegen): ensure doc-comments are copied into generated code
Browse files Browse the repository at this point in the history
...and improve implementation of `pipeInto`
  • Loading branch information
biggyspender committed Mar 11, 2021
1 parent 3b18c26 commit 5f9ba59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"generate-functions:pipe": "ts-node tools/codeGeneration pipe pipe 64 > src/pipe.ts",
"generate-functions:pipeInto": "ts-node tools/codeGeneration pipeInto pipeInto 64 > src/pipeInto.ts",
"generate-functions:compose": "ts-node tools/codeGeneration compose compose 64 > src/compose.ts",
"generate-functions": "npm run generate-functions:pipe && npm run generate-functions:compose && npm run generate-functions:pipeInto"
"generate-functions": "npm run generate-functions:pipe && npm run generate-functions:compose && npm run generate-functions:pipeInto && npm run lint"
},
"lint-staged": {
"{src,test}/**/*.ts": [
Expand Down
3 changes: 2 additions & 1 deletion src/pipeInto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Func } from './types/Func'
import { UnaryFunction } from './types/UnaryFunction'
import { pipeImpl } from './pipeImpl'
import { applyArgs } from './applyArgs'

/**
* `pipeInto(src, f1, f2)` is shorthand for `applyArgs(src).to(pipe(f1, f2))`
Expand Down Expand Up @@ -4337,5 +4338,5 @@ export function pipeInto<TIn, TOut>(
o1: Func<[TIn], any>,
...operations: UnaryFunction<any, any>[]
): TOut {
return pipeImpl(o1, ...operations)(src) as TOut
return applyArgs(src).to(pipeImpl(o1, ...operations))
}
24 changes: 22 additions & 2 deletions tools/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ const rangeGenerator = function* (start: number, count: number) {

const range = (start: number, count: number): number[] => [...rangeGenerator(start, count)];

//function _pipe<TIn extends any[], T1, T2, TOut>(f1: Func<TIn, T1>, f2: UnaryFunction<T1, T2>, f3: UnaryFunction<T2, TOut>): Func<TIn, TOut>
const comments = {
pipe: `/**
* Type-enforcing left-to-right function composition function.
* The first parameter can be a function of any arity, but the remaining parameters must be unary functions.
* The return type of one function must be compatible with the argument of next function in the argument list
* (i.e. types flow from left-to-right)
* @returns A function with the arguments of the *first* function in the argument list and a return type of the *last* function in the argument list
*/`,
compose: `/**
* Type-enforcing right-to-left function composition function.
* The last parameter (i.e. the first function to be evaluated) in the argument list can be a function of any arity, but the remaining parameters must be unary functions.
* The return type of one function must be compatible with the argument of previous function in the argument list
* (i.e. types flow from right-to-left)
* @returns A function with the arguments of the last function in the argument list and a return type of the first function in the argument list
*/`,
pipeInto: `/**
* \`pipeInto(src, f1, f2)\` is shorthand for \`applyArgs(src).to(pipe(f1, f2))\`
*/`
}

const getLines =
(name: string, type: "pipe" | "compose" | "pipeInto", numOverloads: number) => {
return [
"import { Func } from './types/Func';",
"import { UnaryFunction } from './types/UnaryFunction';",
"import { pipeImpl } from './pipeImpl';",
[...(type === "pipeInto" ? ["import { applyArgs } from './applyArgs'"] : [])],
"",
comments[type],
...range(0, numOverloads).map(n => {
const functionAndGenericDecl = `export function ${name}<${type === "pipeInto" ? "TIn" : "TIn extends any[]"}, ${[...range(1, n).map(n => `T${n}`), "TOut"].join(", ")}>`;
const parameterTypeList =
Expand All @@ -40,7 +60,7 @@ const getLines =
[...(type === "pipe"
? ["return pipeImpl(o1, ...operations)"]
: type === "pipeInto"
? ["return pipeImpl(o1, ...operations)(src) as TOut"]
? ["return applyArgs(src).to(pipeImpl(o1, ...operations))"]
: [
"const o1 = args[args.length - 1] as Func<TIn, any>;",
"const operations = args.slice(0, -1).reverse() as UnaryFunction<any, any>[];",
Expand Down

0 comments on commit 5f9ba59

Please sign in to comment.