Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
12 changes: 4 additions & 8 deletions tests/baselines/reference/asyncAwaitWithCapturedBlockScopeVar.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ function fn1() {
i = 0;
_a.label = 1;
case 1:
if (!(i < 1))
return [3 /*break*/, 4];
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_1(i)];
case 2:
_a.sent();
Expand Down Expand Up @@ -92,8 +91,7 @@ function fn2() {
i = 0;
_a.label = 1;
case 1:
if (!(i < 1))
return [3 /*break*/, 4];
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_2(i)];
case 2:
state_1 = _a.sent();
Expand Down Expand Up @@ -129,8 +127,7 @@ function fn3() {
i = 0;
_a.label = 1;
case 1:
if (!(i < 1))
return [3 /*break*/, 4];
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_3(i)];
case 2:
_a.sent();
Expand Down Expand Up @@ -164,8 +161,7 @@ function fn4() {
i = 0;
_a.label = 1;
case 1:
if (!(i < 1))
return [3 /*break*/, 4];
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, _loop_4(i)];
case 2:
state_2 = _a.sent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ function binaryLogicalAnd1() {
switch (_b.label) {
case 0:
_a = x;
if (!_a)
return [3 /*break*/, 2];
if (!_a) return [3 /*break*/, 2];
return [4 /*yield*/, y];
case 1:
_a = (_b.sent());
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/es5-asyncFunctionConditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function conditional1() {
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!x)
return [3 /*break*/, 2];
if (!x) return [3 /*break*/, 2];
return [4 /*yield*/, y];
case 1:
_a = _b.sent();
Expand All @@ -54,8 +53,7 @@ function conditional2() {
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!x)
return [3 /*break*/, 1];
if (!x) return [3 /*break*/, 1];
_a = y;
return [3 /*break*/, 3];
case 1: return [4 /*yield*/, z];
Expand Down
Loading