Ensure that emitter calls callbacks for empty blocks (#18547)

This commit is contained in:
Andy
2017-09-18 10:05:44 -07:00
committed by GitHub
parent 0ac8406cd7
commit 79e12eb48b
3 changed files with 57 additions and 36 deletions

View File

@@ -1440,29 +1440,18 @@ namespace ts {
//
function emitBlock(node: Block) {
if (isSingleLineEmptyBlock(node)) {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
write(" ");
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
else {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
emitBlockStatements(node);
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
increaseIndent();
emitLeadingCommentsOfPosition(node.statements.end);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
emitBlockStatements(node, /*forceSingleLine*/ !node.multiLine && isEmptyBlock(node));
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
increaseIndent();
emitLeadingCommentsOfPosition(node.statements.end);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
function emitBlockStatements(node: BlockLike) {
if (getEmitFlags(node) & EmitFlags.SingleLine) {
emitList(node, node.statements, ListFormat.SingleLineBlockStatements);
}
else {
emitList(node, node.statements, ListFormat.MultiLineBlockStatements);
}
function emitBlockStatements(node: BlockLike, forceSingleLine: boolean) {
const format = forceSingleLine || getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineBlockStatements : ListFormat.MultiLineBlockStatements;
emitList(node, node.statements, format);
}
function emitVariableStatement(node: VariableStatement) {
@@ -1889,16 +1878,11 @@ namespace ts {
}
function emitModuleBlock(node: ModuleBlock) {
if (isEmptyBlock(node)) {
write("{ }");
}
else {
pushNameGenerationScope();
write("{");
emitBlockStatements(node);
write("}");
popNameGenerationScope();
}
pushNameGenerationScope();
write("{");
emitBlockStatements(node, /*forceSingleLine*/ isEmptyBlock(node));
write("}");
popNameGenerationScope();
}
function emitCaseBlock(node: CaseBlock) {
@@ -2762,11 +2746,6 @@ namespace ts {
&& !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile);
}
function isSingleLineEmptyBlock(block: Block) {
return !block.multiLine
&& isEmptyBlock(block);
}
function isEmptyBlock(block: BlockLike) {
return block.statements.length === 0
&& rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);