Skip to content

Commit e007ccf

Browse files
Andy HansonRyanCavanaugh
authored andcommitted
Simplify chaining of transforms (microsoft#22994)
1 parent bc46c77 commit e007ccf

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

src/compiler/core.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,39 +1639,6 @@ namespace ts {
16391639
};
16401640
}
16411641

1642-
/**
1643-
* High-order function, creates a function that executes a function composition.
1644-
* For example, `chain(a, b)` is the equivalent of `x => ((a', b') => y => b'(a'(y)))(a(x), b(x))`
1645-
*
1646-
* @param args The functions to chain.
1647-
*/
1648-
export function chain<T, U>(...args: ((t: T) => (u: U) => U)[]): (t: T) => (u: U) => U;
1649-
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 {
1650-
if (e) {
1651-
const args: ((t: T) => (u: U) => U)[] = [];
1652-
for (let i = 0; i < arguments.length; i++) {
1653-
args[i] = arguments[i];
1654-
}
1655-
1656-
return t => compose(...map(args, f => f(t)));
1657-
}
1658-
else if (d) {
1659-
return t => compose(a(t), b(t), c(t), d(t));
1660-
}
1661-
else if (c) {
1662-
return t => compose(a(t), b(t), c(t));
1663-
}
1664-
else if (b) {
1665-
return t => compose(a(t), b(t));
1666-
}
1667-
else if (a) {
1668-
return t => compose(a(t));
1669-
}
1670-
else {
1671-
return _ => u => u;
1672-
}
1673-
}
1674-
16751642
/**
16761643
* High-order function, composes functions. Note that functions are composed inside-out;
16771644
* for example, `compose(a, b)` is the equivalent of `x => b(a(x))`.

src/compiler/transformer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,13 @@ namespace ts {
151151
performance.mark("beforeTransform");
152152

153153
// Chain together and initialize each transformer.
154-
const transformation = chain(...transformers)(context);
154+
const transformersWithContext = transformers.map(t => t(context));
155+
const transformation = (node: T): T => {
156+
for (const transform of transformersWithContext) {
157+
node = transform(node);
158+
}
159+
return node;
160+
};
155161

156162
// prevent modification of transformation hooks.
157163
state = TransformationState.Initialized;

0 commit comments

Comments
 (0)