Skip to content

Commit

Permalink
Improve SourceMap emit for down-level async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 28, 2016
1 parent 150463e commit b155b68
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 58 deletions.
40 changes: 26 additions & 14 deletions src/compiler/emitter.ts
Expand Up @@ -1305,28 +1305,28 @@ namespace ts {
writeToken(SyntaxKind.OpenParenToken, openParenPos, node);
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end, node);
emitEmbeddedStatement(node.thenStatement);
emitEmbeddedStatement(node, node.thenStatement);
if (node.elseStatement) {
writeLine();
writeLineOrSpace(node);
writeToken(SyntaxKind.ElseKeyword, node.thenStatement.end, node);
if (node.elseStatement.kind === SyntaxKind.IfStatement) {
write(" ");
emit(node.elseStatement);
}
else {
emitEmbeddedStatement(node.elseStatement);
emitEmbeddedStatement(node, node.elseStatement);
}
}
}

function emitDoStatement(node: DoStatement) {
write("do");
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
if (isBlock(node.statement)) {
write(" ");
}
else {
writeLine();
writeLineOrSpace(node);
}

write("while (");
Expand All @@ -1338,7 +1338,7 @@ namespace ts {
write("while (");
emitExpression(node.expression);
write(")");
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
}

function emitForStatement(node: ForStatement) {
Expand All @@ -1351,7 +1351,7 @@ namespace ts {
write(";");
emitExpressionWithPrefix(" ", node.incrementor);
write(")");
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
}

function emitForInStatement(node: ForInStatement) {
Expand All @@ -1362,7 +1362,7 @@ namespace ts {
write(" in ");
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
}

function emitForOfStatement(node: ForOfStatement) {
Expand All @@ -1373,7 +1373,7 @@ namespace ts {
write(" of ");
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
}

function emitForBinding(node: VariableDeclarationList | Expression) {
Expand Down Expand Up @@ -1409,7 +1409,7 @@ namespace ts {
write("with (");
emitExpression(node.expression);
write(")");
emitEmbeddedStatement(node.statement);
emitEmbeddedStatement(node, node.statement);
}

function emitSwitchStatement(node: SwitchStatement) {
Expand Down Expand Up @@ -1437,9 +1437,12 @@ namespace ts {
function emitTryStatement(node: TryStatement) {
write("try ");
emit(node.tryBlock);
emit(node.catchClause);
if (node.catchClause) {
writeLineOrSpace(node);
emit(node.catchClause);
}
if (node.finallyBlock) {
writeLine();
writeLineOrSpace(node);
write("finally ");
emit(node.finallyBlock);
}
Expand Down Expand Up @@ -2125,8 +2128,8 @@ namespace ts {
}
}

function emitEmbeddedStatement(node: Statement) {
if (isBlock(node)) {
function emitEmbeddedStatement(parent: Node, node: Statement) {
if (isBlock(node) || getEmitFlags(parent) & EmitFlags.SingleLine) {
write(" ");
emit(node);
}
Expand Down Expand Up @@ -2291,6 +2294,15 @@ namespace ts {
}
}

function writeLineOrSpace(node: Node) {
if (getEmitFlags(node) & EmitFlags.SingleLine) {
write(" ");
}
else {
writeLine();
}
}

function writeIfAny(nodes: NodeArray<Node>, text: string) {
if (nodes && nodes.length > 0) {
write(text);
Expand Down
114 changes: 70 additions & 44 deletions src/compiler/transformers/generators.ts
Expand Up @@ -938,7 +938,7 @@ namespace ts {
}

markLabel(resumeLabel);
return createGeneratorResume();
return createGeneratorResume(/*location*/ node);
}

/**
Expand Down Expand Up @@ -1234,7 +1234,9 @@ namespace ts {

function transformAndEmitVariableDeclarationList(node: VariableDeclarationList): VariableDeclarationList {
for (const variable of node.declarations) {
hoistVariableDeclaration(<Identifier>variable.name);
const name = getSynthesizedClone(<Identifier>variable.name);
setCommentRange(name, variable.name);
hoistVariableDeclaration(name);
}

const variables = getInitializedVariables(node);
Expand Down Expand Up @@ -1287,7 +1289,7 @@ namespace ts {
if (containsYield(node.thenStatement) || containsYield(node.elseStatement)) {
const endLabel = defineLabel();
const elseLabel = node.elseStatement ? defineLabel() : undefined;
emitBreakWhenFalse(node.elseStatement ? elseLabel : endLabel, visitNode(node.expression, visitor, isExpression));
emitBreakWhenFalse(node.elseStatement ? elseLabel : endLabel, visitNode(node.expression, visitor, isExpression), /*location*/ node.expression);
transformAndEmitEmbeddedStatement(node.thenStatement);
if (node.elseStatement) {
emitBreak(endLabel);
Expand Down Expand Up @@ -2965,12 +2967,15 @@ namespace ts {
lastOperationWasAbrupt = true;
lastOperationWasCompletion = true;
writeStatement(
createReturn(
createArrayLiteral(expression
? [createInstruction(Instruction.Return), expression]
: [createInstruction(Instruction.Return)]
setEmitFlags(
createReturn(
createArrayLiteral(expression
? [createInstruction(Instruction.Return), expression]
: [createInstruction(Instruction.Return)]
),
operationLocation
),
operationLocation
EmitFlags.NoTokenSourceMaps
)
);
}
Expand All @@ -2984,12 +2989,15 @@ namespace ts {
function writeBreak(label: Label, operationLocation: TextRange): void {
lastOperationWasAbrupt = true;
writeStatement(
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
setEmitFlags(
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
),
EmitFlags.NoTokenSourceMaps
)
);
}
Expand All @@ -3003,15 +3011,21 @@ namespace ts {
*/
function writeBreakWhenTrue(label: Label, condition: Expression, operationLocation: TextRange): void {
writeStatement(
createIf(
condition,
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
)
setEmitFlags(
createIf(
condition,
setEmitFlags(
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
),
EmitFlags.NoTokenSourceMaps
)
),
EmitFlags.SingleLine
)
);
}
Expand All @@ -3025,15 +3039,21 @@ namespace ts {
*/
function writeBreakWhenFalse(label: Label, condition: Expression, operationLocation: TextRange): void {
writeStatement(
createIf(
createLogicalNot(condition),
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
)
setEmitFlags(
createIf(
createLogicalNot(condition),
setEmitFlags(
createReturn(
createArrayLiteral([
createInstruction(Instruction.Break),
createLabel(label)
]),
operationLocation
),
EmitFlags.NoTokenSourceMaps
)
),
EmitFlags.SingleLine
)
);
}
Expand All @@ -3047,13 +3067,16 @@ namespace ts {
function writeYield(expression: Expression, operationLocation: TextRange): void {
lastOperationWasAbrupt = true;
writeStatement(
createReturn(
createArrayLiteral(
expression
? [createInstruction(Instruction.Yield), expression]
: [createInstruction(Instruction.Yield)]
setEmitFlags(
createReturn(
createArrayLiteral(
expression
? [createInstruction(Instruction.Yield), expression]
: [createInstruction(Instruction.Yield)]
),
operationLocation
),
operationLocation
EmitFlags.NoTokenSourceMaps
)
);
}
Expand All @@ -3067,12 +3090,15 @@ namespace ts {
function writeYieldStar(expression: Expression, operationLocation: TextRange): void {
lastOperationWasAbrupt = true;
writeStatement(
createReturn(
createArrayLiteral([
createInstruction(Instruction.YieldStar),
expression
]),
operationLocation
setEmitFlags(
createReturn(
createArrayLiteral([
createInstruction(Instruction.YieldStar),
expression
]),
operationLocation
),
EmitFlags.NoTokenSourceMaps
)
);
}
Expand Down

0 comments on commit b155b68

Please sign in to comment.