Skip to content

Commit a2e0b19

Browse files
committed
Emit for full down-level generators
1 parent 86091d7 commit a2e0b19

17 files changed

Lines changed: 1422 additions & 880 deletions

src/compiler/binder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,7 +2698,7 @@ namespace ts {
26982698
}
26992699

27002700
// Currently, we only support generators that were originally async function bodies.
2701-
if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
2701+
if (node.asteriskToken) {
27022702
transformFlags |= TransformFlags.AssertGenerator;
27032703
}
27042704

@@ -2774,7 +2774,7 @@ namespace ts {
27742774
// down-level generator.
27752775
// Currently we do not support transforming any other generator fucntions
27762776
// down level.
2777-
if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
2777+
if (node.asteriskToken) {
27782778
transformFlags |= TransformFlags.AssertGenerator;
27792779
}
27802780
}
@@ -2811,7 +2811,7 @@ namespace ts {
28112811
// down-level generator.
28122812
// Currently we do not support transforming any other generator fucntions
28132813
// down level.
2814-
if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) {
2814+
if (node.asteriskToken) {
28152815
transformFlags |= TransformFlags.AssertGenerator;
28162816
}
28172817

src/compiler/comments.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace ts {
2525
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];
2626
let hasWrittenComment = false;
2727
let disabled: boolean = compilerOptions.removeComments;
28+
// let leadingCommentPositions: Map<boolean>;
29+
// let trailingCommentPositions: Map<boolean>;
2830

2931
return {
3032
reset,
@@ -259,7 +261,8 @@ namespace ts {
259261

260262
function forEachLeadingCommentToEmit(pos: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) => void) {
261263
// Emit the leading comments only if the container's pos doesn't match because the container should take care of emitting these comments
262-
if (containerPos === -1 || pos !== containerPos) {
264+
if ((containerPos === -1 || pos !== containerPos) /* && !leadingCommentPositions[pos] */) {
265+
// leadingCommentPositions[pos] = true;
263266
if (hasDetachedComments(pos)) {
264267
forEachLeadingCommentWithoutDetachedComments(cb);
265268
}
@@ -271,7 +274,8 @@ namespace ts {
271274

272275
function forEachTrailingCommentToEmit(end: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean) => void) {
273276
// Emit the trailing comments only if the container's end doesn't match because the container should take care of emitting these comments
274-
if (containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd)) {
277+
if ((containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd)) /*&& !trailingCommentPositions[end] */) {
278+
// trailingCommentPositions[end] = true;
275279
forEachTrailingCommentRange(currentText, end, cb);
276280
}
277281
}
@@ -281,13 +285,17 @@ namespace ts {
281285
currentText = undefined;
282286
currentLineMap = undefined;
283287
detachedCommentsInfo = undefined;
288+
// leadingCommentPositions = undefined;
289+
// trailingCommentPositions = undefined;
284290
}
285291

286292
function setSourceFile(sourceFile: SourceFile) {
287293
currentSourceFile = sourceFile;
288294
currentText = currentSourceFile.text;
289295
currentLineMap = getLineStarts(currentSourceFile);
290296
detachedCommentsInfo = undefined;
297+
// leadingCommentPositions = createMap<boolean>();
298+
// trailingCommentPositions = createMap<boolean>();
291299
}
292300

293301
function hasDetachedComments(pos: number) {

src/compiler/emitter.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,28 +1276,28 @@ namespace ts {
12761276
writeToken(SyntaxKind.OpenParenToken, openParenPos, node);
12771277
emitExpression(node.expression);
12781278
writeToken(SyntaxKind.CloseParenToken, node.expression.end, node);
1279-
emitEmbeddedStatement(node.thenStatement);
1279+
emitEmbeddedStatement(node, node.thenStatement);
12801280
if (node.elseStatement) {
1281-
writeLine();
1281+
writeLineOrSpace(node);
12821282
writeToken(SyntaxKind.ElseKeyword, node.thenStatement.end, node);
12831283
if (node.elseStatement.kind === SyntaxKind.IfStatement) {
12841284
write(" ");
12851285
emit(node.elseStatement);
12861286
}
12871287
else {
1288-
emitEmbeddedStatement(node.elseStatement);
1288+
emitEmbeddedStatement(node, node.elseStatement);
12891289
}
12901290
}
12911291
}
12921292

12931293
function emitDoStatement(node: DoStatement) {
12941294
write("do");
1295-
emitEmbeddedStatement(node.statement);
1295+
emitEmbeddedStatement(node, node.statement);
12961296
if (isBlock(node.statement)) {
12971297
write(" ");
12981298
}
12991299
else {
1300-
writeLine();
1300+
writeLineOrSpace(node);
13011301
}
13021302

13031303
write("while (");
@@ -1309,7 +1309,7 @@ namespace ts {
13091309
write("while (");
13101310
emitExpression(node.expression);
13111311
write(")");
1312-
emitEmbeddedStatement(node.statement);
1312+
emitEmbeddedStatement(node, node.statement);
13131313
}
13141314

13151315
function emitForStatement(node: ForStatement) {
@@ -1322,7 +1322,7 @@ namespace ts {
13221322
write(";");
13231323
emitExpressionWithPrefix(" ", node.incrementor);
13241324
write(")");
1325-
emitEmbeddedStatement(node.statement);
1325+
emitEmbeddedStatement(node, node.statement);
13261326
}
13271327

13281328
function emitForInStatement(node: ForInStatement) {
@@ -1333,7 +1333,7 @@ namespace ts {
13331333
write(" in ");
13341334
emitExpression(node.expression);
13351335
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
1336-
emitEmbeddedStatement(node.statement);
1336+
emitEmbeddedStatement(node, node.statement);
13371337
}
13381338

13391339
function emitForOfStatement(node: ForOfStatement) {
@@ -1344,7 +1344,7 @@ namespace ts {
13441344
write(" of ");
13451345
emitExpression(node.expression);
13461346
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
1347-
emitEmbeddedStatement(node.statement);
1347+
emitEmbeddedStatement(node, node.statement);
13481348
}
13491349

13501350
function emitForBinding(node: VariableDeclarationList | Expression) {
@@ -1380,7 +1380,7 @@ namespace ts {
13801380
write("with (");
13811381
emitExpression(node.expression);
13821382
write(")");
1383-
emitEmbeddedStatement(node.statement);
1383+
emitEmbeddedStatement(node, node.statement);
13841384
}
13851385

13861386
function emitSwitchStatement(node: SwitchStatement) {
@@ -1408,9 +1408,13 @@ namespace ts {
14081408
function emitTryStatement(node: TryStatement) {
14091409
write("try ");
14101410
emit(node.tryBlock);
1411-
emit(node.catchClause);
1411+
if (node.catchClause) {
1412+
writeLineOrSpace(node);
1413+
emit(node.catchClause);
1414+
}
1415+
14121416
if (node.finallyBlock) {
1413-
writeLine();
1417+
writeLineOrSpace(node);
14141418
write("finally ");
14151419
emit(node.finallyBlock);
14161420
}
@@ -1880,7 +1884,6 @@ namespace ts {
18801884
}
18811885

18821886
function emitCatchClause(node: CatchClause) {
1883-
writeLine();
18841887
const openParenPos = writeToken(SyntaxKind.CatchKeyword, node.pos);
18851888
write(" ");
18861889
writeToken(SyntaxKind.OpenParenToken, openParenPos);
@@ -2087,8 +2090,8 @@ namespace ts {
20872090
}
20882091
}
20892092

2090-
function emitEmbeddedStatement(node: Statement) {
2091-
if (isBlock(node)) {
2093+
function emitEmbeddedStatement(parent: Node, node: Statement) {
2094+
if (isBlock(node) || getEmitFlags(parent) & EmitFlags.SingleLine) {
20922095
write(" ");
20932096
emit(node);
20942097
}
@@ -2253,6 +2256,15 @@ namespace ts {
22532256
}
22542257
}
22552258

2259+
function writeLineOrSpace(node: Node) {
2260+
if (getEmitFlags(node) & EmitFlags.SingleLine) {
2261+
write(" ");
2262+
}
2263+
else {
2264+
writeLine();
2265+
}
2266+
}
2267+
22562268
function writeIfAny(nodes: NodeArray<Node>, text: string) {
22572269
if (nodes && nodes.length > 0) {
22582270
write(text);

0 commit comments

Comments
 (0)