Skip to content

Commit

Permalink
Simplify chaining of transforms (#22994)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson authored and RyanCavanaugh committed Apr 25, 2019
1 parent bc46c77 commit e007ccf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 34 deletions.
33 changes: 0 additions & 33 deletions src/compiler/core.ts
Expand Up @@ -1639,39 +1639,6 @@ namespace ts {
};
}

/**
* High-order function, creates a function that executes a function composition.
* For example, `chain(a, b)` is the equivalent of `x => ((a', b') => y => b'(a'(y)))(a(x), b(x))`
*
* @param args The functions to chain.
*/
export function chain<T, U>(...args: ((t: T) => (u: U) => U)[]): (t: T) => (u: U) => U;
export function chain<T, U>(a: (t: T) => (u: U) => U, b: (t: T) => (u: U) => U, c: (t: T) => (u: U) => U, d: (t: T) => (u: U) => U, e: (t: T) => (u: U) => U): (t: T) => (u: U) => U {
if (e) {
const args: ((t: T) => (u: U) => U)[] = [];
for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}

return t => compose(...map(args, f => f(t)));
}
else if (d) {
return t => compose(a(t), b(t), c(t), d(t));
}
else if (c) {
return t => compose(a(t), b(t), c(t));
}
else if (b) {
return t => compose(a(t), b(t));
}
else if (a) {
return t => compose(a(t));
}
else {
return _ => u => u;
}
}

/**
* High-order function, composes functions. Note that functions are composed inside-out;
* for example, `compose(a, b)` is the equivalent of `x => b(a(x))`.
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/transformer.ts
Expand Up @@ -151,7 +151,13 @@ namespace ts {
performance.mark("beforeTransform");

// Chain together and initialize each transformer.
const transformation = chain(...transformers)(context);
const transformersWithContext = transformers.map(t => t(context));
const transformation = (node: T): T => {
for (const transform of transformersWithContext) {
node = transform(node);
}
return node;
};

// prevent modification of transformation hooks.
state = TransformationState.Initialized;
Expand Down

0 comments on commit e007ccf

Please sign in to comment.