Skip to content

Commit edd8eb8

Browse files
authored
Merge pull request microsoft#11165 from Microsoft/emitNode
Transformation API Cleanup
2 parents a07a944 + e151401 commit edd8eb8

18 files changed

+779
-1238
lines changed

src/compiler/binder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ namespace ts {
25922592
}
25932593

25942594
// Currently, we only support generators that were originally async function bodies.
2595-
if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) {
2595+
if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
25962596
transformFlags |= TransformFlags.AssertGenerator;
25972597
}
25982598

@@ -2667,7 +2667,7 @@ namespace ts {
26672667
// down-level generator.
26682668
// Currently we do not support transforming any other generator fucntions
26692669
// down level.
2670-
if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) {
2670+
if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
26712671
transformFlags |= TransformFlags.AssertGenerator;
26722672
}
26732673
}
@@ -2698,7 +2698,7 @@ namespace ts {
26982698
// down-level generator.
26992699
// Currently we do not support transforming any other generator fucntions
27002700
// down level.
2701-
if (asteriskToken && node.emitFlags & NodeEmitFlags.AsyncFunctionBody) {
2701+
if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
27022702
transformFlags |= TransformFlags.AssertGenerator;
27032703
}
27042704

src/compiler/comments.ts

+24-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ts {
55
export interface CommentWriter {
66
reset(): void;
77
setSourceFile(sourceFile: SourceFile): void;
8-
emitNodeWithComments(node: Node, emitCallback: (node: Node) => void): void;
8+
emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void;
99
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1010
emitTrailingCommentsOfPosition(pos: number): void;
1111
}
@@ -34,22 +34,24 @@ namespace ts {
3434
emitTrailingCommentsOfPosition,
3535
};
3636

37-
function emitNodeWithComments(node: Node, emitCallback: (node: Node) => void) {
37+
function emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void) {
3838
if (disabled) {
39-
emitCallback(node);
39+
emitCallback(emitContext, node);
4040
return;
4141
}
4242

4343
if (node) {
44-
const { pos, end } = node.commentRange || node;
45-
const emitFlags = node.emitFlags;
44+
const { pos, end } = getCommentRange(node);
45+
const emitFlags = getEmitFlags(node);
4646
if ((pos < 0 && end < 0) || (pos === end)) {
4747
// Both pos and end are synthesized, so just emit the node without comments.
48-
if (emitFlags & NodeEmitFlags.NoNestedComments) {
49-
disableCommentsAndEmit(node, emitCallback);
48+
if (emitFlags & EmitFlags.NoNestedComments) {
49+
disabled = true;
50+
emitCallback(emitContext, node);
51+
disabled = false;
5052
}
5153
else {
52-
emitCallback(node);
54+
emitCallback(emitContext, node);
5355
}
5456
}
5557
else {
@@ -58,8 +60,8 @@ namespace ts {
5860
}
5961

6062
const isEmittedNode = node.kind !== SyntaxKind.NotEmittedStatement;
61-
const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0;
62-
const skipTrailingComments = end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0;
63+
const skipLeadingComments = pos < 0 || (emitFlags & EmitFlags.NoLeadingComments) !== 0;
64+
const skipTrailingComments = end < 0 || (emitFlags & EmitFlags.NoTrailingComments) !== 0;
6365

6466
// Emit leading comments if the position is not synthesized and the node
6567
// has not opted out from emitting leading comments.
@@ -90,11 +92,13 @@ namespace ts {
9092
performance.measure("commentTime", "preEmitNodeWithComment");
9193
}
9294

93-
if (emitFlags & NodeEmitFlags.NoNestedComments) {
94-
disableCommentsAndEmit(node, emitCallback);
95+
if (emitFlags & EmitFlags.NoNestedComments) {
96+
disabled = true;
97+
emitCallback(emitContext, node);
98+
disabled = false;
9599
}
96100
else {
97-
emitCallback(node);
101+
emitCallback(emitContext, node);
98102
}
99103

100104
if (extendedDiagnostics) {
@@ -125,9 +129,9 @@ namespace ts {
125129
}
126130

127131
const { pos, end } = detachedRange;
128-
const emitFlags = node.emitFlags;
129-
const skipLeadingComments = pos < 0 || (emitFlags & NodeEmitFlags.NoLeadingComments) !== 0;
130-
const skipTrailingComments = disabled || end < 0 || (emitFlags & NodeEmitFlags.NoTrailingComments) !== 0;
132+
const emitFlags = getEmitFlags(node);
133+
const skipLeadingComments = pos < 0 || (emitFlags & EmitFlags.NoLeadingComments) !== 0;
134+
const skipTrailingComments = disabled || end < 0 || (emitFlags & EmitFlags.NoTrailingComments) !== 0;
131135

132136
if (!skipLeadingComments) {
133137
emitDetachedCommentsAndUpdateCommentsInfo(detachedRange);
@@ -137,8 +141,10 @@ namespace ts {
137141
performance.measure("commentTime", "preEmitBodyWithDetachedComments");
138142
}
139143

140-
if (emitFlags & NodeEmitFlags.NoNestedComments) {
141-
disableCommentsAndEmit(node, emitCallback);
144+
if (emitFlags & EmitFlags.NoNestedComments && !disabled) {
145+
disabled = true;
146+
emitCallback(node);
147+
disabled = false;
142148
}
143149
else {
144150
emitCallback(node);
@@ -284,17 +290,6 @@ namespace ts {
284290
detachedCommentsInfo = undefined;
285291
}
286292

287-
function disableCommentsAndEmit(node: Node, emitCallback: (node: Node) => void): void {
288-
if (disabled) {
289-
emitCallback(node);
290-
}
291-
else {
292-
disabled = true;
293-
emitCallback(node);
294-
disabled = false;
295-
}
296-
}
297-
298293
function hasDetachedComments(pos: number) {
299294
return detachedCommentsInfo !== undefined && lastOrUndefined(detachedCommentsInfo).nodePos === pos;
300295
}

src/compiler/core.ts

+66-1
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,72 @@ namespace ts {
840840
};
841841
}
842842

843+
/**
844+
* High-order function, creates a function that executes a function composition.
845+
* For example, `chain(a, b)` is the equivalent of `x => ((a', b') => y => b'(a'(y)))(a(x), b(x))`
846+
*
847+
* @param args The functions to chain.
848+
*/
849+
export function chain<T, U>(...args: ((t: T) => (u: U) => U)[]): (t: T) => (u: U) => U;
850+
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 {
851+
if (e) {
852+
const args: ((t: T) => (u: U) => U)[] = [];
853+
for (let i = 0; i < arguments.length; i++) {
854+
args[i] = arguments[i];
855+
}
856+
857+
return t => compose(...map(args, f => f(t)));
858+
}
859+
else if (d) {
860+
return t => compose(a(t), b(t), c(t), d(t));
861+
}
862+
else if (c) {
863+
return t => compose(a(t), b(t), c(t));
864+
}
865+
else if (b) {
866+
return t => compose(a(t), b(t));
867+
}
868+
else if (a) {
869+
return t => compose(a(t));
870+
}
871+
else {
872+
return t => u => u;
873+
}
874+
}
875+
876+
/**
877+
* High-order function, composes functions. Note that functions are composed inside-out;
878+
* for example, `compose(a, b)` is the equivalent of `x => b(a(x))`.
879+
*
880+
* @param args The functions to compose.
881+
*/
882+
export function compose<T>(...args: ((t: T) => T)[]): (t: T) => T;
883+
export function compose<T>(a: (t: T) => T, b: (t: T) => T, c: (t: T) => T, d: (t: T) => T, e: (t: T) => T): (t: T) => T {
884+
if (e) {
885+
const args: ((t: T) => T)[] = [];
886+
for (let i = 0; i < arguments.length; i++) {
887+
args[i] = arguments[i];
888+
}
889+
890+
return t => reduceLeft<(t: T) => T, T>(args, (u, f) => f(u), t);
891+
}
892+
else if (d) {
893+
return t => d(c(b(a(t))));
894+
}
895+
else if (c) {
896+
return t => c(b(a(t)));
897+
}
898+
else if (b) {
899+
return t => b(a(t));
900+
}
901+
else if (a) {
902+
return t => a(t);
903+
}
904+
else {
905+
return t => t;
906+
}
907+
}
908+
843909
function formatStringFromArgs(text: string, args: { [index: number]: any; }, baseIndex?: number): string {
844910
baseIndex = baseIndex || 0;
845911

@@ -1778,7 +1844,6 @@ namespace ts {
17781844
this.transformFlags = TransformFlags.None;
17791845
this.parent = undefined;
17801846
this.original = undefined;
1781-
this.transformId = 0;
17821847
}
17831848

17841849
export let objectAllocator: ObjectAllocator = {

0 commit comments

Comments
 (0)